I have a rails application that does some upfront scoping to handle multiple subdomains and multiple languages but results in having two sets of inner matches that are identical. I would like to break these inner matches out to a function so that I can reduce and reuse code I cannot find any examples of someone doing this in the routes.
Code:
constraints lambda { |request| request.subdomains[0].include? "internal" } do
scope ":hash" do
get '/', to: 'product#index'
get '/:productID', to: 'product#show'
end
get "", to: 'product#no_hash'
get "/*path", to: 'product#no_hash'
end
constraints lambda { |request| !request.subdomains[0].include? "internal" } do
scope ":hash" do
get '/', to: 'product#index'
get '/:productID', to: 'product#show'
end
end
Again the goal is to get the inner matches placed into a function so that I can reduce duplication, reuse code, and save the world. Please help.
Well, I have two answers:
First one: actually your shared routes do not depend on your subdomain, so you could just write them out of your constraints, they would be directly shared.
Another one, with a function
def shared_routes
scope ":hash" do
get '/', to: 'product#index'
get '/:productID', to: 'product#show'
end
end
constraints lambda { |request| request.subdomains[0].include? "internal" } do
shared_routes
get "", to: 'product#no_hash'
get "/*path", to: 'product#no_hash'
end
constraints lambda { |request| !request.subdomains[0].include? "internal" } do
shared_routes
end
Not sure I can help you save the world though :)