mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #40395 from marcelolx/fix_read_attribute_before_type_cast_for_aliased_attributes
Fix `read_attribute_before_type_cast` to consider attribute aliases
This commit is contained in:
commit
871073c4a9
5 changed files with 24 additions and 1 deletions
|
@ -1,3 +1,7 @@
|
|||
* Fix `read_attribute_before_type_cast` to consider attribute aliases.
|
||||
|
||||
*Marcelo Lauxen*
|
||||
|
||||
* Support passing record to uniqueness validator `:conditions` callable:
|
||||
|
||||
```ruby
|
||||
|
|
|
@ -46,7 +46,10 @@ module ActiveRecord
|
|||
# task.read_attribute_before_type_cast('completed_on') # => "2012-10-21"
|
||||
# task.read_attribute_before_type_cast(:completed_on) # => "2012-10-21"
|
||||
def read_attribute_before_type_cast(attr_name)
|
||||
attribute_before_type_cast(attr_name.to_s)
|
||||
name = attr_name.to_s
|
||||
name = self.class.attribute_aliases[name] || name
|
||||
|
||||
attribute_before_type_cast(name)
|
||||
end
|
||||
|
||||
# Returns a hash of attributes before typecasting and deserialization.
|
||||
|
|
|
@ -12,6 +12,7 @@ require "models/category"
|
|||
require "models/reply"
|
||||
require "models/contact"
|
||||
require "models/keyboard"
|
||||
require "models/numeric_data"
|
||||
|
||||
class AttributeMethodsTest < ActiveRecord::TestCase
|
||||
include InTimeZone
|
||||
|
@ -1087,6 +1088,11 @@ class AttributeMethodsTest < ActiveRecord::TestCase
|
|||
assert_equal "Topic::GeneratedAttributeMethods", mod.inspect
|
||||
end
|
||||
|
||||
test "read_attribute_before_type_cast with aliased attribute" do
|
||||
model = NumericData.new(new_bank_balance: "abcd")
|
||||
assert_equal "abcd", model.read_attribute_before_type_cast("new_bank_balance")
|
||||
end
|
||||
|
||||
private
|
||||
def new_topic_like_ar_class(&block)
|
||||
klass = Class.new(ActiveRecord::Base) do
|
||||
|
|
|
@ -102,4 +102,12 @@ class NumericalityValidationTest < ActiveRecord::TestCase
|
|||
|
||||
assert_not_predicate subject, :valid?
|
||||
end
|
||||
|
||||
def test_aliased_attribute
|
||||
model_class.validates_numericality_of(:new_bank_balance, greater_or_equal_than: 0)
|
||||
|
||||
subject = model_class.new(new_bank_balance: "abcd")
|
||||
|
||||
assert_not_predicate subject, :valid?
|
||||
end
|
||||
end
|
||||
|
|
|
@ -7,4 +7,6 @@ class NumericData < ActiveRecord::Base
|
|||
attribute :world_population, :big_integer
|
||||
attribute :my_house_population, :big_integer
|
||||
attribute :atoms_in_universe, :big_integer
|
||||
|
||||
alias_attribute :new_bank_balance, :bank_balance
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue