After initializer change 'alias_method' un

2019-06-24 16:53发布

问题:

I've got the following initializer:

app/config/initializers/store_location.rb

module StoreLocation

  def self.skip_store_location
    [
        Devise::SessionsController,
        Devise::RegistrationsController,
        Devise::PasswordsController
    ].each do |controller|
      controller.skip_before_filter :store_location
    end
  end

  self.skip_store_location
end

Relevant parts of my ApplicationController:

class ApplicationController < ActionController::Base
  protect_from_forgery
  before_filter :convert_legacy_cookies
  before_filter :store_location

  alias_method :devise_current_user, :current_user

  def current_user
    # do something
  end

  private
  def store_location
    # store location
  end

Plus this in config/environments/development.rb

Foo::Application.configure do
# normal rails stuff
config.to_prepare do
    StoreLocation.skip_store_location
  end
end

If I let RSpec/Rails run the self.skip_store_location I'm getting the following error:

/foo/app/controllers/application_controller.rb:7:in `alias_method': undefined method `current_user' for class `ApplicationController' (NameError)

If I remove the call, everything is back to normal (except the filter is run, as expected). I'm guessing that I mess up dependency loading somehow?

回答1:

The problem is that you use alias_method before the method is defined in ApplicationController. To fix the problem, move the line

alias_method :devise_current_user, :current_user

below

def current_user
  # do something
end

It's a bit misleading that the error appears when running skip_store_location. I assume that happens because skip_store_location loads several controllers, and one of them is a subclass of ApplicationController.