Merge pull request #15204 from sgrif/sg-delegate-predicates

Delegate predicate methods to injected type object on Column
This commit is contained in:
Rafael Mendonça França 2014-05-20 18:02:49 -03:00
commit c4640ca805
12 changed files with 44 additions and 24 deletions

@ -18,7 +18,7 @@ module Format
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 @@ def initialize(name, default, cast_type, sql_type = nil, null = true)
@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 @@ def klass
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.

@ -21,14 +21,6 @@ def initialize(name, default, oid_type, sql_type = nil, null = true)
@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

@ -14,6 +14,10 @@ class SpecializedString < Type::String
def initialize(type)
@type = type
end
def text?
false
end
end
class Bit < Type::String

@ -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'

@ -5,6 +5,10 @@ class Binary < Value # :nodoc:
def type
:binary
end
def binary?
true
end
end
end
end

@ -2,6 +2,8 @@ module ActiveRecord
module ConnectionAdapters
module Type
class Decimal < Value # :nodoc:
include Numeric
def type
:decimal
end

@ -2,6 +2,8 @@ module ActiveRecord
module ConnectionAdapters
module Type
class Float < Value # :nodoc:
include Numeric
def type
:float
end

@ -2,6 +2,8 @@ module ActiveRecord
module ConnectionAdapters
module Type
class Integer < Value # :nodoc:
include Numeric
def type
:integer
end

@ -0,0 +1,11 @@
module ActiveRecord
module ConnectionAdapters
module Type
module Numeric # :nodoc:
def number?
true
end
end
end
end
end

@ -6,6 +6,10 @@ def type
:string
end
def text?
true
end
private
def cast_value(value)

@ -8,6 +8,18 @@ def type_cast(value)
cast_value(value) unless value.nil?
end
def text?
false
end
def number?
false
end
def binary?
false
end
private
def cast_value(value)

@ -83,7 +83,7 @@ def ensure_warning_is_issued
class PostgresqlCompositeWithCustomOIDTest < ActiveRecord::TestCase
include PostgresqlCompositeBehavior
class FullAddressType
class FullAddressType < ActiveRecord::ConnectionAdapters::Type::Value
def type; :full_address end
def type_cast(value)