1

I have defined a helper method as such (for my rails engine):

module Xaaron
  class ApplicationController < ActionController::Base
    protect_from_forgery with: :null_session
    rescue_from ActiveRecord::RecordNotFound, :with => :record_not_found

    helper_method :current_user
    helper_method :authenticate_user!

    def current_user
      @current_user ||= Xaaron::User.find_by_auth_token(cookies[:auth_token]) if cookies[:auth_token]
    end

    def authenticate_user!
      if current_user
        true
      else
        redirect_to xaaron.login_path
        false
      end
    end

    protected

    def record_not_found
      flash[:error] = 'Could not find specified role'
      redirect_to xaaron.record_not_found_path
      true
    end
  end
end

As far as I know everything above is correct in terms of creating helper methods. So now I need to use this helper method:

module Xaaron
  class ApiKeysController < ActionController::Base
    before_action :authenticate_user!

    def index
      @api_key = Xaaron::ApiKey.where(:user_id => current_user.id)
    end

    def create
      @api_key = Xaaron::ApiKey.new(:user_id => current_user.id, :api_key => SecureRandom.hex(16))
      create_api_key(@api_key)
    end

    def destroy
      Xaaron::ApiKey.find(params[:id]).destroy
      flash[:notice] = 'Api Key has been deleted.'
      redirect_to xarron.api_keys_path
    end
  end
end

As you can see, before every action the user must be authenticated. So the authenticat_user! method is then called.

Lets write a test for this

it "should not create an api key for those not logged in" do
  post :create
  expect(response).to redirect_to xaaron.login_path
end

This, we expect, to send us back to the login path because we are not signed in, and as you will recall we are using authenticate before every action in the API Controller. What do we get instead:

  1) Xaaron::ApiKeysController#create should not create an api key for those not logged in
     Failure/Error: post :create
     NoMethodError:
       undefined method `authenticate_user!' for #<Xaaron::ApiKeysController:0x007f898e908a98>
     # ./spec/controllers/api_keys_controller_spec.rb:9:in `block (3 levels) in <top (required)>'

Last I checked the way I defined a helper method is how rails casts has done it, how other stack questions have done it and how rails docs states to do it - unless I missed some majour step - why isn't this working?

user3379926
  • 3,855
  • 6
  • 24
  • 42

1 Answers1

0

Maybe I haven't seen a helper method set up like this before (I'm new to rails) but the helper methods I've seen are defined without controllers.

Usually I see a file like this in the helpers folder

module SessionsHelper

def sign_in(user)
remember_token = User.new_remember_token
cookies.permanent[:remember_token] = remember_token
user.update_attribute(:remember_token, User.encrypt(remember_token))
self.current_user = user
end
def current_user=(user)
@current_user = user
end
... 

and then

include SessionsHelper

In the application controller.

To me it looks like you're calling the controller a helper method, I'm not sure what the benefits of that would be - but I suppose I wouldn't.

Sorry if this wasn't helpful

Peege151
  • 1,562
  • 2
  • 21
  • 45