Class: Mobility::Plugins::ActiveModel::Dirty::HandlerMethodsBuilder

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

Overview

Module builder which mimics dirty method handlers on a given dirty class. Used to mimic ActiveModel::Dirty and ActiveRecord::Dirty, which have similar but slightly different sets of handler methods. Doing it this way with introspection allows us to support basically all AR/AM versions without changes here.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass) ⇒ HandlerMethodsBuilder

Returns a new instance of HandlerMethodsBuilder.

Parameters:

  • klass (Class)

    Dirty class to mimic



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

def initialize(klass)
  @klass = klass
  define_handler_methods
end

Instance Attribute Details

#klassObject (readonly)

Returns the value of attribute klass.



120
121
122
# File 'lib/mobility/plugins/active_model/dirty.rb', line 120

def klass
  @klass
end

Instance Method Details

#define_handler_methodsObject



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/mobility/plugins/active_model/dirty.rb', line 134

def define_handler_methods
  public_patterns.each do |pattern|
    method_name = pattern % 'attribute'

    kwargs = pattern == '%s_changed?' ? ', **kwargs' : ''
    module_eval <<-EOM, __FILE__, __LINE__ + 1
    def #{method_name}(attr_name, *rest#{kwargs})
      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#{kwargs})
      else
        super
      end
    end
    EOM
  end
end

#each_pattern(attr_name) ⇒ Object



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

def each_pattern(attr_name)
  patterns.each do |pattern|
    yield pattern % attr_name, pattern % 'attribute'
  end
end

#patternsObject

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



154
155
156
157
158
159
160
161
162
163
# File 'lib/mobility/plugins/active_model/dirty.rb', line 154

def patterns
  @patterns ||=
    begin
      # Method name changes in Rails 7.1
      attribute_method_patterns = klass.respond_to?(:attribute_method_patterns) ?
        klass.attribute_method_patterns :
        klass.attribute_method_matchers
      attribute_method_patterns.map { |p| "#{p.prefix}%s#{p.suffix}" } - excluded_patterns
    end
end