Module: HasModerated::ActiveRecordHelpers

Defined in:
lib/has_moderated/active_record/active_record_helpers.rb

Class Method Summary collapse

Class Method Details

.add_moderations_association(klass) ⇒ Object

General



17
18
19
20
21
# File 'lib/has_moderated/active_record/active_record_helpers.rb', line 17

def self.add_moderations_association(klass)
  klass.class_eval do
    has_many :moderations, :as => :moderatable, :dependent => :destroy
  end
end

.foreign_key(reflection) ⇒ Object

AR specific



8
9
10
11
12
13
14
# File 'lib/has_moderated/active_record/active_record_helpers.rb', line 8

def self.foreign_key(reflection)
  if reflection.respond_to?(:foreign_key)
    reflection.foreign_key
  else # Rails < v3.1
    reflection.primary_key_name
  end
end

.get_assocs_for_moderation(assocs, from_record = nil) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/has_moderated/active_record/active_record_helpers.rb', line 58

def self.get_assocs_for_moderation assocs, from_record = nil
  from_record ||= self
  return if assocs.blank?

  if assocs == :all
    assocs = from_record.class.reflections.keys.reject do |r|
      r == :moderations
    end
  end

  assocs = [assocs] unless assocs.respond_to?("[]")

  # check for through assocs
  assocs = assocs.dup
  through_assocs = {}
  assocs.each do |assoc|
    join_model = from_record.class.reflections[assoc.to_sym].options[:through]
    if join_model
      join_model = join_model.to_sym
      through_assocs[join_model] ||= []
      through_assocs[join_model].push(assoc)
      assocs.push(join_model) unless assocs.include?(join_model)
      #assocs.delete(assoc)
    end
  end

  assoc_attrs = {}
  assocs.each do |assoc|
    one_assoc = []
    assoc_value = from_record.send(assoc)
    # if it's has_one it won't be an array
    assoc_value = [assoc_value] if assoc_value && assoc_value.class != Array
    assoc_value ||= []
    assoc_value.each do |m|
      if m.new_record?
        one_assoc.push(m.get_moderation_attributes)
      else
        one_assoc.push(m.id)
      end
      if through_assocs[assoc.to_sym]
        one_assoc.last[:associations] = get_assocs_for_moderation(:all, m)
      end
    end
    assoc_attrs[assoc] = one_assoc unless one_assoc.empty?
  end

  assoc_attrs
end

.get_default_moderation_attributes(record) ⇒ Object



23
24
25
# File 'lib/has_moderated/active_record/active_record_helpers.rb', line 23

def self.get_default_moderation_attributes(record)
  record.attributes
end

.hashize_association(from_record, assoc_name, m) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/has_moderated/active_record/active_record_helpers.rb', line 27

def self.hashize_association(from_record, assoc_name, m)
  # todo
  through_assocs = {}
  from_record.class.reflections.keys.each do |assoc|
    join_model = from_record.class.reflections[assoc.to_sym].options[:through]
    if join_model
      join_model = join_model.to_sym
      through_assocs[join_model] ||= []
      through_assocs[join_model].push(assoc)
    end
  end
  
  assoc = nil
  if m.class == Fixnum
    assoc = m
  elsif m.kind_of? Hash  # already a hash (for has_many :through association)
    assoc = m
  elsif m.respond_to?(:get_moderation_attributes) && m.new_record?
    assoc = m.get_moderation_attributes
  elsif m.respond_to?(:get_moderation_attributes)
    assoc = m.id
  else
    raise "don't know how to convert #{m.class} to hash"
  end
  
  if through_assocs[assoc_name.to_sym] # TODO !
    assoc[:associations] = get_assocs_for_moderation(:all, m)
  end
  assoc
end