2013-01-08 7 views
5

私はこのエンジンをマウントしようとしているメインアプリケーションにこの機能を追加/オーバーライドするために、エンジンの中に懸念を作り出しようとしています。問題は、エンジンモジュールの問題も含めて問題があることです。 Railsはそれを見つけることができないようです。Railsエンジンで懸念事項を作成するにはどうすればよいですか?

ですapp/models/blorgh/post.rbの私post.rbファイル:

module Blorgh 
    class Post < ActiveRecord::Base 
    include Blorgh::Concerns::Models::Post 
    end 
end 

そして、これはlib/concerns/models/post.rbで私post.rbの関心事である:

は 'active_support /懸念' を必要と

module Concerns::Models::Post 
    extend ActiveSupport::Concern 

    # 'included do' causes the included code to be evaluated in the 
    # conext where it is included (post.rb), rather than be 
    # executed in the module's context (blorgh/concerns/models/post). 
    included do 
    attr_accessible :author_name, :title, :text 
    attr_accessor :author_name 
    belongs_to :author, class_name: Blorgh.user_class 
    has_many :comments 

    before_save :set_author 

    private 
    def set_author 
     self.author = User.find_or_create_by_name(author_name) 
    end 
    end 

    def summary 
    "#{title}" 
    end 

    module ClassMethods 
    def some_class_method 
     'some class method string' 
    end 
    end 
end 

私がテストを実行します/ダミー、私はこのエラーが発生しました:初期化されていない定数Blorgh ::懸念

これは私のblorgh.gemspecです:

$:.push File.expand_path("../lib", __FILE__) 

# Maintain your gem's version: 
require "blorgh/version" 


# Describe your gem and declare its dependencies: 
Gem::Specification.new do |s| 
    s.name  = "blorgh" 
    s.version  = Blorgh::VERSION 
    s.authors  = ["***"] 
    s.email  = ["***"] 
    s.homepage = "***" 
    s.summary  = "Engine test." 
    s.description = "Description of Blorgh." 

    s.files = Dir["{app,config,db,lib}/**/*"] + ["MIT-LICENSE", "Rakefile", "README.rdoc"] 
    s.test_files = Dir["test/**/*"] 

    s.add_dependency "rails", "~> 3.2.8" 
    s.add_dependency "jquery-rails" 

    s.add_development_dependency "sqlite3" 
end 

誰かがこれで私を助けることができますか?

答えて

0

これは、Rails 3ではlibディレクトリがクラスを見つけるために自動的に検索されないために起こります。 config.autoload_pathsを更新して、libディレクトリをエンジンに追加するか、libディレクトリからapps/modelsに自動的に見つかるようにconcern/models/post.rbを移動することができます。

2

エンジンを使用する場合は、特にメインアプリの動作を変更するときに、読み込み順序を把握する必要があります。あなたのエンジンが "engine_name"と呼ばれているとします。このファイルはengine_name/lib/engine_name/engine.rbです。これはあなたの懸念を含めるための1つの場所です。

Bundler.require 

module EngineName 
    class Engine < ::Rails::Engine 
    require 'engine_name/path/to/concerns/models/post' 

    initializer 'engine_name.include_concerns' do 
     ActionDispatch::Reloader.to_prepare do 
     Blorgh::Post.send(:include, Concerns::Models::Post) 
     end 
    end 

    # Autoload from lib directory 
    config.autoload_paths << File.expand_path('../../', __FILE__) 

    isolate_namespace EngineName 
    end 
end 

あなたはすべてが無事ロードされていることを確認しますが、懸念を使用して非常に注意すると、おそらく異なる構成に対処するためにBlorgh ::ポストをリファクタリングにより依存性の注入を使用して再考このように。

関連する問題