deferring 0.0.7 → 0.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +8 -8
- data/gemfiles/rails_30.gemfile.lock +1 -1
- data/gemfiles/rails_32.gemfile.lock +1 -1
- data/gemfiles/rails_40.gemfile.lock +1 -1
- data/gemfiles/rails_41.gemfile.lock +1 -1
- data/lib/deferring.rb +28 -5
- data/lib/deferring/version.rb +1 -1
- data/spec/lib/deferring_has_many_spec.rb +22 -7
- data/spec/lib/deferring_nested_attributes_spec.rb +2 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
ZmJiMDFiYWJiZWI2Y2UzNTU3NDk3ZmNiOWNhNmYwNzBhNmUwZDk1Yg==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
Y2YwOWRmZjA1NzkwMzZiZGIwYzU0NDdjMWQ4MWFkZGRkODNhMjA1MQ==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
NDg0NTNkMzkwYjc2ZjJhZjgxNzE1NGMyMjE2YTcwMzVjNWE1MDEzYjM2MzVk
|
10
|
+
YTc3MDRjOGM4YjJmNTBhNGQ3YmMwYzQ5Njk1N2QxOWUxMTFhMmEzNWQwOGIw
|
11
|
+
YzNlOGFmNjAwZjg3OWI1NWY0NWZiY2VkNTdmMTM4Mzc2ZmY0NzA=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
N2RlNzQxOTBhYzk5NGQzN2Q0OWM1NjE4MDI4YmVmNjlkYmNhMTk0OGMwODk1
|
14
|
+
N2UyNzhiZGIzYzllMDVkYjY4MzJlNjViZWVhZGZhZTQxNWQ3ZmI5NDhiMzIx
|
15
|
+
MDBjZGFmYWE5YjZiOTAyMmYzOGM5NWZmMGRmZTZjOWIzNmU0NGM=
|
data/lib/deferring.rb
CHANGED
@@ -34,20 +34,43 @@ module Deferring
|
|
34
34
|
def deferred_accepts_nested_attributes_for(*args)
|
35
35
|
options = args.extract_options!
|
36
36
|
inverse_association_name = options.fetch(:as, self.name.underscore.to_sym)
|
37
|
+
reject_if_proc = options.delete(:reject_if)
|
37
38
|
accepts_nested_attributes_for(*args, options)
|
38
39
|
|
39
40
|
association_name = args.first.to_s
|
40
41
|
|
41
42
|
# teams_attributes=
|
42
|
-
define_method :"#{association_name}_attributes=" do |
|
43
|
+
define_method :"#{association_name}_attributes=" do |attributes|
|
43
44
|
find_or_create_deferred_association(association_name, [], inverse_association_name)
|
44
45
|
|
45
|
-
#
|
46
|
-
#
|
47
|
-
|
46
|
+
# Convert the attributes to an array if a Hash is passed. This is possible
|
47
|
+
# as the keys of the hash are ignored in this case.
|
48
|
+
#
|
49
|
+
# Example:
|
50
|
+
# {
|
51
|
+
# first: { name: 'Service Desk' },
|
52
|
+
# second: { name: 'DBA' }
|
53
|
+
# }
|
54
|
+
# becomes
|
55
|
+
# [
|
56
|
+
# { name: 'Service Desk' },
|
57
|
+
# { name: 'DBA'}
|
58
|
+
# ]
|
59
|
+
attributes = attributes.values if attributes.is_a? Hash
|
60
|
+
|
61
|
+
# Remove the attributes that are to be destroyed from the ids that are to
|
62
|
+
# be assigned to the DeferredAssociation instance.
|
63
|
+
attributes.reject! { |record| record.delete(:_destroy) == '1' }
|
64
|
+
|
65
|
+
# Remove the attributes that fail the pass :reject_if proc.
|
66
|
+
attributes.reject! { |record| reject_if_proc.call(record) } if reject_if_proc
|
48
67
|
|
49
68
|
klass = self.class.reflect_on_association(:"#{association_name}").klass
|
50
|
-
|
69
|
+
|
70
|
+
objects = attributes.map do |record|
|
71
|
+
record[:id] ? klass.find(record[:id]) : klass.new(record)
|
72
|
+
end
|
73
|
+
|
51
74
|
send(:"deferred_#{association_name}").objects = objects
|
52
75
|
end
|
53
76
|
|
data/lib/deferring/version.rb
CHANGED
@@ -101,24 +101,39 @@ RSpec.describe 'deferred has_many associations' do
|
|
101
101
|
end
|
102
102
|
|
103
103
|
describe 'accepts_nested_attributes' do
|
104
|
-
it '
|
104
|
+
it 'sets associated records when posting an array of hashes' do
|
105
105
|
p = Person.first
|
106
106
|
p.issues << printer_issue << db_issue << sandwich_issue
|
107
107
|
p.save
|
108
108
|
|
109
|
-
# Destroy db and sandwich issues. Keep printer issue.
|
109
|
+
# Destroy db and sandwich issues. Keep printer issue and create a new one.
|
110
110
|
p = Person.first
|
111
111
|
p.attributes = {
|
112
112
|
issues_attributes: [
|
113
113
|
{ id: printer_issue.id },
|
114
|
-
{
|
115
|
-
{ id:
|
114
|
+
{ subject: 'Kapow!' },
|
115
|
+
{ id: sandwich_issue.id, _destroy: '1' },
|
116
|
+
{ id: db_issue.id, _destroy: '1' }
|
116
117
|
]
|
117
118
|
}
|
118
|
-
expect(p.issues.length).to eq(
|
119
|
-
expect(p.issue_ids
|
119
|
+
expect(p.issues.length).to eq(2)
|
120
|
+
expect(p.issue_ids).to eq([printer_issue.id, nil])
|
120
121
|
|
121
|
-
expect{ p.save! }.to change{ Person.first.issues.size }.from(3).to(
|
122
|
+
expect{ p.save! }.to change{ Person.first.issues.size }.from(3).to(2)
|
123
|
+
end
|
124
|
+
|
125
|
+
it 'sets associated records when posting a hash of hashes' do
|
126
|
+
p = Person.first
|
127
|
+
p.attributes = {
|
128
|
+
issues_attributes: {
|
129
|
+
first: { subject: 'Kapow!' },
|
130
|
+
second: { id: printer_issue.id }
|
131
|
+
}
|
132
|
+
}
|
133
|
+
expect(p.issues.length).to eq(2)
|
134
|
+
expect(p.issue_ids).to eq([nil, printer_issue.id])
|
135
|
+
|
136
|
+
expect{ p.save! }.to change{ Person.first.issues.size }.from(0).to(2)
|
122
137
|
end
|
123
138
|
|
124
139
|
it 'sets the belongs_to association of the associated record' do
|
@@ -29,8 +29,8 @@ RSpec.describe 'deferred accepts_nested_attributes' do
|
|
29
29
|
p.attributes = {
|
30
30
|
teams_attributes: [
|
31
31
|
{ id: 1 },
|
32
|
-
{ id: 3, _destroy:
|
33
|
-
{ id: 2, _destroy:
|
32
|
+
{ id: 3, _destroy: '1' },
|
33
|
+
{ id: 2, _destroy: '1' }
|
34
34
|
]
|
35
35
|
}
|
36
36
|
expect(p.teams.length).to eq(1)
|