active_resource-dirty 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9bae3ae674fc1887feef90c75c2b1afab97b88f7ae1be51efbc3ea6a62934abc
4
+ data.tar.gz: '09c5686dff4d771bb2683ea6465b78dc4e582cc010cc988f53845e1f0ecd421d'
5
+ SHA512:
6
+ metadata.gz: 6a16cada9b46458f3797f6c648fdd309e5c9f51c88559d516cc17631e72a0cad6283540ff56d0ddd210a51f041e99c652629df59cf32927e07b91d303231dece
7
+ data.tar.gz: 5e33f13ec08d58790eac191f29a4028b6add1765ec22f79b3baf2438c2d5469f3a1ed22ae1568e1ac7b3a992a91c2142e116a0a79f56936c688640370336caab
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2019 Francisco Caiceo
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,61 @@
1
+ # ActiveResource::Dirty
2
+ `ActiveResource::Dirty` is a monkey patch that supports `ActiveModel::Dirty` methods in `ActiveResource`.
3
+
4
+ ## Usage
5
+ Provides a way to track changes in your object. For example:
6
+ ```ruby
7
+ person = Person.find(1)
8
+ person.changed? # => false
9
+ person.name = 'Frank'
10
+ person.changes # => {"name"=>[nil, 'Frank']}
11
+ person.name_was # nil
12
+ person.save
13
+ person.changes # => {}
14
+ person.previous_changes # => {"name"=>[nil, 'Frank']}
15
+ ```
16
+
17
+ ### PATCH Requests
18
+ It uses the http `PATCH` method instead of `PUT`, and sends in the body only the attributes that have changed. This feature requires the flag `patch_updates`.
19
+ ```ruby
20
+ class Person < ActiveResource::Base
21
+ self.site = 'http://someapi.com'
22
+ self.patch_updates = true
23
+ end
24
+ ```
25
+
26
+ ```ruby
27
+ person = Person.find(1)
28
+ person.name = 'Frank'
29
+ person.save
30
+ # Sends this request:
31
+ # PATCH http://someapi.com/people/1.json
32
+ # {"name":"Frank"}
33
+ #
34
+ ```
35
+ ## Considerations
36
+ This is a monkey patch that overrides methods from both `ActiveResource` and `ActiveModel::Dirty` (the latter one only for ActiveResource::Base. It does not affect `ActiveModel` for other uses). Any change in this methods in future versions can lead to unexpected results. So use with caution.
37
+
38
+ ## Installation
39
+ Add this line to your application's Gemfile:
40
+
41
+ ```ruby
42
+ gem 'active_resource-dirty'
43
+ ```
44
+
45
+ And then execute:
46
+ ```bash
47
+ $ bundle
48
+ ```
49
+
50
+ Or install it yourself as:
51
+ ```bash
52
+ $ gem install active_resource-dirty
53
+ ```
54
+
55
+ ## Requirements
56
+ * activemodel ~> 5.2.0
57
+ * activeresource ~> 5.1.0
58
+ * activesupport ~> 5.2.0
59
+
60
+ ## License
61
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new(:test) do |t|
4
+ t.libs << 'test'
5
+ t.pattern = 'test/**/*_test.rb'
6
+ t.verbose = false
7
+ end
8
+
9
+ task default: :test
@@ -0,0 +1,9 @@
1
+ module ActiveResource
2
+ module Dirty
3
+ module PatchUpdates
4
+ def self.included(base)
5
+ base.class_attribute :patch_updates
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ module ActiveResource
2
+ module Dirty
3
+ VERSION = '1.0.0'
4
+ end
5
+ end
@@ -0,0 +1,86 @@
1
+ require 'active_resource/dirty/patch_updates'
2
+
3
+ module ActiveResource
4
+ module Dirty
5
+ # Save the record and clears changed attributes if successful.
6
+ def save(*)
7
+ if (status = super)
8
+ changes_applied
9
+ end
10
+ status
11
+ end
12
+
13
+ # <tt>reload</tt> the record and clears changed attributes.
14
+ def reload(*)
15
+ super.tap do
16
+ clear_changes_information
17
+ end
18
+ end
19
+
20
+ def respond_to_missing?(method_name, include_private = false)
21
+ method_name.to_s.end_with?('=') || super
22
+ end
23
+
24
+ # Monkey patch
25
+ def changes_applied
26
+ @previously_changed = changes
27
+ @attributes_changed_by_setter = ActiveSupport::HashWithIndifferentAccess.new
28
+ end
29
+
30
+ private
31
+
32
+ def method_missing(method_symbol, *arguments) #:nodoc:
33
+ method_name = method_symbol.to_s
34
+ if method_name =~ /(=)$/ && attributes.key?($`)
35
+ new_value = arguments.first
36
+
37
+ if attribute_changed?($`) && changed_attributes[$`] == new_value
38
+ # Reset status if already changed and we are returning to the original value
39
+ clear_attribute_changes([$`])
40
+ elsif attributes[$`] != new_value
41
+ # yield change if value changed otherwise
42
+ attribute_will_change!($`)
43
+ end
44
+ end
45
+ super
46
+ end
47
+
48
+ # Monkey patch
49
+ def mutations_from_database
50
+ @mutations_from_database ||= ActiveModel::NullMutationTracker.instance
51
+ end
52
+
53
+ # Monkey patch
54
+ def forget_attribute_assignments; end
55
+
56
+ def encode_changed_attributes(options = {})
57
+ format_options = options.merge(only: keys_for_partial_write)
58
+ send("to_#{self.class.format.extension}", format_options)
59
+ end
60
+
61
+ def keys_for_partial_write
62
+ changed_attributes.keys
63
+ end
64
+
65
+ protected
66
+
67
+ # Update the resource on the remote service.
68
+ def update
69
+ return super unless patch_updates
70
+
71
+ run_callbacks :update do
72
+ connection.patch(element_path(prefix_options),
73
+ encode_changed_attributes,
74
+ self.class.headers).tap do |response|
75
+ load_attributes_from_response(response)
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
81
+
82
+ ActiveSupport.on_load(:active_resource) do
83
+ prepend ActiveResource::Dirty
84
+ include ActiveModel::Dirty
85
+ include ActiveResource::Dirty::PatchUpdates
86
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_resource-dirty
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Francisco Caiceo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-07-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activemodel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 5.2.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 5.2.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: activeresource
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 5.1.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 5.1.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: activesupport
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 5.2.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 5.2.0
55
+ description: Monkey Patch to support ActiveModel::Dirty methods in ActiveResource
56
+ email:
57
+ - jfcaiceo55@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - MIT-LICENSE
63
+ - README.md
64
+ - Rakefile
65
+ - lib/active_resource/dirty.rb
66
+ - lib/active_resource/dirty/patch_updates.rb
67
+ - lib/active_resource/dirty/version.rb
68
+ homepage: https://github.com/jfcaiceo/devise-activeresource
69
+ licenses:
70
+ - MIT
71
+ metadata: {}
72
+ post_install_message:
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirements: []
87
+ rubyforge_project:
88
+ rubygems_version: 2.7.6.2
89
+ signing_key:
90
+ specification_version: 4
91
+ summary: ActiveModel::Dirty support for ActiveResource
92
+ test_files: []