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

fix: serialize empty has-many array #176

Merged
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 lib/store_model/types/many_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def cast_value(value)
def serialize(value)
case value
when Array
return ActiveSupport::JSON.encode(value) unless value.all? { |v| v.is_a?(StoreModel::Model) }
return ActiveSupport::JSON.encode(value) if value.empty? || value.any? { |v| !v.is_a?(StoreModel::Model) }

ActiveSupport::JSON.encode(value,
serialize_unknown_attributes: value.first.serialize_unknown_attributes?,
Expand Down
7 changes: 7 additions & 0 deletions spec/store_model/types/many_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,13 @@
include_examples "serialize examples"
end

context "when any empty Array is passed" do
let(:value) { [] }
let(:attributes_array) { [] }

include_examples "serialize examples"
end

context "when String is passed" do
let(:value) { ActiveSupport::JSON.encode(attributes_array) }
include_examples "serialize examples"
Expand Down
Loading