Plugin controller spec: Plugin routes are not drawn


(Philipp Rudloff) #1

Running the tests for my plugin like this:

LOAD_PLUGINS=1 bundle exec rake plugin:spec["my-plugin"]

… the plugin’s routes don’t seem to be available in the following spec file:

spec/controllers/plugin/routes_controller_spec.rb:

require 'rails_helper'

describe Plugin::RoutesController do
  it 'should route to #index' do
    expect(get '/plugin_routes').to route_to(action: :index, controler: 'plugin/routes')
  end
end

Error:

ActionController::UrlGenerationError:
No route matches {:action=>"/plugin_routes", :controller=>"plugin/routes"}

What am I doing wrong? Below are excerpts of my plugin’s code:

plugin.rb:

# …

Discourse::Application.routes.append do
  get '/plugin_routes' => 'plugin/routes#index', constraints: AdminConstraint.new
end

app/controllers/plugin/routes_controller.rb:

class Plugin::RoutesController < ApplicationController
  def index
    # …
  end
end

(Robin Ward) #2

I’m not sure what you’re doing wrong from your example code, but we have plugins that draw routes and then test them:

and

I would try to pare down your code until it works.


(Philipp Rudloff) #3

I have my plugin’s routes set up like this:

Discourse::Application.routes.append do
  get '/plugin_routes' => 'plugin/routes#index', constraints: AdminConstraint.new
  put '/plugin_routes/:route_id' => 'plugin/routes#update', constraints: AdminConstraint.new
  delete '/plugin_routes/:route_id' => 'plugin/routes#destroy', constraints: AdminConstraint.new
end

I’m not using an engine. Does that matter?


(Robin Ward) #4

If you are creating a sub-path for your plugin, I recommend an engine. All our major plugins do so. If you are wanting to change existing routes, an engine is not required.

I don’t see anything immediately wrong with your example. My suggestion would be to start with one of the official plugins and update it to match what you are doing until it works, and go from there.


(Philipp Rudloff) #5

I tried using routes, but couldn’t figure out why the routes aren’t drawn with this setup:

Plugin::Engine.routes.draw do
  get '/plugin_routes' => 'plugin/routes#index'
  put '/plugin_routes/:route_id' => 'plugin/routes#update'
  delete '/plugin_routes/:route_id' => 'plugin/routes#destroy'
end

Discourse::Application.routes.append do
  mount Plugin::Engine, at: '/plugin', constraints: AdminConstraint.new
end

Running rake routes lists the engine, but no entries under Routes for Plugin::Engine:. The routes are listed for the poll plugin, for example. I can’t tell what the difference between my plugin and other plugins is which use engines.


(Robin Ward) #6

Again, please download one of our existing plugins and start removing code / modifying routes until it matches what you want. That is the best advice I can give.


(Philipp Rudloff) #7

For who might experience the same problem as described above, one way I am able to get the routes drawn with an engine was defining the engine inside plugin.rb rather than lib/engine.rb.

after_initialize do
  # …

  module ::Plugin
    class Engine < ::Rails::Engine
      engine_name Plugin::PLUGIN_NAME
      isolate_namespace Plugin
    end
  end

  Plugin::Engine.routes.draw do
    get '/my_routes' => 'routes#index'
    put '/my_routes/:route_id' => 'routes#update'
    delete '/my_routes/:route_id' => 'routes#destroy'
  end

  Discourse::Application.routes.append do
    mount Plugin::Engine, at: '/plugin', constraints: AdminConstraint.new
  end
end

Why that makes any difference is beyond me.


As for the strategy that was suggested: Yes, I could certainly try re-implementing my plugin starting with one of the default plugins and observe at which point the described failure occurs. However, that’s very impractical as I’ve working on the plugin for close to a year.


(Robin Ward) closed #8

(Régis Hanol) #9

There’s no need to use the LOAD_PLUGINS=1 in this case since the plugin:spec rake task already does it :wink: