2011-07-27 3 views
1

"역할"형식을 사용하여 부모 응용 프로그램의 사용자 모델과 관계를 설정하는 레일 엔진을 작성하고 있습니다. 개발자가 :auth_with 옵션을 사용하여 도우미 메서드 이름을 지정할 수 있도록하기위한레일즈 3.1 엔진에서 부모 앱의 도우미 메서드를 호출하는 방법

module Cornerstone 

    module ActsAsCornerstoneUser 

    extend ActiveSupport::Concern 

    module ClassMethods 

     def acts_as_cornerstone_user(options = {}) 

     #= Associations 
     has_many :cornerstone_discussions 


     #= Options 
     Cornerstone::Config.auth_with << options[:auth_with] if options[:auth_with] 
     Cornerstone::Config.auth_with.flatten! 

     end 
    end 

    module InstanceMethods 

    end 

    end 

    ActiveRecord::Base.send :include, ActsAsCornerstoneUser 

end 

내가 좋아하는 것입니다. 아이디어는 개발자가 부모 응용 프로그램에서 해당 세션에 대해 로그인 한 사용자를 반환하는 도우미 메서드를 지정한다는 것입니다.

내 질문은 개발자가 auth_with 옵션을 지정한 후에는 어떻게 부모 응용 프로그램의 메서드를 호출 할 수 있습니까 ??

상위 응용 프로그램의 로그인 된 사용자를 얻는 더 좋은 방법이 있습니까? 단순히 current_user을 호출하는 것에 의존하지 않도록 가능한 한 유연하게하고 싶습니다. 이 같은

+0

하나 이상의 초석 사용자가 필요합니까? (즉, 하나의 인증 방법 또는 클래스 당 하나) –

+0

또한 인증은 컨트롤러의 작업이어야합니다. 인증 된 사용자는 어디에서 필요합니까? –

+0

인증 된 사용자는 엔진 자체 컨트롤러 내에서 필요하지만 상위 응용 프로그램에서 인증이 발생합니다. – astjohn

답변

2

뭔가는 당신이 응용 프로그램에 정의 된 하나의 초석 사용자가 같이 작동합니다 : (. 즉 app/helpers/cornerstone_helper.rb에)

module Cornerstone 
    module ActsAsCornerstoneUser 
    extend ActiveSupport::Concern 

    module ClassMethods 
     def acts_as_cornerstone_user(options = {}) 

     #= Associations 
     has_many :cornerstone_discussions 

     #= Options 
     Cornerstone::Config.auth_with = options[:auth_with] if options[:auth_with] 
     end 
    end 

    module InstanceMethods 

    end 

    def self.included(base) 
     base.extend(ClassMethods) 
     base.include(InstanceMethods) 
    end 
    end 

    ActiveRecord::Base.send :include, ActsAsCornerstoneUser 
end 

그런 다음 당신의 보석에 도우미를 정의를 :

module Cornerstone 
    module CornerStoneHelper 
    def current_cornerstone_user 
     Config.auth_with.call(controller) 
    end 
    end 
end 

acts_as_cornerstone 방법은 다음과 같이 사용된다 :

class MyUser < ActiveRecord::Base 
    acts_as_cornerstone_user :auth_with => Proc.new { |controller| controller.current_user } 
end 
,

그러면 current_cornerstone_user 도우미를 사용하여 현재 인증 된 사용자를 확보 할 수 있습니다.

이 방법은 acts_as_cornerstone_user이 여러 클래스에서 사용될 때 중단됩니다. 하지만 애플리케이션 모델에 대해 알지 못해도 여러 초석 사용자가 있다는 문제가 있습니다 (보석에 있어야합니다).

당신이 :auth_with => :warden 같은 구문을하려는 경우, 당신은 다음과 도우미를 대체 할 수

업데이트 :

module Cornerstone 
    class Config 
    AUTH_MODES = { 
     :warden => Proc.new { |controller| controller.env['warden'].user }, 
     :devise => Proc.new { |controller| controller.current_user } 
    } 
    end 
end 
: Cornerstone::Config::AUTH_MODES

module Cornerstone 
    module CornerStoneHelper 
    def current_cornerstone_user 
     if Config.auth_with.respond_to?(:call) 
     Config.auth_with.call(controller) 
     elsif Config::AUTH_MODES.keys.include?(Config.auth_with) 
     Config::AUTH_MODES[Config.auth_with].call(controller) 
     end 
    end 
    end 
end 

는 다음과 같이 설정

+0

고마워요! 이것을 시도해 볼 것이지만 여러 사용자 클래스가있는 경우 어떻게됩니까? 예를 들어, User 클래스 나 그와 비슷한 것을 상속 한 Admin 클래스는 ... 가능한 한 엔진을 유연하게하려고합니다. 부모 응용 프로그램 내에 정의 된 current_user 메서드를 호출하는 것이 가장 좋은 방법입니다. 이상적으로, 위에서 설명한 내용을 사용하고 싶습니다. auth_with => : 관리자. 소장 사건은 훨씬 쉽습니다. – astjohn

+0

내 대답이 업데이트되었습니다.이 점을 염두에 두셨습니까? –

+0

방금 ​​해결책을 시험해 볼 시간을 찾았습니다. 불행히도 모델에서 컨트롤러 개체에 액세스 할 수 없기 때문에 모델 내에서 Proc를 지정하는 것이 효과적이라고 생각하지 않습니다. 자세히보고 싶으면 소스가 github에 있습니다. https : // github.com/astjohn/cornerstone – astjohn