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 " def \#{attribute}_\#{suffix}\n attribute_\#{suffix}(\"\#{attribute}_#\\{Mobility.locale\\}\")\n end\n EOM\n end\n end\n end\n\n restore_methods = Module.new do\n attributes.each do |attribute|\n locale_accessor = \"\#{attribute}_\#{Mobility.locale}\"\n define_method \"restore_\#{attribute}!\" do\n if attribute_changed?(locale_accessor)\n __send__(\"\#{attribute}=\", changed_attributes[locale_accessor])\n end\n end\n end\n\n define_method :restore_attribute! do |attr|\n if attributes.include?(attr.to_s)\n send(\"restore_\#{attr}!\")\n else\n super(attr)\n end\n end\n private :restore_attribute!\n end\n model_class.include restore_methods\nend\n", __FILE__, __LINE__ + 1 |