Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
264 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
lib/store_model/combine_errors_strategies/merge_array_error_strategy.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# frozen_string_literal: true | ||
|
||
module StoreModel | ||
module CombineErrorsStrategies | ||
# +MergeArrayErrorStrategy+ copies errors from the StoreModel::Model to the parent | ||
# record attribute errors. | ||
class MergeArrayErrorStrategy | ||
# Merges errors on +attribute+ from the child model with parent errors. | ||
# | ||
# @param attribute [String] name of the validated attribute | ||
# @param base_errors [ActiveModel::Errors] errors object of the parent record | ||
# @param store_models [Array] an array or store_models that have been validated | ||
def call(attribute, base_errors, store_models) | ||
store_models.each_with_index do |store_model, index| | ||
store_model.errors.full_messages.each do |full_message| | ||
base_errors.add(attribute, :invalid, message: "[#{index}] #{full_message}") | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
spec/store_model/combine_error_strategies/merge_array_error_strategy_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
RSpec.describe StoreModel::CombineErrorsStrategies::MergeArrayErrorStrategy do | ||
let(:custom_product_class) do | ||
build_custom_product_class do | ||
attribute :configurations, Configuration.to_array_type | ||
validates :configurations, store_model: true | ||
end | ||
end | ||
|
||
let(:record) do | ||
product = custom_product_class.new( | ||
configurations: | ||
[ | ||
Configuration.new(color: "red"), | ||
Configuration.new | ||
] | ||
) | ||
product.configurations.each(&:validate) | ||
product | ||
end | ||
|
||
it "adds message that associated object is invalid" do | ||
described_class.new.call(:configurations, record.errors, record.configurations) | ||
|
||
expect(record.errors.messages).to eq(configurations: ["[1] Color can't be blank"]) | ||
expect(record.errors.full_messages).to eq(["Configurations [1] Color can't be blank"]) | ||
|
||
expect(record.configurations.second.errors.messages).to eq(color: ["can't be blank"]) | ||
expect(record.configurations.second.errors.full_messages).to eq(["Color can't be blank"]) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters