diff --git a/features/fixture_replacement_config.feature b/features/fixture_replacement_config.feature index fb26bb3..91edb7e 100644 --- a/features/fixture_replacement_config.feature +++ b/features/fixture_replacement_config.feature @@ -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 | diff --git a/lib/factory_girl_rails.rb b/lib/factory_girl_rails.rb index aac86d2..b03895a 100644 --- a/lib/factory_girl_rails.rb +++ b/lib/factory_girl_rails.rb @@ -1,2 +1,4 @@ require 'factory_girl_rails/railtie' +module FactoryGirlRails +end diff --git a/lib/factory_girl_rails/generator.rb b/lib/factory_girl_rails/generator.rb new file mode 100644 index 0000000..cd27922 --- /dev/null +++ b/lib/factory_girl_rails/generator.rb @@ -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 diff --git a/lib/factory_girl_rails/generators/non_rspec_generator.rb b/lib/factory_girl_rails/generators/non_rspec_generator.rb new file mode 100644 index 0000000..e261d9a --- /dev/null +++ b/lib/factory_girl_rails/generators/non_rspec_generator.rb @@ -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 diff --git a/lib/factory_girl_rails/generators/null_generator.rb b/lib/factory_girl_rails/generators/null_generator.rb new file mode 100644 index 0000000..e1868fb --- /dev/null +++ b/lib/factory_girl_rails/generators/null_generator.rb @@ -0,0 +1,11 @@ +module FactoryGirlRails + module Generators + class NullGenerator + def initialize(generators) + end + + def run + end + end + end +end diff --git a/lib/factory_girl_rails/generators/rspec_generator.rb b/lib/factory_girl_rails/generators/rspec_generator.rb new file mode 100644 index 0000000..0248337 --- /dev/null +++ b/lib/factory_girl_rails/generators/rspec_generator.rb @@ -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 diff --git a/lib/factory_girl_rails/railtie.rb b/lib/factory_girl_rails/railtie.rb index daf92ca..707be0b 100644 --- a/lib/factory_girl_rails/railtie.rb +++ b/lib/factory_girl_rails/railtie.rb @@ -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