2011-07-10 34 views
0

我正在開發Rails引擎,將其打包爲gem。在我的引擎的主要模塊文件,我有:無法從Rails引擎添加的ActionController方法內部訪問模型

module Auditor 
    require 'engine' if defined?(Rails) && Rails::VERSION::MAJOR == 3 
    require 'application_controller' 
end 

module ActionController 
    module Auditor 
    def self.included(base) 
     base.extend(ClassMethods) 
    end 

    module ClassMethods 
     def is_audited 
     include ActionController::Auditor::InstanceMethods 
     before_filter :audit_request 
     end 
    end 

    module InstanceMethods 
     def audit_request 
     a = AuditorLog.new 
     a.save! 
     end 
    end 
    end 
end 

ActionController::Base.send(:include, ActionController::Auditor) 

其中AuditorLog也由發動機提供的模型。 (我的意圖是在應用程序中使用此引擎將「is_audited」添加到控制器中,這將引起審覈記錄請求的詳細信息。)

我遇到的問題是,當從代碼中調用此代碼在使用引擎的地方,AuditorLog模型不可訪問。它看起來像Ruby認爲它應該在的ActionController類:

NameError(未初始化不斷 的ActionController ::審計:: InstanceMethods :: AuditorLog)

,而不是從我的發動機模型。

任何人都可以指向正確的方向嗎?這是我第一次創建引擎並試圖將其打包成寶石;我搜索了這個例子,並沒有太多的運氣。我將這個功能添加到ActionController類的方法是基於mobile_fu所做的,所以請讓我知道如果我對這一切都做錯了。

+0

'AuditorLog'定義在哪裏?當你執行':: AuditorLog'時它有效嗎? – apneadiving

回答

1

使用:: AuditorLog來訪問ActiveRecord類(除非你在模塊或命名空間中,在這種情況下你需要包含模塊名稱)。

+0

完美,那很簡單。謝謝傑里米! –

+0

我覺得有點溺愛:) – apneadiving