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
30 31 32 |
# File 'motion-prime/models/_dirty_mixin.rb', line 30 def changed_attributes @_changed_attributes ||= {} end |
#has_changed?(key = nil) ⇒ Boolean
Return true if model was changed
42 43 44 45 46 47 48 |
# File 'motion-prime/models/_dirty_mixin.rb', line 42 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
59 60 61 62 63 64 65 |
# File 'motion-prime/models/_dirty_mixin.rb', line 59 def reload changed_attributes.each do |key, value| self.info[key] = value end reset_changed_attributes self end |
#reset_changed_attributes ⇒ Object
34 35 36 |
# File 'motion-prime/models/_dirty_mixin.rb', line 34 def reset_changed_attributes @_changed_attributes = {} end |
#save! ⇒ Object
50 51 52 53 54 |
# File 'motion-prime/models/_dirty_mixin.rb', line 50 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 27 28 |
# File 'motion-prime/models/_dirty_mixin.rb', line 9 def track_changed_attributes(&block) @_tracking_changes = true @_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 @_tracking_changes = false result end |