aboutsummaryrefslogtreecommitdiffstats
path: root/lib/arel/table.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 /lib/arel/table.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 'lib/arel/table.rb')
-rw-r--r--lib/arel/table.rb21
1 files changed, 17 insertions, 4 deletions
diff --git a/lib/arel/table.rb b/lib/arel/table.rb
index 2c7a2b7f93..b4b4a861b8 100644
--- a/lib/arel/table.rb
+++ b/lib/arel/table.rb
@@ -11,18 +11,19 @@ module Arel
# TableAlias and Table both have a #table_name which is the name of the underlying table
alias :table_name :name
- def initialize name, options = {}
+ def initialize(name, as: nil, type_caster: nil)
@name = name.to_s
@columns = nil
@aliases = []
+ @type_caster = type_caster
# Sometime AR sends an :as parameter to table, to let the table know
# that it is an Alias. We may want to override new, and return a
# TableAlias node?
- @table_alias = options[:as]
- if @table_alias.to_s == @name
- @table_alias = nil
+ if as.to_s == @name
+ as = nil
end
+ @table_alias = as
end
def alias name = "#{self.name}_2"
@@ -98,6 +99,18 @@ module Arel
end
alias :== :eql?
+ def type_cast_for_database(attribute_name, value)
+ type_caster.type_cast_for_database(attribute_name, value)
+ end
+
+ def able_to_type_cast?
+ !type_caster.nil?
+ end
+
+ protected
+
+ attr_reader :type_caster
+
private
def attributes_for columns