rails/rails — Issue #4537: Problem with loading subclasses of a class on server
Author: theodorton
Created: 2012-01-19 10:32:39+00:00
State: open

Tried to search the issues for a solution, and Googled the problem - but didn't find any answers.

I've written the following code

````ruby
# app/models/app/dashboard/modules/base.rb
class App::Dashboard::Modules::Base
..
class << self
def all
subclasses
end
end
end

# app/controllers/app/dashboards_controller.rb
class App::DashboardsController < App::BaseController
def show
@modules = App::Dashboard::Modules::Base.all
end
end
````

And the following controller and view spec passes:

````ruby
# spec/controllers/app/dashboards_controller_spec.rb
it "should assign some modules" do
expected = [
App::Dashboard::Modules::EmployeesModule
]
get :show
assigns[:modules].should == expected
end

# spec/views/app/dashboard/show.html.haml_spec.rb
it "should get some subclasses of the base module" do
assign(:modules, App::Dashboard::Modules::Base.all)
render
rendered.should contain('Employees module')
end
````

The spec passes and everything seems great, but when I try to view the page, the `@modules` assign is empty.

Adding a `require_relative 'employees_module.rb'` to `app/models/app/dashboard/modules/base.rb` solves the problem, but this doesn't make the use of `Class#subclasses` worth it, as everything needs to be hard-coded anyways.

My question is, why does the specs pass - when the output is different?