Project

General

Profile

« Previous | Next » 

Revision 38f854cd

Added by Tomer Brisker almost 6 years ago

Fixes #23965 - Only save ids for association audits (#5753)

Otherwise, we have issues when trying to check revisions of the audit
since it will fail trying to assign a string to an attribute that
expects an object id.

View differences:

app/models/concerns/audit_associations.rb
super.merge(associated_attributes)
end
private
protected
def find_association_class(name)
self.class.reflect_on_association(name).class_name.constantize
# Prevent associations from being set when looking at revisions since
# otherwise they will update the original object rather then the revision
def revision_with(attrs)
super(attrs.reject{|k, v| k.to_s.ends_with?('_ids')})
end
private
def associated_changes
audited_options[:associations].each_with_object({}) do |association, changes|
association_ids = "#{association.to_s.singularize}_ids"
if public_send("#{association_ids}_changed?")
change = public_send("#{association_ids}_change")
changes[association] = change.map do |ids|
associated_names(association, ids)
end
if public_send("#{association}_changed?")
changes[association] = public_send("#{association}_change")
end
end
end
def associated_attributes
audited_options[:associations].each_with_object({}) do |association, attributes|
ids = public_send("#{association.to_s.singularize}_ids")
attributes[association.to_s] = associated_names(association, ids)
attributes[association] = public_send(association)
end
end
def associated_names(association, ids)
find_association_class(association).where(id: ids).map(&:to_label).sort.join(', ')
end
end
module AssociationsDefinitions
def audited(options = {})
options[:associations] = Array(options[:associations])
options[:associations] = normalize_associations(options[:associations])
if options[:associations].present?
configure_dirty_associations(options[:associations])
end
......
end
def audit_associations(*associations)
new_associations = Array(associations)
new_associations = normalize_associations(associations)
if self.respond_to?(:audited_options)
configure_dirty_associations(new_associations)
self.audited_options[:associations] = self.audited_options[:associations] | new_associations
......
end
end
def normalize_associations(associations)
Array(associations).map do |association|
"#{association.to_s.singularize}_ids"
end
end
def configure_dirty_associations(associations)
include DirtyAssociations unless included_modules.include?(DirtyAssociations)
dirty_has_many_associations(*associations)

Also available in: Unified diff