Skip to content

Commit 8daa015

Browse files
authored
Merge pull request #243 from leonvogt/fix-dirty-tracking-for-boolean-attributes
Fix dirty tracking for boolean attributes
2 parents 115c399 + 6cb59f2 commit 8daa015

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

lib/attr_json/record.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def attr_json_sync_to_rails_attributes
6565
attr_name = attribute_def.name
6666
value = container_value[attribute_def.store_key]
6767

68-
if value
68+
unless value.nil?
6969
# TODO, can we just make this use the setter?
7070
write_attribute(attr_name, value)
7171

spec/record/rails_attribute_dirty_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
self.table_name = "products"
3030
attr_json :str, :string
3131
attr_json :int, :integer
32+
attr_json :bool, :boolean
3233
# just to make our changes more sane, set no default
3334
attr_json :str_array, :string, array: true, default: AttrJson::AttributeDefinition::NO_DEFAULT_PROVIDED
3435
attr_json :embedded, model_class_type
@@ -180,6 +181,20 @@
180181
expect(obj.will_save_change_to_str?).to be true
181182
end
182183
end
184+
185+
describe "boolean attribute dirty tracking" do
186+
it "does not report a change when assigning false to false" do
187+
instance.bool = false
188+
instance.save!
189+
instance.reload
190+
expect(instance.bool).to be false
191+
expect(instance.changes.empty?).to be true
192+
193+
instance.assign_attributes({ str: "new", bool: false })
194+
expect(instance.bool_changed?).to be false
195+
expect(instance.str_changed?).to be true
196+
end
197+
end
183198
end
184199

185200
describe "array" do

0 commit comments

Comments
 (0)