如何从 rails 3.1 引擎调用父应用程序的辅助方法 [英] How to call a parent app's helper method from a rails 3.1 engine

查看:34
本文介绍了如何从 rails 3.1 引擎调用父应用程序的辅助方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个 Rails 引擎,它使用充当"格式来建立与父应用程序的用户模型的关系.

I'm building a rails engine that uses the "acts as" format to establish relationships with the parent application's User model.

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 选项指定辅助方法名称.这个想法是,开发人员将在父应用程序中指定一个帮助程序方法,该方法将返回该会话的登录用户.

I would like for a developer to be able to specify a helper method name using the :auth_with option. The idea is that the developer will specify a helper method in the parent application that will return the signed in user for that session.

我的问题是,一旦开发人员指定了 auth_with 选项,我该如何调用父应用程序的方法??

My question is once the developer has specified the auth_with option, how can I call that parent application's method??

是否有更好的方法来获取父应用程序的登录用户?我希望它尽可能灵活,使其不依赖于简单地调用 current_user.

Is there a better approach to obtaining the parent application's signed in user? I'd like it to be as flexible as possible such that it is not dependent on simply calling current_user.

推荐答案

只要您在应用程序中只定义了一个基石用户,这样的事情应该可以工作:

Something like this should work, as long as you have only one cornerstone user defined in your application :

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

然后在您的 gem 中定义一个助手(即在 app/helpers/cornerstone_helper.rb 中):

Then define a helper in your gem (ie. in app/helpers/cornerstone_helper.rb) :

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 帮助程序获取当前已验证的用户.

You can then use the current_cornerstone_user helper to get the current authenticated user.

acts_as_cornerstone_user 用于多个类时,此方法会中断.但是,您会遇到一个问题,即拥有多个基础用户,而对应用程序模型一无所知(您应该在您的 gem 中).

This method breaks when acts_as_cornerstone_user is used on multiple classes. But you then have the problem of having multiple cornerstone users without knowing anything about the application models (you're supposed to be in your gem).

更新

如果你想要像 :auth_with => 这样的语法;:warden,您可以用以下内容替换助手:

If you'd like to have a syntax like :auth_with => :warden, you could replace the helper with the following :

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

使用 Cornerstone::Config::AUTH_MODES 设置如下:

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

这篇关于如何从 rails 3.1 引擎调用父应用程序的辅助方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆