From 50869f37d379a09a69b21399ae4360d7c13017c1 Mon Sep 17 00:00:00 2001 From: Sean Griffin Date: Mon, 19 May 2014 17:19:16 -0700 Subject: Delegate predicate methods to injected type object on Column --- .../lib/active_record/connection_adapters/column.rb | 16 +--------------- .../connection_adapters/postgresql/column.rb | 8 -------- .../active_record/connection_adapters/postgresql/oid.rb | 4 ++++ .../lib/active_record/connection_adapters/type.rb | 1 + .../lib/active_record/connection_adapters/type/binary.rb | 4 ++++ .../active_record/connection_adapters/type/decimal.rb | 2 ++ .../lib/active_record/connection_adapters/type/float.rb | 2 ++ .../active_record/connection_adapters/type/integer.rb | 2 ++ .../active_record/connection_adapters/type/numeric.rb | 11 +++++++++++ .../lib/active_record/connection_adapters/type/string.rb | 4 ++++ .../lib/active_record/connection_adapters/type/value.rb | 12 ++++++++++++ .../test/cases/adapters/postgresql/composite_test.rb | 2 +- 12 files changed, 44 insertions(+), 24 deletions(-) create mode 100644 activerecord/lib/active_record/connection_adapters/type/numeric.rb diff --git a/activerecord/lib/active_record/connection_adapters/column.rb b/activerecord/lib/active_record/connection_adapters/column.rb index f46f9af239..0f0aa9110e 100644 --- a/activerecord/lib/active_record/connection_adapters/column.rb +++ b/activerecord/lib/active_record/connection_adapters/column.rb @@ -18,7 +18,7 @@ module ActiveRecord alias :encoded? :coder - delegate :type, to: :cast_type + delegate :type, :text?, :number?, :binary?, to: :cast_type # Instantiates a new column in the table. # @@ -43,16 +43,6 @@ module ActiveRecord @coder = nil end - # Returns +true+ if the column is either of type string or text. - def text? - type == :string || type == :text - end - - # Returns +true+ if the column is either of type integer, float or decimal. - def number? - type == :integer || type == :float || type == :decimal - end - def has_default? !default.nil? end @@ -70,10 +60,6 @@ module ActiveRecord end end - def binary? - type == :binary - end - # Casts a Ruby value to something appropriate for writing to the database. # Numeric columns will typecast boolean and string to appropriate numeric # values. diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/column.rb b/activerecord/lib/active_record/connection_adapters/postgresql/column.rb index e5118b5427..aba721eece 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/column.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/column.rb @@ -21,14 +21,6 @@ module ActiveRecord @default_function = default if has_default_function?(default_value, default) end - def number? - !array && super - end - - def text? - !array && super - end - # :stopdoc: class << self include PostgreSQL::Cast diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/oid.rb b/activerecord/lib/active_record/connection_adapters/postgresql/oid.rb index 94afc3aec5..56b856acf6 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/oid.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/oid.rb @@ -14,6 +14,10 @@ module ActiveRecord def initialize(type) @type = type end + + def text? + false + end end class Bit < Type::String diff --git a/activerecord/lib/active_record/connection_adapters/type.rb b/activerecord/lib/active_record/connection_adapters/type.rb index f215266560..763176cb2b 100644 --- a/activerecord/lib/active_record/connection_adapters/type.rb +++ b/activerecord/lib/active_record/connection_adapters/type.rb @@ -1,3 +1,4 @@ +require 'active_record/connection_adapters/type/numeric' require 'active_record/connection_adapters/type/time_value' require 'active_record/connection_adapters/type/value' diff --git a/activerecord/lib/active_record/connection_adapters/type/binary.rb b/activerecord/lib/active_record/connection_adapters/type/binary.rb index 168d824d3d..e0ea226216 100644 --- a/activerecord/lib/active_record/connection_adapters/type/binary.rb +++ b/activerecord/lib/active_record/connection_adapters/type/binary.rb @@ -5,6 +5,10 @@ module ActiveRecord def type :binary end + + def binary? + true + end end end end diff --git a/activerecord/lib/active_record/connection_adapters/type/decimal.rb b/activerecord/lib/active_record/connection_adapters/type/decimal.rb index 9581cd5964..91957fe84b 100644 --- a/activerecord/lib/active_record/connection_adapters/type/decimal.rb +++ b/activerecord/lib/active_record/connection_adapters/type/decimal.rb @@ -2,6 +2,8 @@ module ActiveRecord module ConnectionAdapters module Type class Decimal < Value # :nodoc: + include Numeric + def type :decimal end diff --git a/activerecord/lib/active_record/connection_adapters/type/float.rb b/activerecord/lib/active_record/connection_adapters/type/float.rb index 2d436d4aa3..31d8e7d1d3 100644 --- a/activerecord/lib/active_record/connection_adapters/type/float.rb +++ b/activerecord/lib/active_record/connection_adapters/type/float.rb @@ -2,6 +2,8 @@ module ActiveRecord module ConnectionAdapters module Type class Float < Value # :nodoc: + include Numeric + def type :float end diff --git a/activerecord/lib/active_record/connection_adapters/type/integer.rb b/activerecord/lib/active_record/connection_adapters/type/integer.rb index b839e043ad..fbc72ae4ef 100644 --- a/activerecord/lib/active_record/connection_adapters/type/integer.rb +++ b/activerecord/lib/active_record/connection_adapters/type/integer.rb @@ -2,6 +2,8 @@ module ActiveRecord module ConnectionAdapters module Type class Integer < Value # :nodoc: + include Numeric + def type :integer end diff --git a/activerecord/lib/active_record/connection_adapters/type/numeric.rb b/activerecord/lib/active_record/connection_adapters/type/numeric.rb new file mode 100644 index 0000000000..a440de4d14 --- /dev/null +++ b/activerecord/lib/active_record/connection_adapters/type/numeric.rb @@ -0,0 +1,11 @@ +module ActiveRecord + module ConnectionAdapters + module Type + module Numeric # :nodoc: + def number? + true + end + end + end + end +end diff --git a/activerecord/lib/active_record/connection_adapters/type/string.rb b/activerecord/lib/active_record/connection_adapters/type/string.rb index d6c1c64834..24f9659c7b 100644 --- a/activerecord/lib/active_record/connection_adapters/type/string.rb +++ b/activerecord/lib/active_record/connection_adapters/type/string.rb @@ -6,6 +6,10 @@ module ActiveRecord :string end + def text? + true + end + private def cast_value(value) diff --git a/activerecord/lib/active_record/connection_adapters/type/value.rb b/activerecord/lib/active_record/connection_adapters/type/value.rb index a83f0e652d..506f402fef 100644 --- a/activerecord/lib/active_record/connection_adapters/type/value.rb +++ b/activerecord/lib/active_record/connection_adapters/type/value.rb @@ -8,6 +8,18 @@ module ActiveRecord cast_value(value) unless value.nil? end + def text? + false + end + + def number? + false + end + + def binary? + false + end + private def cast_value(value) diff --git a/activerecord/test/cases/adapters/postgresql/composite_test.rb b/activerecord/test/cases/adapters/postgresql/composite_test.rb index 1e7071c136..f01717d1a7 100644 --- a/activerecord/test/cases/adapters/postgresql/composite_test.rb +++ b/activerecord/test/cases/adapters/postgresql/composite_test.rb @@ -83,7 +83,7 @@ end class PostgresqlCompositeWithCustomOIDTest < ActiveRecord::TestCase include PostgresqlCompositeBehavior - class FullAddressType + class FullAddressType < ActiveRecord::ConnectionAdapters::Type::Value def type; :full_address end def type_cast(value) -- cgit v1.2.3