aboutsummaryrefslogtreecommitdiffstats
path: root/test/attributes/test_attribute.rb
diff options
context:
space:
mode:
authorSean Griffin <sean@thoughtbot.com>2014-12-29 11:24:44 -0700
committerSean Griffin <sean@thoughtbot.com>2014-12-29 11:27:32 -0700
commit6160bfbda1d1781c3b08a33ec4955f170e95be11 (patch)
tree82f04cf6f61d64e3950acaef60b937bf7432c371 /test/attributes/test_attribute.rb
parent008445d6fd5f825d9b445ac75a7be67f0f7ab52c (diff)
downloadrails-6160bfbda1d1781c3b08a33ec4955f170e95be11.tar.gz
rails-6160bfbda1d1781c3b08a33ec4955f170e95be11.tar.bz2
rails-6160bfbda1d1781c3b08a33ec4955f170e95be11.zip
Allow a type caster to be given to the `Arel::Table` object
This will allow most consuming code to avoid the deprecation introduced in 008445d6fd5f825d9b445ac75a7be67f0f7ab52c. The only code which will be affected is code that is building the `Arel::Table` object manually, rather than calling `arel_table` on an Active Record class. Hopefully this case will be rare enough that we don't need to introduce any additional APIs to work around it.
Diffstat (limited to 'test/attributes/test_attribute.rb')
-rw-r--r--test/attributes/test_attribute.rb34
1 files changed, 34 insertions, 0 deletions
diff --git a/test/attributes/test_attribute.rb b/test/attributes/test_attribute.rb
index e4ddb27e72..0f6d6e447b 100644
--- a/test/attributes/test_attribute.rb
+++ b/test/attributes/test_attribute.rb
@@ -950,5 +950,39 @@ module Arel
end
end
end
+
+ describe 'type casting' do
+ it 'does not type cast by default' do
+ table = Table.new(:foo)
+ condition = table["id"].eq("1")
+
+ refute table.able_to_type_cast?
+ condition.to_sql.must_equal %("foo"."id" = '1')
+ end
+
+ it 'type casts when given an explicit caster' do
+ fake_caster = Object.new
+ def fake_caster.type_cast_for_database(attr_name, value)
+ if attr_name == "id"
+ value.to_i
+ else
+ value
+ end
+ end
+ table = Table.new(:foo, type_caster: fake_caster)
+ condition = table["id"].eq("1").and(table["other_id"].eq("2"))
+
+ assert table.able_to_type_cast?
+ condition.to_sql.must_equal %("foo"."id" = 1 AND "foo"."other_id" = '2')
+ end
+
+ it 'falls back to using the connection adapter for type casting' do
+ table = Table.new(:users)
+ condition = table["id"].eq("1")
+
+ refute table.able_to_type_cast?
+ condition.to_sql.must_equal %("users"."id" = 1)
+ end
+ end
end
end