Module: NoBrainer::Document::Dirty

Extended by:
ActiveSupport::Concern
Defined in:
lib/no_brainer/document/dirty.rb

Defined Under Namespace

Modules: ClassMethods Classes: None

Instance Method Summary collapse

Instance Method Details

#_create(*args) ⇒ Object

1) We should save the changes as seen through read_attribute, because the user sees attributes through the read_attribute getters, but it’s near impossible because we would need to wrap the user defined getters, so we’ll go through _read_attribute. 2) We want to detect changes based on @_attributes to track things like undefined -> nil. Going through the getters will not give us that.



10
11
12
# File 'lib/no_brainer/document/dirty.rb', line 10

def _create(*args)
  super.tap { clear_dirtiness }
end

#_read_attribute(name) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/no_brainer/document/dirty.rb', line 69

def _read_attribute(name)
  super.tap do |value|
    # This take care of string/arrays/hashes that could change without going
    # through the setter.
    attribute_may_change(name, value) if value.respond_to?(:size)
  end
end

#_update(*args) ⇒ Object



14
15
16
# File 'lib/no_brainer/document/dirty.rb', line 14

def _update(*args)
  super.tap { clear_dirtiness }
end

#_write_attribute(name, value) ⇒ Object



77
78
79
80
# File 'lib/no_brainer/document/dirty.rb', line 77

def _write_attribute(name, value)
  attribute_may_change(name)
  super
end

#attribute_may_change(attr, current_value = None) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/no_brainer/document/dirty.rb', line 49

def attribute_may_change(attr, current_value = None)
  if current_value == None
    current_value = begin
      assert_access_field(attr)
      _read_attribute(attr)
    rescue NoBrainer::Error::MissingAttribute => e
      e
    end
  end

  unless @_old_attributes.key?(attr)
    @_old_attributes[attr] = current_value.deep_dup
  end
end

#attribute_will_change!Object



64
65
66
67
# File 'lib/no_brainer/document/dirty.rb', line 64

def attribute_will_change!(*)
  # Provided for comatibility. See issue #190
  :not_implemented_in_no_brainer_see_issue_190
end

#changedObject



33
34
35
# File 'lib/no_brainer/document/dirty.rb', line 33

def changed
  changes.keys
end

#changed?Boolean

Returns:



29
30
31
# File 'lib/no_brainer/document/dirty.rb', line 29

def changed?
  changes.present?
end

#changesObject



37
38
39
40
41
42
43
44
45
46
# File 'lib/no_brainer/document/dirty.rb', line 37

def changes
  result = {}.with_indifferent_access
  @_old_attributes.each do |attr, old_value|
    current_value = _read_attribute(attr)
    if current_value != old_value || !@_old_attributes_keys.include?(attr)
      result[attr] = [old_value, current_value]
    end
  end
  result
end

#clear_dirtiness(options = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/no_brainer/document/dirty.rb', line 18

def clear_dirtiness(options={})
  if options[:keep_ivars] && options[:missing_attributes].try(:[], :pluck)
    attrs = options[:missing_attributes][:pluck].keys
    @_old_attributes = @_old_attributes.reject { |k,v| attrs.include?(k) }
  else
    @_old_attributes = {}.with_indifferent_access
  end

  @_old_attributes_keys = @_attributes.keys # to track undefined -> nil changes
end