Permalink
Browse files

Respect model namespaces when generating factories.

The factory name for the model Namespaced::User wil be :namespaced_user.
The :class option is specified as a string to prevent class reloading issues.

Addresses GH-44
  • Loading branch information...
1 parent abd78d8 commit 7df9c9b13a41f3410d979032aaff6831397f2c6f @joshuaflanagan joshuaflanagan committed Feb 26, 2012
@@ -10,4 +10,8 @@ Feature:
And I add "factory_girl_rails" from this project as a dependency
When I successfully run `bundle install`
And I successfully run `bundle exec rails generate model User name:string --fixture-replacement=factory_girl`
- Then the output should contain "test/factories/users.rb"
+ And I successfully run `bundle exec rails generate model Namespaced::User name:string --fixture-replacement=factory_girl`
+ Then the output should contain "test/factories/users.rb"
+ And the output should contain "test/factories/namespaced_users.rb"
+ And the file "test/factories/users.rb" should contain "factory :user do"
+ And the file "test/factories/namespaced_users.rb" should contain "factory :namespaced_user, :class => 'Namespaced::User' do"
@@ -6,6 +6,10 @@ class Base < Rails::Generators::NamedBase #:nodoc:
def self.source_root
@_factory_girl_source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'factory_girl', generator_name, 'templates'))
end
+
+ def explicit_class_option
+ ", :class => '#{class_name}'" unless class_name == singular_table_name.camelize
+ end
end
end
-end
+end
@@ -1,7 +1,7 @@
# Read about factories at https://github.com/thoughtbot/factory_girl
FactoryGirl.define do
- factory :<%= singular_name %> do
+ factory :<%= singular_table_name %><%= explicit_class_option %> do
<% for attribute in attributes -%>
<%= attribute.name %> <%= attribute.default.inspect %>
<% end -%>

0 comments on commit 7df9c9b

Please sign in to comment.