Skip to content

Commit

Permalink
Merge pull request #225 from LyricL-Gitster/224-fix-polymorphic-valid…
Browse files Browse the repository at this point in the history
…ation

224: validate PolymorphicModels
  • Loading branch information
jrochkind authored May 30, 2024
2 parents 4dca354 + 924254c commit 5fb2bd6
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

* Keep nested model validation working in Rails post-7.1, by no longer trying to re-use ActiveRecord::Validations::AssociatedValidator, instead supplying our own custom code. https://github.com/jrochkind/attr_json/pull/220

*
* Validate PolymorphicModel instances. https://github.com/jrochkind/attr_json/pull/225 Thanks @LyricL-Gitster

*

Expand Down
2 changes: 1 addition & 1 deletion lib/attr_json/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ def attr_json(name, type, **options)
)

# By default, automatically validate nested models
if type.kind_of?(AttrJson::Type::Model) && options[:validate] != false
if (type.kind_of?(AttrJson::Type::Model) || type.kind_of?(AttrJson::Type::PolymorphicModel)) && options[:validate] != false
# Post validations up with something based on ActiveRecord::Validations::AssociatedValidator
self.validates_with ::AttrJson::Model::NestedModelValidator, attributes: [name.to_sym]
end
Expand Down
50 changes: 50 additions & 0 deletions spec/model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,56 @@ def self.model_name ; ActiveModel::Name.new(self, nil, "Klass") ; end
end
end

describe "nested polymorphic model with validation" do
let(:nested_class1) do
Class.new do
include AttrJson::Model
def self.model_name ; ActiveModel::Name.new(self, nil, "NestedClass1") ; end
attr_json :str1, :string
validates_presence_of :str1
end
end
let(:nested_class2) do
Class.new do
include AttrJson::Model
def self.model_name ; ActiveModel::Name.new(self, nil, "NestedClass2") ; end
attr_json :str2, :string
validates_presence_of :str2
end
end
let(:klass) do
class1 = nested_class1
class2 = nested_class2
Class.new do
include AttrJson::Model
def self.model_name ; ActiveModel::Name.new(self, nil, "Klass") ; end

attr_json :nested, AttrJson::Type::PolymorphicModel.new(class1, class2), array: true
end
end

it "is invalid when nested are" do
instance.nested = [nested_class1.new, nested_class2.new]

expect(instance.valid?).to be false
expect(instance.errors.key?(:nested)).to be true
expect(instance.errors[:nested]).to include("is invalid")

expect(instance.nested[0].errors.key?(:str1))
expect(instance.nested[0].errors[:str1]).to include a_string_matching(/\Acan'|’t be blank\z/)
expect(instance.errors.details[:nested].first[:value][0]).to be_kind_of(nested_class1)

expect(instance.nested[1].errors.key?(:str2))
expect(instance.nested[1].errors[:str2]).to include a_string_matching(/\Acan'|’t be blank\z/)
expect(instance.errors.details[:nested].first[:value][1]).to be_kind_of(nested_class2)
end

it "is valid when nested are" do
instance.nested = [nested_class1.new(str1: "something"), nested_class2.new(str2: "something")]
expect(instance.valid?).to be true
end
end

describe "nested array of models with validation" do
let(:nested_class) do
Class.new do
Expand Down

0 comments on commit 5fb2bd6

Please sign in to comment.