Sha256: 74719505b3e4bb1a6af2ac862ed8789c1f2831b3fba4e8f31ec4c090b5a159bb

Contents?: true

Size: 1.71 KB

Versions: 4

Compression:

Stored size: 1.71 KB

Contents

module Mobility
  module Backend
=begin

Dirty tracking for Sequel models which use the +Sequel::Plugins::Dirty+ plugin.

@see http://sequel.jeremyevans.net/rdoc-plugins/index.html Sequel dirty plugin

=end
    module Sequel::Dirty
      # @!group Backend Accessors
      # @!macro backend_writer
      def write(locale, value, **options)
        locale_accessor = "#{attribute}_#{locale}".to_sym
        if model.column_changes.has_key?(locale_accessor) && model.initial_values[locale_accessor] == value
          super
          [model.changed_columns, model.initial_values].each { |h| h.delete(locale_accessor) }
        else
          model.will_change_column("#{attribute}_#{locale}".to_sym)
          super
        end
      end
      # @!endgroup

      # @param [Class] backend_class Class of backend
      def self.included(backend_class)
        backend_class.extend(ClassMethods)
      end

      # Adds hook after {Backend::Setup#setup_model} to add dirty-tracking
      # methods for translated attributes onto model class.
      module ClassMethods
        # (see Mobility::Backend::Setup#setup_model)
        def setup_model(model_class, attributes, **options)
          super
          model_class.class_eval do
            mod = Module.new do
              %w[initial_value column_change column_changed? reset_column].each do |method_name|
                define_method method_name do |column|
                  if attributes.map(&:to_sym).include?(column)
                    super("#{column}_#{Mobility.locale}".to_sym)
                  else
                    super(column)
                  end
                end
              end
            end
            include mod
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
mobility-0.1.3 lib/mobility/backend/sequel/dirty.rb
mobility-0.1.2 lib/mobility/backend/sequel/dirty.rb
mobility-0.1.1 lib/mobility/backend/sequel/dirty.rb
mobility-0.1.0 lib/mobility/backend/sequel/dirty.rb