Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore behavior to always store unknown attributes in the database #169

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/unknown_attributes.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ configuration = Configuration.to_type.cast_value(color: "red", archived: true)
configuration.as_json(serialize_unknown_attributes: true) # => {"color": "red", "archived": true}
```

Prior to version 2.1.1, unknown attributes are always stored in the database. However, starting from version 2.1.1, if `serialize_unknown_attributes` is set to `false`, unknown attributes will not be stored in the database.
In any case unknown attributes are always stored in the database.
2 changes: 1 addition & 1 deletion lib/store_model/types/one.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def cast_value(value)
def serialize(value)
case value
when @model_klass, Hash
ActiveSupport::JSON.encode(value)
ActiveSupport::JSON.encode(value, serialize_unknown_attributes: true)
else
super
end
Expand Down
34 changes: 32 additions & 2 deletions spec/store_model/types/many_polymorphic_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,19 @@
[
{
color: "red",
disabled_at: Time.new(2019, 2, 22, 12, 30).utc
model: nil,
active: true,
disabled_at: Time.new(2019, 2, 22, 12, 30).utc,
encrypted_serial: nil,
type: "left"
},
{
color: "green",
disabled_at: Time.new(2019, 3, 12, 8, 10).utc
model: nil,
active: false,
disabled_at: Time.new(2019, 3, 12, 8, 10).utc,
encrypted_serial: nil,
type: "right"
}
]
end
Expand Down Expand Up @@ -195,5 +203,27 @@
let(:value) { ActiveSupport::JSON.encode(attributes_array) }
include_examples "serialize examples"
end

context "when Array of instances is passed" do
let(:value) { attributes_array.map { |attrs| Configuration.new(attrs) } }
include_examples "serialize examples"

context "with unknown attributes" do
before do
value.each_with_index { |v, index| v.unknown_attributes[:archived] = index.even? }
end

[true, false].each do |serialize_unknown_attributes|
it "always includes unknown attributes regardless of the serialize_unknown_attributes option" do
StoreModel.config.serialize_unknown_attributes = serialize_unknown_attributes
expect(subject).to eq(
attributes_array.map.with_index do |attrs, index|
attrs.merge(value[index].unknown_attributes)
end.to_json
)
end
end
end
end
end
end
34 changes: 32 additions & 2 deletions spec/store_model/types/many_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,19 @@
[
{
color: "red",
disabled_at: Time.new(2019, 2, 22, 12, 30).utc
model: nil,
active: true,
disabled_at: Time.new(2019, 2, 22, 12, 30).utc,
encrypted_serial: nil,
type: "left"
},
{
color: "green",
disabled_at: Time.new(2019, 3, 12, 8, 10).utc
model: nil,
active: false,
disabled_at: Time.new(2019, 3, 12, 8, 10).utc,
encrypted_serial: nil,
type: "right"
}
]
end
Expand Down Expand Up @@ -162,5 +170,27 @@
let(:value) { ActiveSupport::JSON.encode(attributes_array) }
include_examples "serialize examples"
end

context "when Array of instances is passed" do
let(:value) { attributes_array.map { |attrs| Configuration.new(attrs) } }
include_examples "serialize examples"

context "with unknown attributes" do
before do
value.each_with_index { |v, index| v.unknown_attributes[:archived] = index.even? }
end

[true, false].each do |serialize_unknown_attributes|
it "always includes unknown attributes regardless of the serialize_unknown_attributes option" do
StoreModel.config.serialize_unknown_attributes = serialize_unknown_attributes
expect(subject).to eq(
attributes_array.map.with_index do |attrs, index|
attrs.merge(value[index].unknown_attributes)
end.to_json
)
end
end
end
end
end
end
13 changes: 13 additions & 0 deletions spec/store_model/types/one_polymorphic_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,19 @@
context "when Configuration instance is passed" do
let(:value) { Configuration.new(attributes) }
include_examples "serialize examples"

context "with unknown attributes" do
before do
value.unknown_attributes[:archived] = true
end

[true, false].each do |serialize_unknown_attributes|
it "always includes unknown attributes regardless of the serialize_unknown_attributes option" do
StoreModel.config.serialize_unknown_attributes = serialize_unknown_attributes
expect(subject).to eq(attributes.merge(value.unknown_attributes).to_json)
end
end
end
end
end
end
13 changes: 13 additions & 0 deletions spec/store_model/types/one_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,19 @@
it { is_expected.to be_a(String) }

it("is equal to attributes") { is_expected.to eq(attributes.merge(type: nil).to_json) }

context "with unknown attributes" do
before do
value.unknown_attributes[:archived] = true
end

[true, false].each do |serialize_unknown_attributes|
it "always includes unknown attributes regardless of the serialize_unknown_attributes option" do
StoreModel.config.serialize_unknown_attributes = serialize_unknown_attributes
expect(subject).to eq(attributes.merge(type: nil, **value.unknown_attributes).to_json)
end
end
end
end
end

Expand Down
Loading