代码之家  ›  专栏  ›  技术社区  ›  mikewilliamson

Rails::Railtie:创建Rails 3 gem时出现问题

  •  4
  • mikewilliamson  · 技术社区  · 15 年前

    我真的需要另一双眼睛来观察这个,所以我想我会把它贴在这里。 如果我跳过Railtie,把它作为initializers文件夹中的传统monkeypatch来做,就可以了。使用铁轨。。。没有什么。

    从表面上看,我的铁轨从来没有被处决,因此似乎没有其他事情发生。

    有人看到这里有什么问题吗?

    项目文件:

    gem 'sql_explain', :path => "/home/mike/projects/sql_explain/"
    

    gemspec公司:

    ...
      spec.files = %w(README.rdoc sql_explain.rb lib/sql_explain.rb lib/railtie.rb sql_explain.gemspec)
    ...
    

    require 'lib/railtie.rb'
    

    铁路.rb

    require 'active_record'
    require 'sql_explain'
    
    module SqlExplain
      class Railtie < Rails::Railtie
        railtie_name :sql_explain
        initializer 'sql_explain.extend.activerecord' do
          if defined?(ActiveRecord)
            ActiveRecord::ConnectionAdapters::MysqlAdapter.include SqlExplain::AR
          end
        end
      end
    end
    

    sql语句_解释.rb

    module SqlExplain
      module AR
        def self.included(base_klass)
          base_klass.send :alias_method_chain, :select, :explain
        end
    
    
        def select_with_explain(sql, name = nil)
          @connection.query_with_result = true
          result = execute('explain ' + sql, :skip_logging)
          rows = []
          result.each_hash { |row| rows << row }
          result.free
          @connection.more_results && @connection.next_result    # invoking stored procedures with CLIENT_MULTI_RESULTS requires this to tidy up else connection will be dropped
          exp_string = ""
          rows.each{|row| row.each_pair{|k,v| exp_string += " #{k}: #{v} |"}}
          log(exp_string, "Explanation") {}
          select_without_explain(sql, name)
        end
      end
    end
    
    2 回复  |  直到 13 年前
        1
  •  3
  •   PreciousBodilyFluids    14 年前

    看起来您已经解决了这个问题,但是请记住,使用Rails 3可以:

    ActiveSupport.on_load :active_record do
      ActiveRecord::ConnectionAdapters::MysqlAdapter.include SqlExplain::AR
    end
    

    这将确保只有在加载ActiveRecord之后才会触发include。

        2
  •  1
  •   gertas    15 年前

    你确定这是真的吗?:

    if defined?(ActiveRecord)
    

    我想这是假的。尝试要求“rails/all”而不是“rails”—第一个不是加载AR。