I'd like to add some logic to a named route / url helper:
# app/helpers/my_helper.rb
module MyHelper
def article_path(obj, opts = {})
opts.merge!(token: @redis.get("token"))
super(obj, opts)
end
end
I also like to get the same logic in every controller, so I included it in my ApplicationController:
class ApplicationController < ActionController::Base
include TokenRoutesHelper
end
Inside a view, the helper get's overwritten and everything works fine. But inside a controller, it doesn't overwrite the article_path helper. I also tried to do a prepend TokenRoutesHelper, but this also doesn't work.
I had a look with pry on the methods, and it seems that Rails puts it's own named route helpers BEFORE any included helper, even when added via prepend:
[1] pry(#<ArticlesController>)> show-source article_path
From: /usr/local/bundle/gems/actionpack-5.2.4.5/lib/action_dispatch/routing/route_set.rb:322:
Owner: #<Module:0x000055b67aa85bc0>
Visibility: public
Signature: article_path(*args)
Number of lines: 11
define_method(name) do |*args|
last = args.last
options = \
case last
when Hash
args.pop
when ActionController::Parameters
args.pop.to_h
end
helper.call self, args, options
end
[2] pry(#<ArticlesController>)> show-source article_path --super
From: /usr/src/app/app/helpers/my_helper.rb:4:
Owner: MyHelper
Visibility: public
Signature: article_path(obj, opts = {})
Number of lines: 4
def article_path(obj, opts = {})
opts.merge!(token: @redis.get("token"))
super(obj, opts)
end
[3] pry(#<ArticlesController>)>
So, does Rails have a way to override it's auto generated url helpers?