DmitryTsepelev / store_model

Work with JSON-backed attributes as ActiveRecord-ish models
MIT License
1.04k stars 85 forks source link

fix: serialize empty has-many array #176

Closed bf4 closed 3 months ago

bf4 commented 3 months ago

Fixes https://github.com/DmitryTsepelev/store_model/issues/174

Root cause:

[].all? # => true
[].any? # => false
[ StoreModel::Model.allocate ].all? { |v| v.is_a?(StoreModel::Model) # => true

So, to check if all are StoreModel::Model, we need to

  1. (new) check the array has something in it
  2. check that everything in it is a StoreModel::Model
bf4 commented 3 months ago
Oddly, even though master is green in Github, I get the same errors locally on master as here. ``` Failures: 1) StoreModel::CombineErrorsStrategies::MergeErrorStrategy when parent model has validation keeps parent validation messages Failure/Error: expect(record.errors.messages).to eq( name: ["can't be blank"], configuration: ["Color can't be blank"] ) expected: {:configuration=>["Color can't be blank"], :name=>["can't be blank"]} got: ##, :name=>#})> (compared using ==) Diff: @@ -1,3 +1,3 @@ -:configuration => ["Color can't be blank"], -:name => ["can't be blank"], +{:name=>["can't be blank"], + :configuration=>["is invalid", "Color can't be blank"]} # ./spec/store_model/combine_error_strategies/merge_error_strategy_spec.rb:63:in `block (3 levels) in ' 2) StoreModel::Model#parent array store model parent updates parent after assignment Failure/Error: expect(configuration.parent).to eq(subject) expected: #< configuration: [#]> got: nil (compared using ==) Shared Example Group: "for array type" called from ./spec/store_model/model_spec.rb:270 # ./spec/store_model/model_spec.rb:213:in `block (4 levels) in ' 3) StoreModel::Model#parent json store model parent updates parent after assignment Failure/Error: expect(configuration.parent).to eq(subject) expected: #< configuration: #> got: nil (compared using ==) Shared Example Group: "for singular type" called from ./spec/store_model/model_spec.rb:244 # ./spec/store_model/model_spec.rb:190:in `block (4 levels) in ' Finished in 0.33743 seconds (files took 0.98431 seconds to load) 342 examples, 3 failures Failed examples: rspec ./spec/store_model/combine_error_strategies/merge_error_strategy_spec.rb:49 # StoreModel::CombineErrorsStrategies::MergeErrorStrategy when parent model has validation keeps parent validation messages rspec ./spec/store_model/model_spec.rb[1:6:2:2:2] # StoreModel::Model#parent array store model parent updates parent after assignment rspec ./spec/store_model/model_spec.rb[1:6:1:2:2] # StoreModel::Model#parent json store model parent updates parent after assignment Randomized with seed 16673 ```