Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leoarnold/activerecord on load #439

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions lib/factory_bot_rails/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@

module FactoryBotRails
class Railtie < Rails::Railtie
config.factory_bot = ActiveSupport::OrderedOptions.new
config.factory_bot.definition_file_paths = FactoryBot.definition_file_paths
config.factory_bot.reject_primary_key_attributes = true
config.factory_bot.validator = FactoryBotRails::FactoryValidator.new
initializer "factory_bot.configuration" do
config.factory_bot = ActiveSupport::OrderedOptions.new
config.factory_bot.definition_file_paths = FactoryBot.definition_file_paths
config.factory_bot.reject_primary_key_attributes = true
config.factory_bot.validator = FactoryBotRails::FactoryValidator.new
end

initializer "factory_bot.set_fixture_replacement" do
Generator.new(config).run
Expand All @@ -21,13 +23,15 @@ class Railtie < Rails::Railtie
FactoryBot.definition_file_paths = definition_file_paths
end

ActiveSupport.on_load :active_record do
config = Rails.configuration.factory_bot
initializer "factory_bot.reject_primary_key_attributes" do
ActiveSupport.on_load :active_record do
config = Rails.configuration.factory_bot

if config.reject_primary_key_attributes
require "factory_bot_rails/factory_validator/active_record_validator"
if config.reject_primary_key_attributes
require "factory_bot_rails/factory_validator/active_record_validator"

config.validator.add_validator FactoryValidator::ActiveRecordValidator.new
config.validator.add_validator FactoryValidator::ActiveRecordValidator.new
end
end
end

Expand Down
48 changes: 48 additions & 0 deletions spec/factory_bot_rails/railtie_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,52 @@ def wait_for_rails_to_reload
sleep 0.01
end
end

describe "initializer 'factory_bot.reject_primary_key_attributes'" do
before do
Rails.application.config.factory_bot.validator = FactoryBotRails::FactoryValidator.new
end

context "when config.reject_primary_key_attributes is true" do
before do
Rails.application.config.factory_bot.reject_primary_key_attributes = true
allow(Rails.application.config.factory_bot.validator).to receive(:add_validator)
end

it "adds the ActiveRecordValidator to the validator list" do
run_reject_primary_key_attributes_initializer

expect(Rails.application.config.factory_bot.validator).to have_received(:add_validator)
end
end

context "when config.reject_primary_key_attributes is false" do
before do
Rails.application.config.factory_bot.reject_primary_key_attributes = false
allow(Rails.application.config.factory_bot.validator).to receive(:add_validator)
end

it "does not add the ActiveRecordValidator to the validator list" do
run_reject_primary_key_attributes_initializer

expect(Rails.application.config.factory_bot.validator).not_to have_received(:add_validator)
end
end

def run_reject_primary_key_attributes_initializer
initializer = FactoryBotRails::Railtie.initializers.find do |i|
i.name == "factory_bot.reject_primary_key_attributes"
end
initializer.run(Rails.application)
end
end

describe "initializer order" do
it "runs 'factory_bot.configuration' before any other initializer" do
initializers = FactoryBotRails::Railtie.initializers.map(&:name)
first_initializer = initializers.first

expect(first_initializer).to eq("factory_bot.configuration")
end
end
end