aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/test/models
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2018-08-19 14:57:05 +0900
committerRyuta Kamizono <kamipo@gmail.com>2018-08-24 00:44:02 +0900
commit47a6d788ddbab08b2a04c72cd80352aac44090ab (patch)
treea3dbe86af22b7357be32a33a670e408ef1dcad57 /activemodel/test/models
parenta0b57bbb21ce81071220bd8c6cfd8cdda342c6c6 (diff)
downloadrails-47a6d788ddbab08b2a04c72cd80352aac44090ab.tar.gz
rails-47a6d788ddbab08b2a04c72cd80352aac44090ab.tar.bz2
rails-47a6d788ddbab08b2a04c72cd80352aac44090ab.zip
Fix numericality validator to still use value before type cast except Active Record
The purpose of fe9547b is to work type casting to value from database. But that was caused not to use the value before type cast even except Active Record. There we never guarantees that the value before type cast was going to the used in this validation, but we should not change the behavior unless there is some particular reason. To restore original behavior, still use the value before type cast if `came_from_user?` is undefined (i.e. except Active Record). Fixes #33651. Fixes #33686.
Diffstat (limited to 'activemodel/test/models')
-rw-r--r--activemodel/test/models/topic.rb14
1 files changed, 14 insertions, 0 deletions
diff --git a/activemodel/test/models/topic.rb b/activemodel/test/models/topic.rb
index b0af00ee45..db3284f833 100644
--- a/activemodel/test/models/topic.rb
+++ b/activemodel/test/models/topic.rb
@@ -3,6 +3,11 @@
class Topic
include ActiveModel::Validations
include ActiveModel::Validations::Callbacks
+ include ActiveModel::AttributeMethods
+ include ActiveSupport::NumberHelper
+
+ attribute_method_suffix "_before_type_cast"
+ define_attribute_method :price
def self._validates_default_keys
super | [ :message ]
@@ -10,6 +15,7 @@ class Topic
attr_accessor :title, :author_name, :content, :approved, :created_at
attr_accessor :after_validation_performed
+ attr_writer :price
after_validation :perform_after_validation
@@ -38,4 +44,12 @@ class Topic
def my_validation_with_arg(attr)
errors.add attr, "is missing" unless send(attr)
end
+
+ def price
+ number_to_currency @price
+ end
+
+ def attribute_before_type_cast(attr)
+ instance_variable_get(:"@#{attr}")
+ end
end