Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blazer app colliding with my main ApplicationController and app's routes #66

Closed
aguynamedben opened this issue Jun 16, 2016 · 8 comments

Comments

@aguynamedben
Copy link

Hi, I'm trying to install blazer to try it out. I'm running into a few weird issues where Blazer is colliding with my main app.

My core question is: Is Blazer supposed to be mounted as an entire separate app, similar to Sidekiq's web interface? Or is Blazer supposed to inherit code (controller, routes) from my main app?

Environment:

  • Ruby 2.3.1
  • Rails 4.2.6
  • Blazer 1.4.0

I followed the README very directly...

  • Default blazer.yml generated by rails g blazer:install
  • mount Blazer::Engine, at: "blazer" in config/routes.rb

The first problem I run into is this error when loading /blazer:

You must activate the Authlogic::Session::Base.controller with a
controller object before creating objects

My main app uses the authlogic gem. The traceback for this error leads to a method in app/controllers/application_controller.rb (my main application controller). Blazer::BaseController subclasses ApplicationController, and it's using my main app's ApplicationController. Is this intentional?

When I comment out the offending authentication function in ApplicationController, the /blazer path loads, but all URL generation on the page fails and none of the links work (i.e. can't a new dashboard). The log says:

DEPRECATION WARNING: You are trying to generate the URL for a named route called "new_query"
but no such route was found. In the future, this will result in an `ActionController::UrlGenerationError`
exception.` `rake routes` shows the routes like this:

rake routes shows:

(my apps routes, where there is no new_query route)

Routes for Blazer::Engine:
      run_queries POST   /queries/run(.:format)            blazer/queries#run
    refresh_query POST   /queries/:id/refresh(.:format)    blazer/queries#refresh
   tables_queries GET    /queries/tables(.:format)         blazer/queries#tables
   new_query GET    /queries/new(.:format)            blazer/queries#new
...

Somehow, it seems like Blazer is again, when looking for routes, looking in the routes for my app, not the Blazer Engine routes. From reading about Rails Engines, it seems like the intention is for none of Blazer's code to use my ApplicationController or routes, but that's not happening.

It's too bad, I wish I could get this working to test. I'm happy to do some extra work around here if you guide me on where to look. Thanks for the time you spend working on this gem, it looks awesome, I hope I can get it working somehow.

@ankane
Copy link
Owner

ankane commented Jun 21, 2016

Blazer is a separate engine, but it uses your ApplicationController for authentication (as not all authentication libraries add themselves to ActionController::Base). It shouldn't use any routes from your application. Would be great to hear if others have gotten Blazer to work with authlogic.

@cannikin
Copy link
Contributor

cannikin commented Mar 23, 2017

@aguynamedben I got Blazer working with Authlogic with a tiny workaround. You get to specify one before_action in the blazer.yml config which is meant to be one that, for example, only allows admin access to the /blazer endpoint. But, you can have this before_action do whatever you want, including setting up Authlogic so that it recognizes the controller:

# config/blazer.yml
before_action :activate_authlogic_and_require_admin

# app/controllers/application_controller.rb
private def activate_authlogic_and_require_admin
  activate_authlogic
  require_admin
end

(In this case require_admin is an existing method in my app that redirects to a "Not Found" page if the user doesn't have admin permissions.)

Works like a charm and doesn't feel too hacky, always the sign of a good workaround. :)

@ankane Would you guys accept a PR that updated the README.md with this info?

@ankane
Copy link
Owner

ankane commented Mar 24, 2017

Hey @cannikin, thanks for sharing. I'm happy to include instructions for Authlogic if it's non-trivial. However, I don't see any reference to the approach above in the Authlogic docs. Is this specific to your app? https://github.com/binarylogic/authlogic

@cannikin
Copy link
Contributor

@ankane Nope not specific to my app, it'll happen in any controller that tries to access something that Authlogic provides (like current_user) but that doesn't run the activate_authlogic before_action that Authlogic automatically adds to ApplicationController.

Since Blazer clears out all before_actions Authlogic doesn't get a chance to set Authlogic::Session::Base.controller automagically.

You can see it appending the before_action here: https://github.com/binarylogic/authlogic/blob/master/lib/authlogic/controller_adapters/rails_adapter.rb#L49

And then what that action actually does here: https://github.com/binarylogic/authlogic/blob/master/lib/authlogic/controller_adapters/rails_adapter.rb#L58

Any controller that inherits from ApplicationController (including Blazer) would get that before_action automatically, but since you guys strip them it never happens and Authlogic doesn't know what's going on.

It would definitely be cleaner to just call activate_authlogic in the Blazer config, but don't you guys only allow a single before_action to be set? Or is it array-like behind the scenes and you can append multiple? It would be awesome if in blazer.yml you could do something like:

before_action: activate_authlogic, require_admin

Or maybe:

before_action: activate_authlogic
before_action: require_admin

I'll actually update my code sample above to use the built-in activate_authlogic instead of setting Authlogic::Session::Base.controller manually.

@ankane
Copy link
Owner

ankane commented Mar 26, 2017

When removing callbacks, we could keep activate_authlogic around.

@cannikin
Copy link
Contributor

Ooo, that would be even cleaner for Authlogic folks! I can work on that if you'd like. PR in the next few days?

@ankane
Copy link
Owner

ankane commented Mar 28, 2017

👍 works for me

@cannikin
Copy link
Contributor

Just created #140

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants