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.6'.freeze

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:



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/active_resource/dirty.rb', line 55

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



40
41
42
43
44
45
46
47
# File 'lib/active_resource/dirty.rb', line 40

def changes_applied
  if new_active_model_version?
    super
  else
    @previously_changed = changes
    @attributes_changed_by_setter = ActiveSupport::HashWithIndifferentAccess.new
  end
end

#reloadObject

reload the record and clears changed attributes.



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

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

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

Returns:

  • (Boolean)


35
36
37
# File 'lib/active_resource/dirty.rb', line 35

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

#save_without_validationObject

Save the record and clears changed attributes if successful.



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

def save_without_validation(*)
  run_callbacks :save do
    saved = new? ? create : update
    changes_applied if saved
    saved
  end
end

#update_attributes(attributes) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/active_resource/dirty.rb', line 21

def update_attributes(attributes)
  unless attributes.respond_to?(:to_hash)
    raise ArgumentError, 'expected attributes to be able to convert'\
      " to Hash, got #{attributes.inspect}"
  end

  attributes = attributes.to_hash
  attributes.each do |key, value|
    send("#{key}=".to_sym, value)
  end

  save
end