Sha256: 2f6c47a192baba47393d2e1db0edefeedcc98bce20d91a239e513a3ce1828aa5

Contents?: true

Size: 990 Bytes

Versions: 12

Compression:

Stored size: 990 Bytes

Contents

module MotionPrime
  module ModelDirtyMixin
    extend ::MotionSupport::Concern

    def self.included(base)
      base.class_attribute :_changed_attributes
    end

    def track_changed_attributes(&block)
      @_changed_attributes ||= {}
      old_attrs = self.info.clone
      result = block.call
      new_attrs = self.info.clone
      new_bags = self._bags.clone
      new_attrs.each do |key, value|
        if value != old_attrs[key] && !@_changed_attributes.has_key?(key.to_s)
          @_changed_attributes[key.to_s] = old_attrs[key]
        end
      end
      new_bags.each do |key, value|
        if value.key != old_attrs[key] && !@_changed_attributes.has_key?(key.to_s)
          @_changed_attributes[key.to_s] = old_attrs[key]
        end
      end
      result
    end

    def has_changed?(key = nil)
      @_changed_attributes ||= {}
      if key
        @_changed_attributes.has_key?(key.to_s)
      else
        @_changed_attributes.present?
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
motion-prime-0.8.6 motion-prime/models/_dirty_mixin.rb
motion-prime-0.8.5 motion-prime/models/_dirty_mixin.rb
motion-prime-0.8.4 motion-prime/models/_dirty_mixin.rb
motion-prime-0.8.3 motion-prime/models/_dirty_mixin.rb
motion-prime-0.8.2 motion-prime/models/_dirty_mixin.rb
motion-prime-0.8.1 motion-prime/models/_dirty_mixin.rb
motion-prime-0.8.0 motion-prime/models/_dirty_mixin.rb
motion-prime-0.7.2 motion-prime/models/_dirty_mixin.rb
motion-prime-0.7.1 motion-prime/models/_dirty_mixin.rb
motion-prime-0.7.0 motion-prime/models/_dirty_mixin.rb
motion-prime-0.6.0 motion-prime/models/_dirty_mixin.rb
motion-prime-0.5.7 motion-prime/models/_dirty_mixin.rb