Revision 38964973
Added by Dominic Cleal over 6 years ago
app/models/concerns/dirty_associations.rb | ||
---|---|---|
# class Model
|
||
# dirty_has_many_associations :organizations, :locations
|
||
def dirty_has_many_associations(*args)
|
||
args.each do |association|
|
||
association_ids = association.to_s.singularize + '_ids'
|
||
extension = Module.new do
|
||
args.each do |association|
|
||
association_ids = association.to_s.singularize + '_ids'
|
||
|
||
# result for :organizations
|
||
# def organization_ids_with_change_detection=(organizations)
|
||
# organizations ||= []
|
||
# @organization_ids_changed = organizations.uniq.select(&:present?).map(&:to_i).sort != organization_ids.sort
|
||
# @organization_ids_was = organization_ids.clone
|
||
# self.organization_ids_without_change_detection = organizations
|
||
# end
|
||
define_method "#{association_ids}_with_change_detection=" do |collection|
|
||
collection ||= [] # in API, #{association}_ids is converted to nil if user sent empty array
|
||
instance_variable_set("@#{association_ids}_changed", collection.uniq.select(&:present?).map(&:to_i).sort != self.send(association_ids).sort)
|
||
instance_variable_set("@#{association_ids}_was", self.send(association_ids).clone)
|
||
self.send("#{association_ids}_without_change_detection=", collection)
|
||
end
|
||
alias_method_chain("#{association_ids}=", :change_detection)
|
||
# result for :organizations
|
||
# def organization_ids_with_change_detection=(organizations)
|
||
# organizations ||= []
|
||
# @organization_ids_changed = organizations.uniq.select(&:present?).map(&:to_i).sort != organization_ids.sort
|
||
# @organization_ids_was = organization_ids.clone
|
||
# self.organization_ids_without_change_detection = organizations
|
||
# end
|
||
define_method "#{association_ids}=" do |collection|
|
||
collection ||= [] # in API, #{association}_ids is converted to nil if user sent empty array
|
||
instance_variable_set("@#{association_ids}_changed", collection.uniq.select(&:present?).map(&:to_i).sort != self.send(association_ids).sort)
|
||
instance_variable_set("@#{association_ids}_was", self.send(association_ids).clone)
|
||
super(collection)
|
||
end
|
||
|
||
# result for :organizations
|
||
# def organization_ids_changed?
|
||
# @organization_ids_changed
|
||
# end
|
||
define_method "#{association_ids}_changed?" do
|
||
instance_variable_get("@#{association_ids}_changed")
|
||
end
|
||
# result for :organizations
|
||
# def organization_ids_changed?
|
||
# @organization_ids_changed
|
||
# end
|
||
define_method "#{association_ids}_changed?" do
|
||
instance_variable_get("@#{association_ids}_changed")
|
||
end
|
||
|
||
# result for :organizations
|
||
# def organization_ids_was
|
||
# @organization_ids_was ||= organization_ids
|
||
# end
|
||
define_method "#{association_ids}_was" do
|
||
value = instance_variable_get("@#{association_ids}_was")
|
||
if value.nil?
|
||
instance_variable_set("@#{association_ids}_was", self.send(association_ids))
|
||
else
|
||
value
|
||
# result for :organizations
|
||
# def organization_ids_was
|
||
# @organization_ids_was ||= organization_ids
|
||
# end
|
||
define_method "#{association_ids}_was" do
|
||
value = instance_variable_get("@#{association_ids}_was")
|
||
if value.nil?
|
||
instance_variable_set("@#{association_ids}_was", self.send(association_ids))
|
||
else
|
||
value
|
||
end
|
||
end
|
||
end
|
||
end
|
||
self.prepend(extension)
|
||
end
|
||
end
|
||
end
|
Also available in: Unified diff
Fixes #20957 - Replace alias_method_chain with Module prepend
Deprecated in Rails 5.0 and will be removed in 5.1. Some instances of
classes overwriting existing methods can be handled with `super`, other
concerns or modules are changed to use prepend instead of include.
Note: no ActiveSupport::Concern support for prepends, so load class
methods the pure Ruby way.