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

时间:2019-06-04 03:41:01

标签: ruby-on-rails ruby rubygems

给我的印象是,一旦安装了bundler,就可以在Rails应用程序中的任何位置访问gem的方法。但是不是吗?我很困惑,因为在移到文件外时无法识别在controller中可访问的方法。 有什么方法可以要求gem在command文件而不是controller文件中运行以下代码?

我正在使用名为sorcery的身份验证工具来实现OAuth登录系统。 gem提供了一些methods,例如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:>)

谢谢。

1 个答案:

答案 0 :(得分:0)

  

给我的印象是,一旦安装了bundler,就可以在Rails应用程序中的任何位置访问gem的方法。

那将是一个不正确的印象。

如您所见,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继承的类上被定义为受保护的实例方法,而在不从{继承的类上被定义为实例方法。 {1}},这就是为什么您获得ActionController::Base的原因。

  

我是否可以通过某种方式要求gem在命令文件中而不是控制器中运行以下代码?

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