Class: Mobility::Sequel::ColumnChanges

Inherits:
Module
  • Object
show all
Defined in:
lib/mobility/sequel/column_changes.rb

Overview

Internal class used to force Sequel model to notice changes when mobility setter method is called.

Instance Method Summary collapse

Constructor Details

#initialize(attributes, column_affix: "%s") ⇒ ColumnChanges

Returns a new instance of ColumnChanges.

Parameters:

  • attributes (Array<String>)

    Backend attributes


13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/mobility/sequel/column_changes.rb', line 13

def initialize(attributes, column_affix: "%s")
  attributes.each do |attribute|
    define_method "#{attribute}=" do |value, **options|
      if !options[:super] && send(attribute) != value
        locale = options[:locale] || Mobility.locale
        column = (column_affix % attribute).to_sym
        attribute_with_locale = :"#{attribute}_#{Mobility.normalize_locale(locale)}"
        @changed_columns = changed_columns | [column, attribute.to_sym, attribute_with_locale]
      end
      super(value, **options)
    end
  end
end