Module: ActiveResource::Dirty

Defined in:
lib/active_resource/dirty.rb,
lib/active_resource/dirty/version.rb,
lib/active_resource/dirty/patch_updates.rb

Defined Under Namespace

Modules: PatchUpdates

Constant Summary collapse

VERSION =
'1.0.2'

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_symbol, *arguments) ⇒ Object (private)

:nodoc:



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/active_resource/dirty.rb', line 32

def method_missing(method_symbol, *arguments) #:nodoc:
  method_name = method_symbol.to_s
  if method_name =~ /(=)$/ && attributes.key?($`)
    new_value = arguments.first

    if attribute_changed?($`) && changed_attributes[$`] == new_value
      # Reset status if already changed and we are returning to the original value
      clear_attribute_changes([$`])
    elsif attributes[$`] != new_value
      # yield change if value changed otherwise
      attribute_will_change!($`)
    end
  end
  super
end

Instance Method Details

#changes_appliedObject

Monkey patch



25
26
27
28
# File 'lib/active_resource/dirty.rb', line 25

def changes_applied
  @previously_changed = changes
  @attributes_changed_by_setter = ActiveSupport::HashWithIndifferentAccess.new
end

#reloadObject

reload the record and clears changed attributes.



14
15
16
17
18
# File 'lib/active_resource/dirty.rb', line 14

def reload(*)
  super.tap do
    clear_changes_information
  end
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/active_resource/dirty.rb', line 20

def respond_to_missing?(method_name, include_private = false)
  method_name.to_s.end_with?('=') || super
end

#saveObject

Save the record and clears changed attributes if successful.



6
7
8
9
10
11
# File 'lib/active_resource/dirty.rb', line 6

def save(*)
  if (status = super)
    changes_applied
  end
  status
end