I don't like previously posted solutions - extensions through monkey-patching aside - since they either:
- break OOP (
File.read
+ instance_eval
- cripes) or
- inhibit reusability: including modules - you can do it only once - never mind that on the 2nd you understand that module inclusion is actually a mechanic to execute a bit of code, cripes
we're not in the 2000s any more, it shouldn't be acceptable to move parts of source files around and File.read
-instance_eval
them and then wonder what happened - or include modules as a poor man's function call - if there's any alternative.
I do this, notice it is nestable:
scope :processing do
SomeModule::SomeDomain::Routes.call(self)
end
# and it's reusable:
scope :some_other_scope do
SomeModule::SomeDomain::Routes.call(self)
end
The benefit of this approach is that in the end it's just simple executable ruby code that can be moved around and understood clearly both by your IDE and by your fellow programmers.
You will find the above-mentioned piece of code in routes.rb
you will look at it, you will control-click it and what you'll find is what you expected:
module SomeModule::SomeDomain
class Routes
def self.call r
r.scope :some_scope do
r.match .....
end
end
end
this means this is clean code.
It's also not stuck in "function-oriented code organization" that rails seemingly enforces for a long time now, but allows you to build domain-specific structures with their own route trees that should be the preferable norm.
PS
I also use a module that makes a class quack like a function - allows composition with procs and much more - but this isn't really necessary here, it'd just give you the syntax candy of saying SomeModule::SomeDomain::Routes[self]