简体   繁体   English

Rails :: Railtie:无法创建Rails 3 gem

[英]Rails::Railtie: Trouble creating a Rails 3 gem

I could really use another set of eyes on this so I thought I would post it here. 我真的可以用另一套眼睛,所以我想我会把它贴在这里。 A while ago I wrote a basic ActiveRecord Extension for my own educational purposes. 不久之前,我为自己的教育目的写了一个基本的ActiveRecord扩展。 I've been reading about Railties lately and thought I would try to get it working with Rails 3. I thought I would package it up as a gem to get a sense of that process as well. 我最近一直在阅读有关Railties的文章,并且我认为我会尝试使用Rails 3.我想我会把它打包成一块宝石来了解这个过程。 If I skip the Railtie and just do this as a traditional monkeypatch in the initializers folder it works fine. 如果我跳过Railtie并在初始化文件夹中将其作为传统的monkeypatch执行,它可以正常工作。 Using a Railtie... nothing. 使用铁路......没什么。

From the looks of it my Railtie is never executed and therefore nothing else seems to be happening. 从它的外观来看,我的Railtie永远不会被执行,因此似乎没有其他任何事情发生。

Does anyone see anything wrong here? 这里有人看错吗?

Any suggestions for best practices or improvements are also welcome. 我们也欢迎任何有关最佳实践或改进的建议。

project Gemfile: 项目Gemfile:

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

gemspec: gemspec:

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

sql_explain.rb sql_explain.rb

require 'lib/railtie.rb'

railtie.rb railtie.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_explain.rb sql_explain.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

Looks like you've already got this sorted out, but remember that with Rails 3 you can do: 看起来你已经解决了这个问题,但请记住,使用Rails 3,你可以做到:

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

That'll ensure that your include will only be fired once ActiveRecord has been loaded. 这将确保只有在加载ActiveRecord后才会触发您的include。

Are you sure this is true?: 你确定这是真的吗?:

if defined?(ActiveRecord)

I suppose it is false. 我想这是假的。 Instead of "rails" try to require "rails/all" - the first one is not loading AR. 而不是“rails”尝试要求“rails / all” - 第一个不加载AR。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM