diff --git a/packages/forest_admin_rpc_agent/lib/forest_admin_rpc_agent/routes/base_route.rb b/packages/forest_admin_rpc_agent/lib/forest_admin_rpc_agent/routes/base_route.rb index b52e8b672..8e07c0651 100644 --- a/packages/forest_admin_rpc_agent/lib/forest_admin_rpc_agent/routes/base_route.rb +++ b/packages/forest_admin_rpc_agent/lib/forest_admin_rpc_agent/routes/base_route.rb @@ -8,10 +8,10 @@ def initialize(url, method, name) end def registered(app) - if defined?(Sinatra) && (app == Sinatra::Base || app.ancestors.include?(Sinatra::Base)) - register_sinatra(app) - elsif defined?(Rails) && app.is_a?(ActionDispatch::Routing::Mapper) + if defined?(Rails) && app.is_a?(ActionDispatch::Routing::Mapper) register_rails(app) + elsif defined?(Sinatra) && app.is_a?(Class) && (app == Sinatra::Base || app.ancestors.include?(Sinatra::Base)) + register_sinatra(app) else raise NotImplementedError, "Unsupported application type: #{app.class}. #{self} works with Sinatra::Base or ActionDispatch::Routing::Mapper." diff --git a/packages/forest_admin_rpc_agent/spec/lib/forest_admin_rpc_agent/routes/base_route_spec.rb b/packages/forest_admin_rpc_agent/spec/lib/forest_admin_rpc_agent/routes/base_route_spec.rb index 1c7578647..f414f6ac6 100644 --- a/packages/forest_admin_rpc_agent/spec/lib/forest_admin_rpc_agent/routes/base_route_spec.rb +++ b/packages/forest_admin_rpc_agent/spec/lib/forest_admin_rpc_agent/routes/base_route_spec.rb @@ -49,6 +49,21 @@ def handle_request(_params) end end + context 'when the app is an ActionDispatch::Routing::Mapper and Sinatra is defined' do + let(:rails_app) { Class.new(Rails::Application) } + let(:rails_router) { ActionDispatch::Routing::Mapper.new(rails_app.routes) } + + before do + stub_const('Sinatra::Base', Class.new) + allow(route).to receive(:register_rails) + end + + it 'calls register_rails without raising NoMethodError' do + expect { route.registered(rails_router) }.not_to raise_error + expect(route).to have_received(:register_rails).with(rails_router) + end + end + context 'when the app is neither Sinatra nor Rails' do let(:unknown_app) { Object.new }