Skip to content

Commit

Permalink
Merge pull request #149 from madeintandem/bugfix/non-jsonb-accessor-a…
Browse files Browse the repository at this point in the history
…ttributes

Do not write non-jsonb_accessor attributes
  • Loading branch information
haffla authored Jul 23, 2022
2 parents 23505e8 + 7e48893 commit 76c2137
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
5 changes: 2 additions & 3 deletions lib/jsonb_accessor/macro.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,8 @@ def jsonb_accessor(jsonb_attribute, field_types)

empty_named_attributes = names_to_store_keys.transform_values { nil }
empty_named_attributes.merge(value_with_named_keys).each do |name, attribute_value|
# Undefined keys: There might be things in the JSON that haven't been defined using jsonb_accessor
# It should still be possible to save arbitrary data in the JSON
next unless has_attribute? name
# Only proceed if this attribute has been defined using `jsonb_accessor`.
next unless names_to_store_keys.key?(name)

write_attribute(name, attribute_value)
end
Expand Down
8 changes: 8 additions & 0 deletions spec/jsonb_accessor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ def build_class(jsonb_accessor_config, &block)
self.table_name = "products"
jsonb_accessor :options, jsonb_accessor_config
instance_eval(&block) if block

attribute :bang, :string
end
end

Expand Down Expand Up @@ -274,6 +276,12 @@ def build_class(jsonb_accessor_config, &block)
expect(subklass_instance.sub).to be_nil
end
end

it "does not write a normal Ruby attribute" do
expect(instance.bang).to be_nil
instance.options = { bang: "bang" }
expect(instance.bang).to be_nil
end
end

context "dirty tracking for already persisted models" do
Expand Down

0 comments on commit 76c2137

Please sign in to comment.