Module: ActiveEntity::AttributeMethods::Dirty

Extended by:
ActiveSupport::Concern
Includes:
ActiveEntity::AMAttributeMethods
Defined in:
lib/active_entity/attribute_methods/dirty.rb

Constant Summary

Constants included from ActiveEntity::AMAttributeMethods

ActiveEntity::AMAttributeMethods::CALL_COMPILABLE_REGEXP, ActiveEntity::AMAttributeMethods::NAME_COMPILABLE_REGEXP

Instance Method Summary collapse

Methods included from ActiveEntity::AMAttributeMethods

#attribute_missing, #method_missing, #respond_to?, #respond_to_without_attributes?

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class ActiveEntity::AMAttributeMethods

Instance Method Details

#as_json(options = {}) ⇒ Object

:nodoc:



28
29
30
31
# File 'lib/active_entity/attribute_methods/dirty.rb', line 28

def as_json(options = {}) # :nodoc:
  options[:except] = [*options[:except], "mutations_from_database", "mutations_before_last_save"]
  super(options)
end

#attribute_changed?(attr_name, **options) ⇒ Boolean

Dispatch target for *_changed? attribute methods.

Returns:

  • (Boolean)


63
64
65
# File 'lib/active_entity/attribute_methods/dirty.rb', line 63

def attribute_changed?(attr_name, **options) # :nodoc:
  mutations_from_database.changed?(attr_name.to_s, **options)
end

#attribute_changed_in_place?(attr_name) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


130
131
132
# File 'lib/active_entity/attribute_methods/dirty.rb', line 130

def attribute_changed_in_place?(attr_name) # :nodoc:
  mutations_from_database.changed_in_place?(attr_name.to_s)
end

#attribute_previously_changed?(attr_name, **options) ⇒ Boolean

Dispatch target for *_previously_changed? attribute methods.

Returns:

  • (Boolean)


73
74
75
# File 'lib/active_entity/attribute_methods/dirty.rb', line 73

def attribute_previously_changed?(attr_name, **options) # :nodoc:
  mutations_before_last_save.changed?(attr_name.to_s, **options)
end

#attribute_previously_was(attr_name) ⇒ Object

Dispatch target for *_previously_was attribute methods.



78
79
80
# File 'lib/active_entity/attribute_methods/dirty.rb', line 78

def attribute_previously_was(attr_name) # :nodoc:
  mutations_before_last_save.original_value(attr_name.to_s)
end

#attribute_was(attr_name) ⇒ Object

Dispatch target for *_was attribute methods.



68
69
70
# File 'lib/active_entity/attribute_methods/dirty.rb', line 68

def attribute_was(attr_name) # :nodoc:
  mutations_from_database.original_value(attr_name.to_s)
end

#changedObject

Returns an array with the name of the attributes with unsaved changes.

person.changed # => []
person.name = 'bob'
person.changed # => ["name"]


58
59
60
# File 'lib/active_entity/attribute_methods/dirty.rb', line 58

def changed
  mutations_from_database.changed_attribute_names
end

#changed?Boolean

Returns true if any of the attributes has unsaved changes, false otherwise.

person.changed? # => false
person.name = 'bob'
person.changed? # => true

Returns:

  • (Boolean)


49
50
51
# File 'lib/active_entity/attribute_methods/dirty.rb', line 49

def changed?
  mutations_from_database.any_changes?
end

#changed_attributesObject

Returns a hash of the attributes with unsaved changes indicating their original values like attr => original value.

person.name # => "bob"
person.name = 'robert'
person.changed_attributes # => {"name" => "bob"}


106
107
108
# File 'lib/active_entity/attribute_methods/dirty.rb', line 106

def changed_attributes
  mutations_from_database.changed_values
end

#changesObject

Returns a hash of changed attributes indicating their original and new values like attr => [original value, new value].

person.changes # => {}
person.name = 'bob'
person.changes # => { "name" => ["bill", "bob"] }


116
117
118
# File 'lib/active_entity/attribute_methods/dirty.rb', line 116

def changes
  mutations_from_database.changes
end

#changes_appliedObject

Clears dirty data and moves changes to previous_changes and mutations_from_database to mutations_before_last_save respectively.



35
36
37
38
39
40
41
42
# File 'lib/active_entity/attribute_methods/dirty.rb', line 35

def changes_applied
  unless defined?(@attributes)
    mutations_from_database.finalize_changes
  end
  @mutations_before_last_save = mutations_from_database
  forget_attribute_assignments
  @mutations_from_database = nil
end

#clear_attribute_changes(attr_names) ⇒ Object



94
95
96
97
98
# File 'lib/active_entity/attribute_methods/dirty.rb', line 94

def clear_attribute_changes(attr_names)
  attr_names.each do |attr_name|
    clear_attribute_change(attr_name)
  end
end

#clear_changes_informationObject

Clears all dirty data: current changes and previous changes.



88
89
90
91
92
# File 'lib/active_entity/attribute_methods/dirty.rb', line 88

def clear_changes_information
  @mutations_before_last_save = nil
  forget_attribute_assignments
  @mutations_from_database = nil
end

#initialize_dup(other) ⇒ Object

:nodoc:



18
19
20
21
22
23
24
25
26
# File 'lib/active_entity/attribute_methods/dirty.rb', line 18

def initialize_dup(other) # :nodoc:
  super
  if self.class.respond_to?(:_default_attributes)
    @attributes = self.class._default_attributes.map do |attr|
      attr.with_value_from_user(@attributes.fetch_value(attr.name))
    end
  end
  @mutations_from_database = nil
end

#previous_changesObject

Returns a hash of attributes that were changed before the model was saved.

person.name # => "bob"
person.name = 'robert'
person.save
person.previous_changes # => {"name" => ["bob", "robert"]}


126
127
128
# File 'lib/active_entity/attribute_methods/dirty.rb', line 126

def previous_changes
  mutations_before_last_save.changes
end

#restore_attributes(attr_names = changed) ⇒ Object

Restore all previous data of the provided attributes.



83
84
85
# File 'lib/active_entity/attribute_methods/dirty.rb', line 83

def restore_attributes(attr_names = changed)
  attr_names.each { |attr_name| restore_attribute!(attr_name) }
end