Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think this is because of how you've defined the module. You're attempting to define it like this:</p> <pre><code>module Modules::Projects::Financials def foo 'bar' end end </code></pre> <p>But Ruby will interpret this by attempting to find the <code>Modules</code> module first, then the <code>Projects</code> module and then will finally define the <code>Financials</code> module, but ONLY if it can find the first two.</p> <p>In your case, it <em>can't</em> find the first two and so bombs out like that.</p> <p>Go ahead, fire up <code>irb</code> and copy and paste the above code example into it. You'll see exactly the same error that you're getting when you run your specs.</p> <hr> <p>The way to fix this is to just define/re-open the module in each file:</p> <pre><code>module Modules module Projects module Financials def foo 'bar' end end end end </code></pre> <p>Now that each time each one of these files is loaded, it will either define or re-open the modules, adding functionality to them. The best part about this is that it doesn't matter if <code>Modules</code> is or isn't defined first, it'll just define it anyway.</p> <hr> <p>Now to address the point: Why does it work in Rails?</p> <p><strong>Oh man, Rails is totally doing some awesome stuff!</strong></p> <p>I actually covered how Rails deals with the automatic definition of modules <a href="http://ryanbigg.com/2011/11/screencast-wrong-argument-type/" rel="nofollow">in my "wrong argument type" screencast</a>. Well, I don't exactly cover how modules are automatically loaded, but (<em>spoiler alert</em>) it's the culprit for what's going wrong there.</p> <p>I'm not going to force you to watch it. The problem lies with <a href="https://github.com/rails/rails/blob/3-1-stable/activesupport/lib/active_support/dependencies.rb#L488-492" rel="nofollow">these lines inside <code>activesupport/lib/active_support/dependencies.rb</code></a>. </p> <p>The <code>load_missing_constant</code> method is used by Rails when it can't find a constant. That's when Rails's magic autoloading stuff kicks into gear. It calls this method, and attempts to find a file that defines this module. </p> <p>If it can't do that, it does this:</p> <pre><code>mod = Module.new into.const_set const_name, mod autoloaded_constants &lt;&lt; qualified_name unless autoload_once_paths.include?(base_path) return mod </code></pre> <p>This is <a href="https://github.com/rails/rails/blob/3-1-stable/activesupport/lib/active_support/dependencies.rb#L433-439" rel="nofollow">inside the <code>autoload_module!</code> method in that <code>dependencies.rb</code> file</a>. </p> <p>What this code does is simple: creates a new module, sets its constant name to the one that's missing, adds the <code>qualified_name</code> to the <code>autoloaded_constants</code> and returns that module.</p> <p>This is why Rails is defining your modules <em>even though they don't really exist</em>. You're completely bypassing this in your spec (with good reason, you don't want all of that nasty Rails junk), and so it's not automatically loading the modules.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload