Module: MotionPrime::ModelDirtyMixin

Extended by:
MotionSupport::Concern
Included in:
Model
Defined in:
motion-prime/models/_dirty_mixin.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



5
6
7
# File 'motion-prime/models/_dirty_mixin.rb', line 5

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

Instance Method Details

#has_changed?(key = nil) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
31
32
33
34
35
# File 'motion-prime/models/_dirty_mixin.rb', line 28

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

#track_changed_attributes(&block) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'motion-prime/models/_dirty_mixin.rb', line 9

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