2014-04-19 5 views
0

Я хочу переопределить метод type_cast (значение, столбец) от C: /RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-4.0.2/lib/ active_record/connection_adapters/аннотация/quoting.rbМетод переопределения из модуля Quoting

module ActiveRecord 
    module ConnectionAdapters # :nodoc: 
    module Quoting 

    # Cast a +value+ to a type that the database understands. For example, 
    # SQLite does not understand dates, so this method will convert a Date 
    # to a String. 
    def type_cast(value, column) 
    return value.id if value.respond_to?(:quoted_id) 

    case value 
    when String, ActiveSupport::Multibyte::Chars 
     value = value.to_s 
     return value unless column 

     case column.type 
     when :binary then value 
     when :integer then value.to_i 
     when :float then value.to_f 
     else 
     value 
     end 

    when true, false 
     if column && column.type == :integer 
     value ? 1 : 0 
     else 
     value ? 't' : 'f' 
     end 
     # BigDecimals need to be put in a non-normalized form and quoted. 
    when nil  then nil 
    when BigDecimal then value.to_s('F') 
    when Numeric then value 
    when Date, Time then quoted_date(value) 
    when Symbol  then value.to_s 
    else 
     to_type = column ? " to #{column.type}" : "" 
     raise TypeError, "can't cast #{value.class}#{to_type}" 
    end 
    end 
    end 
    end 
end 

Проблема в строках

when true, false 
    if column && column.type == :integer 
    value ? 1 : 0 
    else 
    value ? 't' : 'f' 

Я хочу, чтобы получить

when true, false 
    value ? 1 : 0 

В C: /RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-4.0.2/lib/ active_record/connection_adapters/abstract_adapter.rb класса AbstractAdapter включает Цитирование.

module ActiveRecord 
    module ConnectionAdapters # :nodoc: 
    extend ActiveSupport::Autoload 

    ... 

    class AbstractAdapter 
     include Quoting 

    ... 
    end 
    end 
end 

И класс SQLite3Adapter унаследуют AbstractAdapter: (C: /RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-4.0.2/lib/ active_record/connection_adapters/sqlite3_adapter .rb)

class SQLite3Adapter < AbstractAdapter 
    def type_cast(value, column) # :nodoc: 
    return value.to_f if BigDecimal === value 
    return super unless String === value 
    return super unless column && value 

    value = super 
    if column.type == :string && value.encoding == Encoding::ASCII_8BIT 
     logger.error "Binary data inserted for `string` type on column `#{column.name}`" if logger 
     value = value.encode Encoding::UTF_8 
    end 
    value 
    end 
end 

Как переопределить метод type_cast (значение, столбец)? Я пробую что-то вроде этого

# config/initializers/sqlite3_adapter_extension.rb 

module ActiveRecord 
    module ConnectionAdapters 
    class SQLite3Adapter < AbstractAdapter 

     def type_cast(value, column) # :nodoc: 
     case value 
     when true, false 
      value ? 1 : 0 
     end 
     end 
    end 
    end 
end 

Но оно предсказуемо не работает.

ответ

0

Я не уверен, что вы делаете в точности, но если вы хотите сделать это для конкретного модуля, вы можете переопределить этот метод внутри самого класса модели, но если вы уверены, что хотите переопределить его для всех своих вам просто нужно создать новый .rb-файл в вашем config/initializers и добавить код, подобный этому

module ActiveRecord 
    module ConnectionAdapters # :nodoc: 
    module Quoting 
     def type_cast(value, column) # :nodoc: 
     case value 
     when true, false 
      value ? 1 : 0 
     end 
     end 
    end 
    end 
end 
Смежные вопросы