aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/lib
diff options
context:
space:
mode:
authoreileencodes <eileencodes@gmail.com>2016-02-20 09:15:39 -0500
committereileencodes <eileencodes@gmail.com>2016-02-20 09:15:39 -0500
commit6f15b276cb69ae1241e5c021fdc8e73fd3fe1197 (patch)
tree72e4ee8e88d583fce9b2658522ea6c2dc1ef2d55 /activemodel/lib
parent3156a7692c3c51adb846252192364172b05bd67f (diff)
downloadrails-6f15b276cb69ae1241e5c021fdc8e73fd3fe1197.tar.gz
rails-6f15b276cb69ae1241e5c021fdc8e73fd3fe1197.tar.bz2
rails-6f15b276cb69ae1241e5c021fdc8e73fd3fe1197.zip
Always validate record if validating a virtual attribute
Fixes #23645 When you're using an `attr_accessor` for a record instead of an attribute in the database there's no way for the record to know if it has `changed?` unless you tell it `attribute_will_change!("attribute")`. The change made in 27aa4dd updated validations to check if a record was `changed?` or `marked_for_destruction?` or not `persisted?`. It did not take into account virtual attributes that do not affect the model's dirty status. The only way to fix this is to always validate the record if the attribute does not belong to the set of attributes the record expects (in `record.attributes`) because virtual attributes will not be in that hash. I think we should consider deprecating this particular behavior in the future and requiring that the user mark the record dirty by noting that the virtual attribute will change. Unfortunately this isn't easy because we have no way of knowing that you did the "right thing" in your application by marking it dirty and will get the deprecation warning even if you are doing the correct thing. For now this restores expected behavior when using a virtual attribute by always validating the record, as well as adds tests for this case. I was going to add the `!record.attributes.include?(attribute)` to the `should_validate?` method but `uniqueness` cannot validate a virtual attribute with nothing to hold on to the attribute. Because of this `should_validate?` was about to become a very messy method so I decided to split them up so we can handle it specifically for each case.
Diffstat (limited to 'activemodel/lib')
-rw-r--r--activemodel/lib/active_model/validator.rb7
1 files changed, 7 insertions, 0 deletions
diff --git a/activemodel/lib/active_model/validator.rb b/activemodel/lib/active_model/validator.rb
index 1d2888a818..21339b628e 100644
--- a/activemodel/lib/active_model/validator.rb
+++ b/activemodel/lib/active_model/validator.rb
@@ -167,6 +167,13 @@ module ActiveModel
def should_validate?(record) # :nodoc:
!record.persisted? || record.changed? || record.marked_for_destruction?
end
+
+ # Always validate the record if the attribute is a virtual attribute.
+ # We have no way of knowing that the record was changed if the attribute
+ # does not exist in the database.
+ def unknown_attribute?(record, attribute) # :nodoc:
+ !record.attributes.include?(attribute.to_s)
+ end
end
# +BlockValidator+ is a special +EachValidator+ which receives a block on initialization