deferring 0.0.9 → 0.0.10
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/lib/deferring.rb +13 -11
- data/lib/deferring/deferred_association.rb +3 -15
- data/lib/deferring/version.rb +1 -1
- data/spec/lib/deferring_has_many_spec.rb +55 -20
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
M2Q5NGQyOGY3MTViMzE1ODA4ODgwNzdjZTRmNDhjMjMyYWI4MDc3OA==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
MDQ5ZTg4NTUyYmM3ZjgyNDM0NzI5ZTdhY2ZiMzMwMzkxM2M4NmM0Yw==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
ZmI0YTk4ZDIwOGZhNjFlMWViOTc3NGRmODhmYmE0YmI0NzgxZWM3ODI1ZGZl
|
10
|
+
YmEwODQ3YjRlN2JlYzJmODY2MTI3Y2FhZjM5MTlkMjBjNWI4YjYwNDkxZTBh
|
11
|
+
MTdhM2QxYzdhMjk1MGViM2JkOTQ0N2ZjZWRmYTlkYjAxNjVhOTU=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
ZGE0NGE5ODhiYmQwMzMzMjg0ZmZmOTc0YTVlMjJmMjQxZGZkMjExYTJlNGI2
|
14
|
+
ZmEzZmZhMmM0MGFmNjk4OTk2NmVkNzM0ZmUwMmUwMTEyZjc4ODhhZTM3MzRk
|
15
|
+
MjU4NDdhNTE1OTIzYTVmMjE2NmNjYWFkOTIzOTNjMDQ4YWIyMTk=
|
data/lib/deferring.rb
CHANGED
@@ -62,24 +62,26 @@ module Deferring
|
|
62
62
|
attributes = attributes.with_indifferent_access
|
63
63
|
|
64
64
|
if attributes['id'].blank?
|
65
|
-
if !
|
66
|
-
send(:"#{association_name}").build(attributes.except(*
|
65
|
+
if !deferred_reject_new_record?(attributes)
|
66
|
+
send(:"#{association_name}").build(attributes.except(*deferred_unassignable_keys))
|
67
67
|
end
|
68
68
|
|
69
69
|
elsif existing_record = send(:"#{association_name}").detect { |record| record.id.to_s == attributes['id'].to_s }
|
70
|
-
if !
|
70
|
+
if !deferred_call_reject_if(attributes)
|
71
71
|
|
72
|
-
existing_record.attributes = attributes.except(*
|
72
|
+
existing_record.attributes = attributes.except(*deferred_unassignable_keys)
|
73
73
|
|
74
74
|
# TODO: Implement value_to_boolean code from rails for checking _destroy field.
|
75
75
|
if attributes['_destroy'] == '1' && options[:allow_destroy]
|
76
|
-
# remove from existing records
|
76
|
+
# remove from existing records and mark for destruction upon
|
77
|
+
# saving
|
77
78
|
send(:"#{association_name}").delete(existing_record)
|
79
|
+
existing_record.mark_for_destruction
|
78
80
|
end
|
79
81
|
end
|
80
82
|
|
81
83
|
else # new record referenced by id
|
82
|
-
if !
|
84
|
+
if !deferred_call_reject_if(attributes)
|
83
85
|
klass = self.class.reflect_on_association(:"#{association_name}").klass
|
84
86
|
|
85
87
|
attribute_ids = attributes_collection.map { |a| a['id'] || a[:id] }.compact
|
@@ -96,12 +98,12 @@ module Deferring
|
|
96
98
|
# Determines if a new record should be build by checking for
|
97
99
|
# has_destroy_flag? or if a <tt>:reject_if</tt> proc exists for this
|
98
100
|
# association and evaluates to +true+.
|
99
|
-
define_method :
|
101
|
+
define_method :deferred_reject_new_record? do |attributes|
|
100
102
|
# TODO: Implement value_to_boolean code from rails for checking _destroy field.
|
101
|
-
attributes['_destroy'] == '1' ||
|
103
|
+
attributes['_destroy'] == '1' || deferred_call_reject_if(attributes)
|
102
104
|
end
|
103
105
|
|
104
|
-
define_method :
|
106
|
+
define_method :deferred_call_reject_if do |attributes|
|
105
107
|
return false if attributes['_destroy'] == '1'
|
106
108
|
case callback = reject_if
|
107
109
|
when Symbol
|
@@ -111,8 +113,8 @@ module Deferring
|
|
111
113
|
end
|
112
114
|
end
|
113
115
|
|
114
|
-
define_method :
|
115
|
-
%w(_destroy)
|
116
|
+
define_method :deferred_unassignable_keys do
|
117
|
+
%w(_destroy id)
|
116
118
|
end
|
117
119
|
|
118
120
|
generate_find_or_create_deferred_association_method
|
@@ -43,7 +43,7 @@ module Deferring
|
|
43
43
|
|
44
44
|
# Delegates methods from Ruby's Array module to the object in the deferred
|
45
45
|
# association.
|
46
|
-
delegate :[]=, :[], :clear, :
|
46
|
+
delegate :[]=, :[], :clear, :select!, :reject!, :flatten, :flatten!, :sort!,
|
47
47
|
:sort_by!, :empty?, :size, :length, to: :objects
|
48
48
|
|
49
49
|
# Delegates Ruby's Enumerable#find method to the original association.
|
@@ -90,13 +90,7 @@ module Deferring
|
|
90
90
|
@original_objects = original_association.to_a.clone
|
91
91
|
objects_loaded!
|
92
92
|
|
93
|
-
pending_deletes.each
|
94
|
-
run_deferring_callbacks(:unlink, record) do
|
95
|
-
if inverse_name && record.class.reflect_on_association(inverse_name)
|
96
|
-
record.send(:"#{inverse_name}=", nil)
|
97
|
-
end
|
98
|
-
end
|
99
|
-
end
|
93
|
+
pending_deletes.each { |record| run_deferring_callbacks(:unlink, record) }
|
100
94
|
pending_creates.each { |record| run_deferring_callbacks(:link, record) }
|
101
95
|
|
102
96
|
@objects
|
@@ -126,13 +120,7 @@ module Deferring
|
|
126
120
|
|
127
121
|
def delete(records)
|
128
122
|
Array(records).flatten.uniq.each do |record|
|
129
|
-
run_deferring_callbacks(:unlink, record)
|
130
|
-
if inverse_name && record.class.reflect_on_association(inverse_name)
|
131
|
-
record.send(:"#{inverse_name}=", nil)
|
132
|
-
end
|
133
|
-
|
134
|
-
objects.delete(record)
|
135
|
-
end
|
123
|
+
run_deferring_callbacks(:unlink, record) { objects.delete(record) }
|
136
124
|
end
|
137
125
|
self
|
138
126
|
end
|
data/lib/deferring/version.rb
CHANGED
@@ -102,24 +102,31 @@ RSpec.describe 'deferred has_many associations' do
|
|
102
102
|
|
103
103
|
describe 'accepts_nested_attributes' do
|
104
104
|
it 'sets associated records when posting an array of hashes' do
|
105
|
-
p = Person.first
|
106
|
-
p.issues << printer_issue << db_issue << sandwich_issue
|
107
|
-
p.save
|
108
|
-
|
109
|
-
# Destroy db and sandwich issues. Keep printer issue and create a new one.
|
110
105
|
p = Person.first
|
111
106
|
p.attributes = {
|
112
107
|
issues_attributes: [
|
113
108
|
{ id: printer_issue.id },
|
114
109
|
{ subject: 'Kapow!' },
|
115
|
-
{ id: sandwich_issue.id, _destroy: '1' },
|
116
|
-
{ id: db_issue.id, _destroy: '1' }
|
117
110
|
]
|
118
111
|
}
|
119
112
|
expect(p.issues.length).to eq(2)
|
120
113
|
expect(p.issue_ids).to eq([printer_issue.id, nil])
|
121
114
|
|
122
|
-
expect{ p.save! }.to change{ Person.first.issues.size }.from(
|
115
|
+
expect{ p.save! }.to change{ Person.first.issues.size }.from(0).to(2)
|
116
|
+
end
|
117
|
+
|
118
|
+
it 'sets associated records when posting a hash of hashes' do
|
119
|
+
p = Person.first
|
120
|
+
p.attributes = {
|
121
|
+
issues_attributes: {
|
122
|
+
first: { subject: 'Kapow!' },
|
123
|
+
second: { id: printer_issue.id }
|
124
|
+
}
|
125
|
+
}
|
126
|
+
expect(p.issues.length).to eq(2)
|
127
|
+
expect(p.issue_ids).to eq([nil, printer_issue.id])
|
128
|
+
|
129
|
+
expect{ p.save! }.to change{ Person.first.issues.size }.from(0).to(2)
|
123
130
|
end
|
124
131
|
|
125
132
|
it 'updates associated records' do
|
@@ -137,26 +144,54 @@ RSpec.describe 'deferred has_many associations' do
|
|
137
144
|
expect(Issue.find(printer_issue.id).subject).to eq 'Toner low!'
|
138
145
|
end
|
139
146
|
|
140
|
-
it 'sets
|
147
|
+
it 'sets the belongs_to association of the associated record' do
|
148
|
+
expect(printer_issue.person).to be_nil
|
149
|
+
bob.attributes = {
|
150
|
+
issues_attributes: [{ id: printer_issue.id }]
|
151
|
+
}
|
152
|
+
expect(bob.issues.first.person).to eq bob
|
153
|
+
end
|
154
|
+
|
155
|
+
it 'destroys an associated record when :allow_destroy is true' do
|
156
|
+
p = Person.first
|
157
|
+
p.issues << printer_issue << db_issue << sandwich_issue
|
158
|
+
p.save
|
159
|
+
|
160
|
+
# Destroy db and sandwich issues. Keep printer issue and create a new one.
|
141
161
|
p = Person.first
|
142
162
|
p.attributes = {
|
143
|
-
issues_attributes: {
|
144
|
-
first: { subject: 'Kapow!' },
|
145
|
-
second: { id: printer_issue.id }
|
146
|
-
}
|
163
|
+
issues_attributes: [{ id: sandwich_issue.id, _destroy: '1' }]
|
147
164
|
}
|
165
|
+
|
148
166
|
expect(p.issues.length).to eq(2)
|
149
|
-
expect(p.issue_ids).to eq([
|
167
|
+
expect(p.issue_ids).to eq([printer_issue.id, db_issue.id])
|
150
168
|
|
151
|
-
expect
|
169
|
+
expect(p.issues.unlinks.first).to eq(sandwich_issue)
|
170
|
+
|
171
|
+
expect{ p.save! }.to change{ Issue.count }.from(3).to(2)
|
152
172
|
end
|
153
173
|
|
154
|
-
it '
|
155
|
-
|
156
|
-
|
157
|
-
|
174
|
+
it 'does not destroy an associated record when :allow_destroy is false' do
|
175
|
+
Person.deferred_accepts_nested_attributes_for :issues, allow_destroy: false
|
176
|
+
|
177
|
+
p = Person.first
|
178
|
+
p.issues << printer_issue << db_issue << sandwich_issue
|
179
|
+
p.save
|
180
|
+
|
181
|
+
# Destroy db and sandwich issues. Keep printer issue and create a new one.
|
182
|
+
p = Person.first
|
183
|
+
p.attributes = {
|
184
|
+
issues_attributes: [{ id: sandwich_issue.id, _destroy: '1' }]
|
158
185
|
}
|
159
|
-
|
186
|
+
|
187
|
+
expect(p.issues.length).to eq(3)
|
188
|
+
expect(p.issue_ids).to eq([printer_issue.id, db_issue.id, sandwich_issue.id])
|
189
|
+
|
190
|
+
expect(p.issues.unlinks.size).to eq(0)
|
191
|
+
|
192
|
+
expect{ p.save! }.to_not change{ Issue.count }
|
193
|
+
|
194
|
+
Person.deferred_accepts_nested_attributes_for :issues, allow_destroy: true
|
160
195
|
end
|
161
196
|
end # accepts_nested_attributes
|
162
197
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: deferring
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.10
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Robin Roestenburg
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-09-
|
11
|
+
date: 2014-09-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|