来自Quoting模块的覆盖方法

时间:2014-04-19 20:39:44

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4

我希望从C:/RailsInstaller / Ruby1.9.3 / lib / ruby​​ / gems / 1.9.1 / gems / activerecord-4.0.2 / lib /中覆盖方法 type_cast(value,column) active_record / connection_adapters /抽象/ quoting.rb

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(value,column)?我尝试这样的事情

# 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

但它可以预测不起作用。

1 个答案:

答案 0 :(得分:0)

我不确定你到底在做什么,但是如果你想为特定的模型做这个,你可以在模型类本身内覆盖这个方法,但如果你确定要覆盖所有您的应用程序只需要在config / initializers中创建一个新的.rb文件,并添加如下代码

 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