mirror of
https://github.com/thoughtbot/factory_bot_rails.git
synced 2022-11-09 11:49:18 -05:00
Move generators from rails3-generators gem over to factory_girl_rails.
This commit is contained in:
parent
dde5502e80
commit
9f26cabfdf
4 changed files with 47 additions and 0 deletions
13
features/generators.feature
Normal file
13
features/generators.feature
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
Feature:
|
||||||
|
In order to easily generate factory files instead of fixture files when generating models
|
||||||
|
As a user of Rails3 and factory_girl
|
||||||
|
I would like to use factory_girl_rails generators.
|
||||||
|
|
||||||
|
Scenario: The factory_girl_rails generators create a factory file
|
||||||
|
for each model that I generate
|
||||||
|
When I successfully run `bundle exec rails new testapp`
|
||||||
|
And I cd to "testapp"
|
||||||
|
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"
|
11
lib/generators/factory_girl.rb
Normal file
11
lib/generators/factory_girl.rb
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
require 'rails/generators/named_base'
|
||||||
|
|
||||||
|
module FactoryGirl
|
||||||
|
module Generators
|
||||||
|
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
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
14
lib/generators/factory_girl/model/model_generator.rb
Normal file
14
lib/generators/factory_girl/model/model_generator.rb
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
require 'generators/factory_girl'
|
||||||
|
|
||||||
|
module FactoryGirl
|
||||||
|
module Generators
|
||||||
|
class ModelGenerator < Base
|
||||||
|
argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
|
||||||
|
class_option :dir, :type => :string, :default => "test/factories", :desc => "The directory where the factories should go"
|
||||||
|
|
||||||
|
def create_fixture_file
|
||||||
|
template 'fixtures.rb', File.join(options[:dir], "#{table_name}.rb")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
9
lib/generators/factory_girl/model/templates/fixtures.rb
Normal file
9
lib/generators/factory_girl/model/templates/fixtures.rb
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
# Read about factories at http://github.com/thoughtbot/factory_girl
|
||||||
|
|
||||||
|
FactoryGirl.define do
|
||||||
|
factory :<%= singular_name %> do
|
||||||
|
<% for attribute in attributes -%>
|
||||||
|
<%= attribute.name %> <%= attribute.default.inspect %>
|
||||||
|
<% end -%>
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue