Module: Mobility::Backend::ActiveModel::Dirty::ClassMethods
- Defined in:
- lib/mobility/backend/active_model/dirty.rb
Overview
Adds hook after Setup#setup_model to add dirty-tracking methods for translated attributes onto model class.
Instance Method Summary collapse
-
#setup_model(model_class, attributes, **options) ⇒ Object
Call setup block on a class with attributes and options.
Instance Method Details
#setup_model(model_class, attributes, **options) ⇒ Object
Call setup block on a class with attributes and options.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/mobility/backend/active_model/dirty.rb', line 46 def setup_model(model_class, attributes, **) super model_class.class_eval do %w[changed? change was will_change! previously_changed? previous_change].each do |suffix| attributes.each do |attribute| class_eval <<-EOM, __FILE__, __LINE__ + 1 def #{attribute}_#{suffix} attribute_#{suffix}("#{attribute}_#\{Mobility.locale\}") end EOM end end end restore_methods = Module.new do attributes.each do |attribute| locale_accessor = "#{attribute}_#{Mobility.locale}" define_method "restore_#{attribute}!" do if attribute_changed?(locale_accessor) __send__("#{attribute}=", changed_attributes[locale_accessor]) end end end define_method :restore_attribute! do |attr| if attributes.include?(attr.to_s) send("restore_#{attr}!") else super(attr) end end private :restore_attribute! end model_class.include restore_methods end |