Skip to content

Commit

Permalink
Merge 02d0837 into 2066b1e
Browse files Browse the repository at this point in the history
  • Loading branch information
iarie committed Sep 4, 2019
2 parents 2066b1e + 02d0837 commit 946c09d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/store_model/types/array_type.rb
Expand Up @@ -69,15 +69,19 @@ def changed_in_place?(raw_old_value, new_value)
# rubocop:disable Style/RescueModifier
def decode_and_initialize(array_value)
decoded = ActiveSupport::JSON.decode(array_value) rescue []
decoded.map { |attributes| @model_klass.new(attributes) }
decoded.map { |attributes| cast_model_type_value(attributes) }
end
# rubocop:enable Style/RescueModifier

def ensure_model_class(array)
array.map do |object|
object.is_a?(@model_klass) ? object : @model_klass.new(object)
object.is_a?(@model_klass) ? object : cast_model_type_value(object)
end
end

def cast_model_type_value(value)
@model_klass.to_type.cast_value(value)
end
end
end
end
45 changes: 45 additions & 0 deletions spec/store_model/types/array_type_spec.rb
Expand Up @@ -78,6 +78,51 @@
)
end
end

context "when some keys are not defined as attributes" do
shared_examples "for unknown attributes" do
it { is_expected.to be_a(Array) }

it "assigns attributes" do
expect(subject.first).to have_attributes(color: "red")
expect(subject.second).to have_attributes(color: "green")
end

it "assigns unknown_attributes" do
expect(subject.first.unknown_attributes).to eq(
"unknown_attribute" => "something", "one_more" => "anything"
)
expect(subject.second.unknown_attributes).to eq(
"unknown_attribute" => "something greeny", "one_more" => "anything greeny"
)
end
end

let(:attributes_array) do
[
{
color: "red",
unknown_attribute: "something",
one_more: "anything"
},
{
color: "green",
unknown_attribute: "something greeny",
one_more: "anything greeny"
}
]
end

context "when Hash is passed" do
let(:value) { attributes_array }
include_examples "for unknown attributes"
end

context "when String is passed" do
let(:value) { ActiveSupport::JSON.encode(attributes_array) }
include_examples "for unknown attributes"
end
end
end

describe "#serialize" do
Expand Down

0 comments on commit 946c09d

Please sign in to comment.