How does Rails determine Rails.configuration.root?

87 views Asked by At

Or rather, how does the determination work?

It takes place with the definition of the base.called_from in /lib/rails/engine.rb:

def inherited(base)
    unless base.abstract_railtie?
      Rails::Railtie::Configuration.eager_load_namespaces << base

      base.called_from = begin
        call_stack = caller_locations.map { |l| l.absolute_path || l.path }

        File.dirname(call_stack.detect { |p| !p.match?(%r[railties[\w.-]*/lib/rails|rack[\w.-]*/lib/rack]) })
      end
    end

  super
end 

The invocation of Kernel#caller_locations returns an Array of Thread::Backtrace::Location-objects, right?

First, I don't understand the idiom in the block passed to map:

l.absolute_path || l.path

Does it want to ensure that if l.absolute_path is nil then at least l.path is element of the result of the map-Enumerator ? But why shouldn't l.absolute_path not exist?

Second, I don't understand the construct composed out of the detect-Enumerator and the regex (-operator).

The detectEnumerator takes the first element for which the expression, here !~, is true. Okay.

But how does the expression, which is true if a path p doesn't match either

railties[\w.-]*/lib/rails

"or" (pipe)

rack[\w.-]*/lib/rack

In other words: Rails.application.root is the first path that doesn't match one of the two regex-patterns. Correct?

But if so, then: why?

(And what method, if I may ask, is super in this context?)

Thanks

von Spotz

0

There are 0 answers