繁体   English   中英

Ruby gem方法只在控制器中可用? 如果是这样,如何在其他文件中使用它?

[英]Ruby gem method only available in controller? If so, how to make it available in other files?

我的印象是,一旦用捆绑器安装了gem的方法,就可以在Rails应用程序中的任何地方访问它。 但事实并非如此吗? 我很困惑,因为当移动到文件外时,无法识别controller可访问的方法。 有什么方法我可以要求gem在command文件而不是controller运行下面的代码?

我正在使用名为sorcery的身份验证gem来实现OAuth登录系统。 有一些方法,比如gem提供的login_from(provider) ,可以从我的控制器访问就好了。

# app/controllers/oauths_controller.rb
class OauthsController < ApplicationController
  skip_before_action :require_login

  def callback
    provider = params[:provider]
    if @user = login_from(provider)
    # method continues
  end

但是,我们尝试在我们的应用程序中采用命令查询分离方法,因此我尝试将包含login_from(provider)方法的此进程移动到名为app/commands/authentication/login_command.rb的其他文件中。 这导致该方法无法识别:

NoMethodError (undefined method 'login_from' for #<Authentication::LoginCommand:>)

提前致谢。

我的印象是,一旦用捆绑器安装了gem的方法,就可以在Rails应用程序中的任何地方访问它。

这将是一个错误的印象。

如您所见, Sorcery::Engine使ActionController::Base包含在Sorcery::Controller定义的方法,这里:

require 'sorcery'
require 'rails'

module Sorcery
  # The Sorcery engine takes care of extending ActiveRecord (if used) and ActionController,
  # With the plugin logic.
  class Engine < Rails::Engine
    config.sorcery = ::Sorcery::Controller::Config

    initializer 'extend Controller with sorcery' do
      # TODO: Should this include a modified version of the helper methods?
      if defined?(ActionController::API)
        ActionController::API.send(:include, Sorcery::Controller)
      end

      if defined?(ActionController::Base)
        ActionController::Base.send(:include, Sorcery::Controller)
        ActionController::Base.helper_method :current_user
        ActionController::Base.helper_method :logged_in?
      end
    end
  end
end

Sorcer::Controller依次包含一系列子模块:

module Sorcery
  module Controller
    def self.included(klass)
      klass.class_eval do
        include InstanceMethods
        Config.submodules.each do |mod|
          # FIXME: Is there a cleaner way to handle missing submodules?
          # rubocop:disable Lint/HandleExceptions
          begin
            include Submodules.const_get(mod.to_s.split('_').map(&:capitalize).join)
          rescue NameError
            # don't stop on a missing submodule.
          end
          # rubocop:enable Lint/HandleExceptions
        end
      end
      Config.update!
      Config.configure!
    end

    ...
  end
end

其中一个子模块是Sourcery::Controller::Submodules::External ,当包含它时,还包括Sourcery::Controller::Submodules::External::InstanceMethods ,这里:

module Sorcery
  module Controller
    module Submodules
      # This submodule helps you login users from external auth providers such as Twitter.
      # This is the controller part which handles the http requests and tokens passed between the app and the @provider.
      module External
        def self.included(base)
          base.send(:include, InstanceMethods)
          ...
        end
      end
    end
  end
end

而且, InstanceMethods包含login_from方法,这里:

module Sorcery
  module Controller
    module Submodules
      # This submodule helps you login users from external auth providers such as Twitter.
      # This is the controller part which handles the http requests and tokens passed between the app and the @provider.
      module External
        def self.included(base)
          base.send(:include, InstanceMethods)

        module InstanceMethods
          protected

          ...

          # tries to login the user from provider's callback
          def login_from(provider_name, should_remember = false)
            sorcery_fetch_user_hash provider_name

            return unless (user = user_class.load_from_provider(provider_name, @user_hash[:uid].to_s))

            # we found the user.
            # clear the session
            return_to_url = session[:return_to_url]
            reset_sorcery_session
            session[:return_to_url] = return_to_url

            # sign in the user
            auto_login(user, should_remember)
            after_login!(user)

            # return the user
            user
          end

          ...
        end
      end
    end
  end
end

Soooo,这是一个很长的路要说, login_from被定义为继承自ActionController::Base类的受保护实例方法,并且没有被定义为不从ActionController::Base继承的类的实例方法,这就是为什么你'得到NoMethodError

有什么方法我可以要求gem在命令文件而不是控制器中运行下面的代码?

也许。 也许不吧。 如您所见, login_from正在使用从ActionController::Base继承的其他sorcery方法和方法。 因此,您需要确保所有这些方法在Authentication::LoginCommand中都可用,以便login_from正常运行。

暂无
暂无

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

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