-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #220 from jrochkind/fix_nested_model_validation
Keep nested model validation working in Rails post 7.1 (undefined method `custom_validation_context?')
- Loading branch information
Showing
3 changed files
with
33 additions
and
6 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
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,27 @@ | ||
module AttrJson | ||
module Model | ||
# Used to validate an attribute in an AttrJson::Model whose values are other models, when | ||
# you want validation errors on the nested models to post up. | ||
# | ||
# This is based on ActiveRecord's own ActiveRecord::Validations::AssociatedValidator, and actually forked | ||
# from it at https://github.com/rails/rails/blob/e37adfed4eff3b43350ec87222a922e9c72d9c1b/activerecord/lib/active_record/validations/associated.rb | ||
# | ||
# We used to simply use an ActiveRecord::Validations::AssociatedValidator, but as of https://github.com/jrochkind/attr_json/pull/220 (e1e798142d) | ||
# it got ActiveRecord-specific functionality that no longer worked with our use case. | ||
# | ||
# No problem, the implementation is simple, we can provide it here, based on the last version that did work. | ||
class NestedModelValidator < ActiveModel::EachValidator | ||
def validate_each(record, attribute, value) | ||
if Array(value).reject { |r| valid_object?(r) }.any? | ||
record.errors.add(attribute, :invalid, **options.merge(value: value)) | ||
end | ||
end | ||
|
||
private | ||
def valid_object?(record) | ||
#(record.respond_to?(:marked_for_destruction?) && record.marked_for_destruction?) || record.valid? | ||
record.valid? | ||
end | ||
end | ||
end | ||
end |