ahoy icon indicating copy to clipboard operation
ahoy copied to clipboard

Ability to specify parent controller

Open heaven opened this issue 4 years ago • 6 comments

We try using Ahoy with a React.js application and need to add custom token authentication to controllers. The gem inherits from the ApplicationsController which isn't used in our application for the API and doesn't allow us to apply token-based authentication.

As a workaround I've reopened the base controller and included some of the required concerns:

class Ahoy::BaseController < ApplicationController
  include APIExceptions
  include APITokenAuth
end

But in our case, it doesn't seem right to me that it inherits from the ApplicationsController.

heaven avatar Mar 26 '21 12:03 heaven

Which breaks the app and BaseController from the gem is never loaded. Should be required explicitly in the gem.

Thus my final BaseController looks like this:

class Ahoy::BaseController < API::APIController
  private

  def ahoy
    @ahoy ||= Ahoy::Tracker.new(controller: self, api: true)
  end
end

heaven avatar Mar 26 '21 13:03 heaven

Hey @heaven, thanks for the suggestion. There could probably be a parent_controller option similar to Devise if you'd like to submit a PR.

ankane avatar Mar 29 '21 03:03 ankane

@ankane this sounds like a good option. I'll try preparing a pull request, perhaps over the weekend.

heaven avatar Mar 29 '21 07:03 heaven

Hi @ankane, on a similar note, would you also entertain a PR to to move https://github.com/ankane/ahoy/blob/master/app/controllers/ahoy/base_controller.rb#L8-L33 into a mixin?

I have a use case where I want to define behavior for a specific namespace. For now, I'm just going to copy & paste the above code into my controller, but I would like to avoid the duplication.

Thanks!

kfprimm avatar May 21 '21 18:05 kfprimm

Hi @ankane, any progress with PR? Thanks.

durcak avatar Jan 04 '22 19:01 durcak

@ankane, @heaven is it okay if take on this PR?

jevin avatar Jan 18 '23 09:01 jevin