Module: HasModerated::Associations::Collection::AssociationHelpers

Defined in:
lib/has_moderated/associations/collection.rb

Class Method Summary collapse

Class Method Details

.add_assoc_to_record(to, record, reflection) ⇒ Object



109
110
111
112
113
114
115
# File 'lib/has_moderated/associations/collection.rb', line 109

def self.add_assoc_to_record(to, record, reflection)
  if reflection.macro == :has_and_belongs_to_many || !reflection.options[:through].blank?
    add_assoc_to_record_habtm_hmt(to, record, reflection)
  else
    add_assoc_to_record_hm(to, record, reflection)
  end
end

.add_assoc_to_record_habtm_hmt(target, assoc_record, reflection) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/has_moderated/associations/collection.rb', line 62

def self.add_assoc_to_record_habtm_hmt(target, assoc_record, reflection)
  field = if reflection.options[:join_table].present?
    join_table = reflection.options[:join_table].to_s
    results = assoc_record.class.reflections.reject do |assoc_name, assoc|
      !(assoc.options[:join_table] && assoc.options[:join_table].to_s == join_table)
    end
    if results.count != 1
      raise "has_moderated: Cannot determine join table for a Habtm association! Are you missing has_and_belongs_to_many in one of your models?"
    end
    results.first[1].name.to_s
  elsif reflection.options[:through].present?
    through_model = reflection.options[:through].to_s
    results = assoc_record.class.reflections.reject do |assoc_name, assoc|
      !(assoc.options[:through] && assoc.options[:through].to_s == through_model)
    end
    if results.count != 1
      raise "has_moderated: Cannot determine correct association for a has_many :through association!"
    end
    results.first[1].name.to_s
  else
    raise "has_moderated: Cannot determine association details!"
  end
  assoc_record.send(field) << target
end

.add_assoc_to_record_hm(to, record, reflection) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/has_moderated/associations/collection.rb', line 87

def self.add_assoc_to_record_hm(to, record, reflection)
  fk = HasModerated::ActiveRecordHelpers::foreign_key(reflection).try(:to_s)
  field = if !reflection.options[:as].blank?
    reflection.options[:as].to_s
  elsif !fk.blank?
    all_keys = []
    results = record.class.reflections.reject do |assoc_name, assoc|
      all_keys.push(HasModerated::ActiveRecordHelpers::foreign_key(assoc).try(:to_s))
      !(HasModerated::ActiveRecordHelpers::foreign_key(assoc).try(:to_s) == fk)
    end
    if results.blank?
      raise "Please set foreign_key for both belongs_to and has_one/has_many! fk: #{fk}, keys: #{all_keys.to_yaml}"
    end
    results.first[1].name.to_s
  else
    to.class.to_s.underscore # TODO hardcoded, fix
  end
  HasModerated::Common::try_without_moderation(record) do
    record.send(field + "=", to)
  end
end

.delete_assoc_from_record(from, assoc_id, reflection) ⇒ Object



117
118
119
120
121
122
123
124
125
126
# File 'lib/has_moderated/associations/collection.rb', line 117

def self.delete_assoc_from_record(from, assoc_id, reflection)
  return unless from && assoc_id
  klass = reflection.class_name.constantize

  if reflection.macro == :has_and_belongs_to_many || !reflection.options[:through].blank? || reflection.macro == :has_many
    from.send(reflection.name).delete(klass.find_by_id(assoc_id))
  else
    raise "Cannot delete association for this type of associations!"
  end
end