mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #35320 from kamille-gz/fix_query_method_when_given_Date_data_type
Fix ActiveRecord query attribute method when given value does't respond to to_i method
This commit is contained in:
commit
260a3c6512
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
|
||||
|
|
|
@ -458,6 +458,69 @@ 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 "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