ruby on rails-覆盖Quoting模块中的方法



我想要覆盖C:\RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/rb.rb中的方法type_cast(值,列)

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

但可预测的是行不通的。

我不确定你到底在做什么,但如果你想为特定的模块做这件事,你可以在模型类本身中覆盖这个方法,但如果确定你想为所有应用程序覆盖它,你只需要在你的配置/初始化器中创建一个新的.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

相关内容

  • 没有找到相关文章

最新更新