Using modules as namespaces within an engine

Hi folks,

In writing an engine, it seems to me that I’d want to put all my model
and controller classes within a namespace, to avoid name clashes with
similarly named classes in the base application, or other engines. By
namespaces, I mean wrap the class in a module.

For example, I’m writing a forum engine, and I have two models.

Forum::Thread
Forum::Comment

And I’ve called their tables in the database:

forum_threads
forum_comments

I think this will prevent clashes nicelly, and rails handles it by
creating a /views/forum/ directory for my views. This all works fine
as a rails app, and it runs fine too when I move it into an engine,
except for loading fixtures in tests.

Rails does not have support for looking in a sub directory of
“fixtures”, so i need to call my fixture files:

forum_threads.yml
forum_comments.yml

Then, I do the following in my test case:

fixtures :forum_threads, :forum_comments
set_fixture_class :forum_comments => Forum::Comment
set_fixture_class :forum_threads => Forum::Thread

This works in a normal rails app, but not in an engine. All my tests
do something like this:

  1. Error:
    test_create_replaces_form_if_invalid(Forum::ThreadControllerTest):
    FixtureClassNotFound: The class “ForumThread” was not found.
    method find in fixtures.rb at line 403
    method instantiate_fixtures in testing_extensions.rb at line 86
    method instantiate_fixtures in testing_extensions.rb at line 85
    method silence in base.rb at line 794
    method instantiate_fixtures in testing_extensions.rb at line 84
    method instantiate_fixtures in testing_extensions.rb at line 317
    method instantiate_fixtures in testing_extensions.rb at line 316
    method setup_with_fixtures in fixtures.rb at line 525
    method setup in fixtures.rb at line 547

Does anyone know of any difference in the way that namespaces (nested
modules) work in engines compared to normal apps? I’m using rails
1.1.6

cheers,

Craig
www.craigambrose.com

Hi Craig, did you ever figure this out? I seem to have the same problem
where my tests are not able to load fixtures for a name-spaced model. I
get the same error:

FixtureClassNotFound: The class “MyappCountry” was not found.

(my model is Myapp::Country)

Ingo

I have this issue as well. I noticed that the namespace models are not
being found properly or the framework is not finding the fixture class
name check out issue #5157

if you have a solution I’d love to hear it!

Ingo W. wrote:

Hi Craig, did you ever figure this out? I seem to have the same problem
where my tests are not able to load fixtures for a name-spaced model. I
get the same error:

FixtureClassNotFound: The class “MyappCountry” was not found.

(my model is Myapp::Country)

Ingo

My solution was

require File.dirname(FILE) + ‘/…/…/test_helper’

class Messaging::BulletinTest < Test::Unit::TestCase

Test::Unit::TestCase.fixture_path= File.dirname(FILE)
+"/…/…/fixtures/messaging"

fixtures :messaging_bulletins

set_fixture_class :messaging_bulletins => Messaging::Bulletin

def test_database
assert_equal messaging_bulletins(:one).id, 1
end
end

notice the Test::Unit::TestCase.fixture_path function sets the path to
the fixtures which I want to use in this test case.

then I assign the fixture class to my namespaced model.

cheers!
Tim

Tim G. wrote:

I have this issue as well. I noticed that the namespace models are not
being found properly or the framework is not finding the fixture class
name check out issue #5157

if you have a solution I’d love to hear it!

Ingo W. wrote:

Hi Craig, did you ever figure this out? I seem to have the same problem
where my tests are not able to load fixtures for a name-spaced model. I
get the same error:

FixtureClassNotFound: The class “MyappCountry” was not found.

(my model is Myapp::Country)

Ingo

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs