1
0
Fork 0
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:
Eugene Kenny 2020-10-19 01:06:18 +01:00 committed by GitHub
commit 871073c4a9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 24 additions and 1 deletions

View file

@ -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

View file

@ -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.

View file

@ -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

View file

@ -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

View file

@ -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