Changing routes for Foreman help

I’m working on Rails 5.2 migration, and there is a deprecation error:
DEPRECATION WARNING: Using a dynamic :controller segment in a route is deprecated and will be removed in Rails 6.0. (called from block in <top (required)> at /home/ik/projects/foreman/config/routes.rb:17)

The error is for the following code:

get '(:controller)/help', :action => 'welcome', :as => "help"

The above support was supposed to be removed on rails 5.2, but they decided for now to wait for rails 6 instead.

The path of (:controller) is called dynamic segment. Both (:controller) and (:action) are deprecated.

From action_dispatch/routing/route_set.rb for actionpack 5.2.0 line 580-609:

def add_route(mapping, name)
        raise ArgumentError, "Invalid route name: '#{name}'" unless name.blank? || name.to_s.match(/^[_a-z]\w*$/i)

        if name && named_routes[name]
          raise ArgumentError, "Invalid route name, already in use: '#{name}' \n" \
            "You may have defined two routes with the same name using the `:as` option, or " \
            "you may be overriding a route already defined by a resource with the same naming. " \
            "For the latter, you can restrict the routes created with `resources` as explained here: \n" \
            "http://guides.rubyonrails.org/routing.html#restricting-the-routes-created"
        end

        route = @set.add_route(name, mapping)
        named_routes[name] = route if name

        if route.segment_keys.include?(:controller)
          ActiveSupport::Deprecation.warn(<<-MSG.squish)
            Using a dynamic :controller segment in a route is deprecated and
            will be removed in Rails 6.0.
          MSG
        end

        if route.segment_keys.include?(:action)
          ActiveSupport::Deprecation.warn(<<-MSG.squish)
            Using a dynamic :action segment in a route is deprecated and
            will be removed in Rails 6.0.
          MSG
        end

        route
      end

The thing is, unless I’ved missed something, there is no new way to create a dynamic segment.
I found few hacks to do it (like keeping a list of all controllers), and they ugly and hard to do with foreman due to the rails engines, and not to mention the number of controllers we have.

I have only bad ideas on how to change this routes, and I’m looking for good ideas instead.
So any ideas in the matter are more then welcome.

1 Like

at worse case, can’t you duplicate the line (just like we do for auto
completer?) for every resource?

the naive way will be just that, but there are so many exceptions in the resource rule, with sub resources and so on, that I hope it will work well.

I’ll work on it later on, after some f2f discussion with @iNecas, it is less of a priority at this time, and making Foreman core Rails 5.2 ready is in a high priority.

I’ve came to the same conclusion some time ago.

Is it really that bad to copy and paste those routes around? Then create a simple routing test that will verify that all controllers except those explicitly listed in the test has a help route. Work done.

DRY must not be a dogma, sometimes it’s more simple to just copy - it’s pretty clear for everybody familiar with Rails what’s going on.

2 Likes

not that bad, the only thing in that case, is that it will be harder to manage it, so I wanted to avoid it if possible.

Yeah I could not figure it out as well. :frowning:

I was thinking in breaking the current structure and create something a bit different.
For example:
/help/:controller_name and then it solves the issue, but breaks everything existed today.

It means that the help controller is a sort of a dispatching system based on the controller name, and will be easier to sustain for longer period of time imo.

That’s the best idea I was able to think about on my own, and I still dislike it, because there might be several variation for it.

That is, we have for example nested path’s that needed to be handle, and what happen if we have dynamic path (haven’t researched if we already have that), that is different help for /my_controller and /my_controller/feature, and also ``/my_controller/…/feature`
I mean, the middle can be changed, for example, or something like that.