1
0
Fork 0
mirror of https://github.com/thoughtbot/factory_bot_rails.git synced 2022-11-09 11:49:18 -05:00

Break generator up for separate frameworks

Closes #91, Closes #62
This commit is contained in:
Joshua Clayton 2013-03-08 15:36:00 -05:00
parent b18cc3398e
commit 90e02a3a3c
7 changed files with 113 additions and 14 deletions

View file

@ -77,3 +77,16 @@ Feature:
But the following files should not exist:
| spec/fixtures/users.yml |
And the file "test/models/user_test.rb" should contain "MiniTest::Rails::ActiveSupport::TestCase"
Scenario: Disable Factory Girl generator
When I configure the factories as:
"""
config.generators do |g|
g.factory_girl false
end
"""
And I run `bundle install` with a clean environment
And I run `bundle exec rails generate model User name:string` with a clean environment
Then the following files should not exist:
| test/factories/users.rb |
| spec/factories/users.rb |

View file

@ -1,2 +1,4 @@
require 'factory_girl_rails/railtie'
module FactoryGirlRails
end

View file

@ -0,0 +1,43 @@
require 'factory_girl_rails/generators/rspec_generator'
require 'factory_girl_rails/generators/non_rspec_generator'
require 'factory_girl_rails/generators/null_generator'
module FactoryGirlRails
class Generator
def initialize(config)
@generators = if config.respond_to?(:app_generators)
config.app_generators
else
config.generators
end
end
def run
generator.new(@generators).run
end
def generator
if factory_girl_disabled?
Generators::NullGenerator
else
if test_framework == :rspec
Generators::RSpecGenerator
else
Generators::NonRSpecGenerator
end
end
end
def test_framework
rails_options[:test_framework]
end
def factory_girl_disabled?
rails_options[:factory_girl] == false
end
def rails_options
@generators.options[:rails]
end
end
end

View file

@ -0,0 +1,19 @@
module FactoryGirlRails
module Generators
class NonRSpecGenerator
def initialize(generators)
@generators = generators
end
def run
@generators.test_framework test_framework, fixture: false, fixture_replacement: :factory_girl
end
private
def test_framework
@generators.options[:rails][:test_framework]
end
end
end
end

View file

@ -0,0 +1,11 @@
module FactoryGirlRails
module Generators
class NullGenerator
def initialize(generators)
end
def run
end
end
end
end

View file

@ -0,0 +1,23 @@
module FactoryGirlRails
module Generators
class RSpecGenerator
def initialize(generators)
@generators = generators
end
def run
@generators.fixture_replacement fixture_replacement_setting, dir: factory_girl_directory
end
private
def fixture_replacement_setting
@generators.options[:rails][:fixture_replacement] || :factory_girl
end
def factory_girl_directory
@generators.options.fetch(:factory_girl, { dir: 'spec/factories' })[:dir]
end
end
end
end

View file

@ -1,24 +1,12 @@
require 'factory_girl'
require 'factory_girl_rails/generator'
require 'rails'
module FactoryGirl
class Railtie < Rails::Railtie
initializer "factory_girl.set_fixture_replacement" do
generators = config.respond_to?(:app_generators) ? config.app_generators : config.generators
rails_options = generators.options[:rails]
if rails_options[:test_framework] == :rspec
factory_girl_dir = generators.options.fetch(:factory_girl, { :dir => 'spec/factories' })[:dir]
if rails_options.has_key?(:fixture_replacement)
generators.fixture_replacement rails_options[:fixture_replacement], :dir => factory_girl_dir
else
generators.fixture_replacement :factory_girl, :dir => factory_girl_dir
end
else
generators.test_framework rails_options[:test_framework], :fixture => false, :fixture_replacement => :factory_girl
end
FactoryGirlRails::Generator.new(config).run
end
initializer "factory_girl.set_factory_paths" do