mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Fix query attribute method on user-defined attribute to be aware of typecasted value
change the line to check an attribute has user-defined type ref: https://github.com/rails/rails/pull/35320#discussion_r257924552 check query attribute method is working when given value does not respond to to_i method
This commit is contained in:
parent
b75d6ea5d3
commit
81bc621ed0
3 changed files with 78 additions and 2 deletions
|
@ -1,3 +1,17 @@
|
|||
* Fix query attribute method on user-defined attribute to be aware of typecasted value.
|
||||
|
||||
For example the following code no longer return false as casted non-empty string.
|
||||
|
||||
```
|
||||
class Post < ActiveRecord::Base
|
||||
attribute :user_defined_text, :text
|
||||
end
|
||||
|
||||
Post.new(user_defined_text: "false").user_defined_text? # => true
|
||||
```
|
||||
|
||||
*Yuji Kamijima*
|
||||
|
||||
* Quote empty ranges like other empty enumerables.
|
||||
|
||||
*Patrick Rebsch*
|
||||
|
|
|
@ -16,8 +16,7 @@ module ActiveRecord
|
|||
when true then true
|
||||
when false, nil then false
|
||||
else
|
||||
column = self.class.columns_hash[attr_name]
|
||||
if column.nil?
|
||||
if !type_for_attribute(attr_name) { false }
|
||||
if Numeric === value || value !~ /[^0-9]/
|
||||
!value.to_i.zero?
|
||||
else
|
||||
|
|
|
@ -439,6 +439,22 @@ class AttributeMethodsTest < ActiveRecord::TestCase
|
|||
end
|
||||
end
|
||||
|
||||
test "user defined text attribute predicate" do
|
||||
klass = Class.new(ActiveRecord::Base) do
|
||||
self.table_name = Topic.table_name
|
||||
|
||||
attribute :user_defined_text, :text
|
||||
end
|
||||
|
||||
topic = klass.new(user_defined_text: "text")
|
||||
assert_predicate topic, :user_defined_text?
|
||||
|
||||
ActiveModel::Type::Boolean::FALSE_VALUES.each do |value|
|
||||
topic = klass.new(user_defined_text: value)
|
||||
assert_predicate topic, :user_defined_text?
|
||||
end
|
||||
end
|
||||
|
||||
test "number attribute predicate" do
|
||||
[nil, 0, "0"].each do |value|
|
||||
assert_equal false, Developer.new(salary: value).salary?
|
||||
|
@ -458,6 +474,53 @@ class AttributeMethodsTest < ActiveRecord::TestCase
|
|||
end
|
||||
end
|
||||
|
||||
test "user defined date attribute predicate" do
|
||||
klass = Class.new(ActiveRecord::Base) do
|
||||
self.table_name = Topic.table_name
|
||||
|
||||
attribute :user_defined_date, :date
|
||||
end
|
||||
|
||||
topic = klass.new(user_defined_date: Date.current)
|
||||
assert_predicate topic, :user_defined_date?
|
||||
end
|
||||
|
||||
test "user defined datetime attribute predicate" do
|
||||
klass = Class.new(ActiveRecord::Base) do
|
||||
self.table_name = Topic.table_name
|
||||
|
||||
attribute :user_defined_datetime, :datetime
|
||||
end
|
||||
|
||||
topic = klass.new(user_defined_datetime: Time.current)
|
||||
assert_predicate topic, :user_defined_datetime?
|
||||
end
|
||||
|
||||
test "user defined time attribute predicate" do
|
||||
klass = Class.new(ActiveRecord::Base) do
|
||||
self.table_name = Topic.table_name
|
||||
|
||||
attribute :user_defined_time, :time
|
||||
end
|
||||
|
||||
topic = klass.new(user_defined_time: Time.current)
|
||||
assert_predicate topic, :user_defined_time?
|
||||
end
|
||||
|
||||
test "user defined json attribute predicate" do
|
||||
klass = Class.new(ActiveRecord::Base) do
|
||||
self.table_name = Topic.table_name
|
||||
|
||||
attribute :user_defined_json, :json
|
||||
end
|
||||
|
||||
topic = klass.new(user_defined_json: { key: "value" })
|
||||
assert_predicate topic, :user_defined_json?
|
||||
|
||||
topic = klass.new(user_defined_json: {})
|
||||
assert_not_predicate topic, :user_defined_json?
|
||||
end
|
||||
|
||||
test "custom field attribute predicate" do
|
||||
object = Company.find_by_sql(<<~SQL).first
|
||||
SELECT c1.*, c2.type as string_value, c2.rating as int_value
|
||||
|
|
Loading…
Reference in a new issue