module ActiveAudit::DirtyAssociation
Public Instance Methods
association_change(association_name)
click to toggle source
# File lib/active_audit/dirty_association.rb, line 53 def association_change association_name association_changes[association_name] end
association_changed?(association_name)
click to toggle source
# File lib/active_audit/dirty_association.rb, line 49 def association_changed? association_name !!association_changes[association_name] end
association_changes()
click to toggle source
# File lib/active_audit/dirty_association.rb, line 45 def association_changes @association_changes ||= ActiveSupport::HashWithIndifferentAccess.new end
association_previous_change(association_name)
click to toggle source
# File lib/active_audit/dirty_association.rb, line 65 def association_previous_change association_name association_previous_changes[association_name] end
association_previous_changes()
click to toggle source
# File lib/active_audit/dirty_association.rb, line 41 def association_previous_changes @association_previous_changes ||= ActiveSupport::HashWithIndifferentAccess.new end
association_previously_changed?(association_name)
click to toggle source
# File lib/active_audit/dirty_association.rb, line 61 def association_previously_changed? association_name !!association_previous_changes[association_name] end
association_was(association_name)
click to toggle source
# File lib/active_audit/dirty_association.rb, line 57 def association_was association_name association_changes[association_name].try '[]', 0 end
changes_applied()
click to toggle source
Calls superclass method
# File lib/active_audit/dirty_association.rb, line 69 def changes_applied super @association_previous_changes = association_changes @association_changes = ActiveSupport::HashWithIndifferentAccess.new end
define_association_method_suffix(association_name)
click to toggle source
# File lib/active_audit/dirty_association.rb, line 32 def define_association_method_suffix association_name %w(_changed? _change _was _previously_changed? _previous_change).each do |suffix| self.send :define_method, "#{association_name}#{suffix}" do send "association#{suffix}", association_name end end end
init_association_change(association_name, model, through, attribute)
click to toggle source
# File lib/active_audit/dirty_association.rb, line 76 def init_association_change association_name, model, through, attribute old_attributes = model.send(through).map {|r| r.send attribute } model.association_changes[association_name] = [old_attributes, old_attributes.dup] end
record_add(association_name, model, relation, through, attribute)
click to toggle source
# File lib/active_audit/dirty_association.rb, line 81 def record_add association_name, model, relation, through, attribute unless model.association_changes[association_name] init_association_change association_name, model, through, attribute end model.association_changes[association_name][1].push relation.send(attribute) end
record_remove(association_name, model, relation, through, attribute)
click to toggle source
# File lib/active_audit/dirty_association.rb, line 88 def record_remove association_name, model, relation, through, attribute unless model.association_changes[association_name] init_association_change association_name, model, through, attribute end model.association_changes[association_name][1].delete relation.send(attribute) end
stain(*association_names)
click to toggle source
# File lib/active_audit/dirty_association.rb, line 8 def stain *association_names association_names.each do |association_name| if reflection = _reflect_on_association(association_name) if reflection.collection? && through_refl = reflection.try(:through_reflection) through_id = reflection.foreign_key through_name = through_refl.name define_association_method_suffix association_name ActiveRecord::Associations::Builder::CollectionAssociation.define_callback(self, :before_add, through_name, before_add: lambda do |model, relation| DirtyAssociation.record_add association_name, model, relation, through_name, through_id end) ActiveRecord::Associations::Builder::CollectionAssociation.define_callback(self, :before_remove, through_name, before_remove: lambda do |model, relation| DirtyAssociation.record_remove association_name, model, relation, through_name, through_id end) else raise ArgumentError, "'#{association_name}' is not a many-to-many association." end else raise ArgumentError, "No association found for name `#{association_name}'. Has it been defined yet?" end end end