Skip to content

Fall back to Devise.available_router_name when mapping has none#7

Merged
Fivell merged 3 commits into
mainfrom
fix-failure-path-global-router-name
Jun 2, 2026
Merged

Fall back to Devise.available_router_name when mapping has none#7
Fivell merged 3 commits into
mainfrom
fix-failure-path-global-router-name

Conversation

@Fivell

@Fivell Fivell commented Jun 2, 2026

Copy link
Copy Markdown
Member

Summary

Follow-up to the previous engine-mounted-Devise fix. Hosts that pin URL helpers via Devise.router_name = :engine in the devise initializer (the global setting) — but don't also pass router_name: to devise_for — still hit NoMethodError because Devise.mappings[scope].router_name is nil for them and the previous patch fell through to self.

Why

Devise's own URL-helper dispatcher (devise/controllers/url_helpers.rb#generate_helpers!) uses both:

router_name = Devise.mappings[scope].router_name
context = router_name ? send(router_name) : _devise_route_context

def _devise_route_context
  @_devise_route_context ||= send(Devise.available_router_name)
end

The previous gem fix only honored the per-mapping value. This commit adds the Devise.available_router_name fallback so the dispatcher matches Devise's own behavior:

def after_omniauth_failure_path_for(scope)
  router_name = ::Devise.mappings[scope].router_name ||
                ::Devise.available_router_name
  send(router_name).public_send(:"new_#{scope}_session_path")
end

Devise.available_router_name defaults to :main_app, so the default-app case still resolves through the main_app proxy — no special branching needed.

Test coverage

spec/dummy_engine/lib/admin_panel/config/routes.rb now uses the global-only pattern (devise_for :admin_users, ActiveAdmin::Devise.config with no router_name:), so the existing failure-path scenario in spec/engine/features/engine_mounted_login_spec.rb exercises this code path. It would fail with the previous patch and passes with this one.

Test plan

  • rake spec:all is green — covers default, engine (global-only), and isolated-engine setups.

Fivell added 3 commits June 2, 2026 12:10
Hosts that mount Devise inside an engine commonly pin URL helpers via the GLOBAL setting (Devise.router_name = :engine in devise.rb initializer) rather than passing router_name: in devise_for. In that case Devise.mappings[scope].router_name is nil, but Devise.available_router_name returns :engine — Devise's own URL-helper dispatcher uses that fallback, and we have to too.

Without the fallback the failure handler dropped through to `self`, which doesn't carry the engine-scoped session helper, and raised NoMethodError on hosts using the global pattern.

The dummy_engine app's routes drop the per-mapping `router_name:` option so the regression test now exercises the global-only setup (matches the host-app pattern that surfaced the bug).
PR #7 reconfigured spec/dummy_engine to use global-only Devise.router_name (drops the per-mapping router_name: option) so the existing OmniAuth-failure scenario in spec/engine/features/engine_mounted_login_spec.rb covers the new fallback code path. That coverage was implicit — someone reading the diff sees a config change + a method change without an obvious connection.

Spell it out: top comment now documents that the dummy uses global-only router_name and why the fallback matters; the failure-path context is renamed to 'OmniAuth failure path under global Devise.router_name'; a sanity check asserts Devise.mappings[:admin_user].router_name is nil and Devise.available_router_name == :admin_panel so a future flip back to the per-mapping form is loud.
The scenario now checks not just have_current_path but also have_button (the SSO landing renders again) and have_content for the access-denied flash. If the failure handler ever silently redirects to the wrong place — or if the SSO landing view itself regresses — the test catches it; previously only the path was checked, which would have passed a redirect to an empty AA index page that happens to share /admin/login.
@Fivell Fivell merged commit d53dbe9 into main Jun 2, 2026
6 checks passed
Fivell added a commit that referenced this pull request Jun 2, 2026
PR #7 reconfigured spec/dummy_engine to use global-only Devise.router_name (drops the per-mapping router_name: option) so the existing OmniAuth-failure scenario in spec/engine/features/engine_mounted_login_spec.rb covers the new fallback code path. That coverage was implicit — someone reading the diff sees a config change + a method change without an obvious connection.

Spell it out: top comment now documents that the dummy uses global-only router_name and why the fallback matters; the failure-path context is renamed to 'OmniAuth failure path under global Devise.router_name'; a sanity check asserts Devise.mappings[:admin_user].router_name is nil and Devise.available_router_name == :admin_panel so a future flip back to the per-mapping form is loud.
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

Successfully merging this pull request may close these issues.

1 participant