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
- #changed_attributes ⇒ Object
-
#has_changed?(key = nil) ⇒ Boolean
Return true if model was changed.
-
#reload ⇒ Object
Reverts model changes and returns saved version.
- #reset_changed_attributes ⇒ Object
- #save ⇒ Object
- #track_changed_attributes(&block) ⇒ Object
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
#changed_attributes ⇒ Object
28 29 30 |
# File 'motion-prime/models/_dirty_mixin.rb', line 28 def changed_attributes @_changed_attributes ||= {} end |
#has_changed?(key = nil) ⇒ Boolean
Return true if model was changed
40 41 42 43 44 45 46 |
# File 'motion-prime/models/_dirty_mixin.rb', line 40 def has_changed?(key = nil) if key changed_attributes.has_key?(key.to_s) else changed_attributes.present? end end |
#reload ⇒ Object
Reverts model changes and returns saved version
57 58 59 60 61 62 63 |
# File 'motion-prime/models/_dirty_mixin.rb', line 57 def reload changed_attributes.each do |key, value| self.info[key] = value end reset_changed_attributes self end |
#reset_changed_attributes ⇒ Object
32 33 34 |
# File 'motion-prime/models/_dirty_mixin.rb', line 32 def reset_changed_attributes @_changed_attributes = {} end |
#save ⇒ Object
48 49 50 51 52 |
# File 'motion-prime/models/_dirty_mixin.rb', line 48 def save super reset_changed_attributes self 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 |