A
A
Anton Ivanov2015-11-03 08:36:42
Ruby on Rails
Anton Ivanov, 2015-11-03 08:36:42

Why is rails recommending a separate class for multidomain rather than a lambda?

Hello.
When looking for solutions for a single application to process requests on multiple domains, I find articles like this: stackoverflow.com/questions/4207657/rails-routing-...
This and all other articles recommend using a class to determine which of domains this block of routes is intended.
The following construction normally lives (and works) in my routing:

users_site  = ->(request) { 'users.site' == request.host }
api_site  = ->(request) { 'api.site' == request.host }
docs_site  = ->(request) { 'docs.site' == request.host }

constraints users_site do
...
end

constraints api_site do
...
end

constraints docs_site do
...
end

What is the disadvantage of such a solution, besides the fact that for each new domain it will be necessary to write a separate lambda?
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
Jeiwan, 2015-11-03
@Fly3110

No difference. The class is taken out if there is too much logic in the condition. You have only one line, so a lambda is enough. It's just a matter of code readability.
But I would never take it to a class at all, since every time I have to look for this class, climb into it and understand it. It's better to have everything in one place.
Well, this answer on SO is morally outdated: now putting something in lib is very bad. But then I don’t know where to store these classes - usually this is decided within the framework of a specific project, everyone does what is convenient for him.
In addition, an article on how to split routes.rb into multiple modules: blog.arkency.com/2015/02/how-to-split-routes-dot-r...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question