Skip to content

Commit 4052c9d

Browse files
committed
Rework result logic so results are preserved and are only overridden by save if save returns a result.
1 parent 4941416 commit 4052c9d

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

lib/jsonapi/resource.rb

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,25 +35,28 @@ def is_new?
3535
end
3636

3737
def change(callback)
38+
result = nil
39+
3840
if @changing
3941
run_callbacks callback do
4042
yield
4143
end
4244
else
4345
run_callbacks is_new? ? :create : :update do
4446
@changing = true
45-
result = nil
4647
run_callbacks callback do
4748
result = yield
4849
end
4950

5051
if @save_needed || is_new?
51-
save
52-
else
53-
return result
52+
save_result = save
53+
result = save_result if save_result
5454
end
55+
5556
end
5657
end
58+
59+
return result
5760
end
5861

5962
def remove
@@ -129,6 +132,8 @@ def _save
129132

130133
def _remove
131134
@model.destroy
135+
136+
return
132137
end
133138

134139
def _create_has_many_links(association_type, association_key_values)
@@ -163,6 +168,8 @@ def _replace_has_one_link(association_type, association_key_value)
163168

164169
send("#{association.foreign_key}=", association_key_value)
165170
@save_needed = true
171+
172+
return
166173
end
167174

168175
def _remove_has_many_link(association_type, key)
@@ -178,6 +185,8 @@ def _remove_has_one_link(association_type)
178185

179186
send("#{association.foreign_key}=", nil)
180187
@save_needed = true
188+
189+
return
181190
end
182191

183192
def _replace_fields(field_data)
@@ -203,6 +212,8 @@ def _replace_fields(field_data)
203212
field_data[:has_many].each do |association_type, values|
204213
replace_has_many_links(association_type, values)
205214
end if field_data[:has_many]
215+
216+
return
206217
end
207218

208219
class << self

0 commit comments

Comments
 (0)