Class: Mobility::Plugins::ActiveModel::Dirty::MethodsBuilder

Inherits:
Module
  • Object
show all
Defined in:
lib/mobility/plugins/active_model/dirty.rb

Overview

Builds module which adds suffix/prefix methods for translated attributes so they act like normal dirty-tracked attributes.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*attribute_names) ⇒ MethodsBuilder

Returns a new instance of MethodsBuilder.


50
51
52
# File 'lib/mobility/plugins/active_model/dirty.rb', line 50

def initialize(*attribute_names)
  define_dirty_methods(attribute_names)
end

Class Method Details

.dirty_classObject


133
134
135
# File 'lib/mobility/plugins/active_model/dirty.rb', line 133

def dirty_class
  @dirty_class ||= Class.new { include ::ActiveModel::Dirty }
end

.handler_methods_moduleObject


101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/mobility/plugins/active_model/dirty.rb', line 101

def handler_methods_module
  @handler_methods_module ||= (AttributeHandlerMethods.new.tap do |mod|
    public_method_patterns.each do |pattern|
      method_name = pattern % 'attribute'

      mod.module_eval <<-EOM, __FILE__, __LINE__ + 1
      def #{method_name}(attr_name, *rest)
        if (mutations_from_mobility.attribute_changed?(attr_name) ||
            mutations_from_mobility.attribute_previously_changed?(attr_name))
          mutations_from_mobility.send(#{method_name.inspect}, attr_name, *rest)
        else
          super
        end
      end
      EOM
    end
  end)
end

.method_patternsObject

Get method suffixes. Creating an object just to get the list of suffixes is simplest given they change from Rails version to version.


122
123
124
125
# File 'lib/mobility/plugins/active_model/dirty.rb', line 122

def method_patterns
  @method_patterns ||=
    (dirty_class.attribute_method_matchers.map { |p| "#{p.prefix}%s#{p.suffix}" } - excluded_method_patterns)
end

.public_method_patternsObject


127
128
129
130
131
# File 'lib/mobility/plugins/active_model/dirty.rb', line 127

def public_method_patterns
  @public_method_patterns ||= method_patterns.select do |p|
    !dirty_class.private_instance_methods.include?(:"#{p % 'attribute'}")
  end
end

Instance Method Details

#append_locale(attr_name) ⇒ Object


66
67
68
# File 'lib/mobility/plugins/active_model/dirty.rb', line 66

def append_locale(attr_name)
  Mobility.normalize_locale_accessor(attr_name)
end

#included(model_class) ⇒ Object


54
55
56
57
58
59
60
61
62
63
64
# File 'lib/mobility/plugins/active_model/dirty.rb', line 54

def included(model_class)
  model_class.include InstanceMethods
  model_class.include handler_methods_module

  # In earlier versions of Rails, these methods are private
  %i[clear_attribute_changes clear_changes_information changes_applied].each do |method_name|
    if dirty_class.private_instance_methods.include?(method_name)
      model_class.class_eval { private method_name }
    end
  end
end