1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activemodel/test/models/topic.rb
Ryuta Kamizono 47a6d788dd 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.
2018-08-24 00:44:02 +09:00

55 lines
1.1 KiB
Ruby

# frozen_string_literal: true
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 ]
end
attr_accessor :title, :author_name, :content, :approved, :created_at
attr_accessor :after_validation_performed
attr_writer :price
after_validation :perform_after_validation
def initialize(attributes = {})
attributes.each do |key, value|
send "#{key}=", value
end
end
def condition_is_true
true
end
def condition_is_false
false
end
def perform_after_validation
self.after_validation_performed = true
end
def my_validation
errors.add :title, "is missing" unless title
end
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