From 7be631f90ab26a0fec8ac5462afded2599ce6bd9 Mon Sep 17 00:00:00 2001 From: Mike Burns Date: Fri, 29 Dec 2023 18:56:20 -0500 Subject: [PATCH] Allow primary key attributes This improvement went out too quickly, so let's undo it. Lots of the work around it was great and we want to keep that, so this surgically snips out: - The README describing the feature. - The ActiveRecordValidator that implements the validation. - The railtie code that loaded the validator. It leaves the validation framework and the notification code. --- README.md | 16 ------------- .../active_record_validator.rb | 18 -------------- lib/factory_bot_rails/railtie.rb | 13 ---------- .../active_record_validator_spec.rb | 24 ------------------- 4 files changed, 71 deletions(-) delete mode 100644 lib/factory_bot_rails/factory_validator/active_record_validator.rb delete mode 100644 spec/factory_bot_rails/factory_validator/active_record_validator_spec.rb diff --git a/README.md b/README.md index 4002df70..5687456c 100644 --- a/README.md +++ b/README.md @@ -152,22 +152,6 @@ rails generate factory_bot:model NAME [field:type field:type] [options] [default factory template]: https://github.com/thoughtbot/factory_bot_rails/tree/main/lib/generators/factory_bot/model/templates/factories.erb -### Active Record Configuration - -By default, FactoryBot will refuse to generate Active Record primary key -columns. Without additional configuration, an Active Record model treats a -column named `id` as its primary key. - -For example, defining an `id` attribute with `add_attribute(:id)`, `id { ... }`, -or `sequence(:id)` will raise a `FactoryBot::AttributeDefinitionError`. - -You can disable this behavior by adding the following to the appropriate environment configuration in `config/environments` -e.g. `config/environments/test.rb`: - -```ruby -config.factory_bot.reject_primary_key_attributes = false -``` - ## Contributing Please see [CONTRIBUTING.md](CONTRIBUTING.md). diff --git a/lib/factory_bot_rails/factory_validator/active_record_validator.rb b/lib/factory_bot_rails/factory_validator/active_record_validator.rb deleted file mode 100644 index 6ad2cc12..00000000 --- a/lib/factory_bot_rails/factory_validator/active_record_validator.rb +++ /dev/null @@ -1,18 +0,0 @@ -module FactoryBotRails - class FactoryValidator - class ActiveRecordValidator - def validate!(payload) - attributes, for_class = payload.values_at(:attributes, :class) - attributes.each do |attribute| - if for_class < ActiveRecord::Base && for_class.primary_key == attribute.name.to_s - raise FactoryBot::AttributeDefinitionError, <<~ERROR - Attribute generates #{for_class.primary_key.inspect} primary key for #{for_class.name}" - - Do not define #{for_class.primary_key.inspect}. Instead, rely on the database to generate it. - ERROR - end - end - end - end - end -end diff --git a/lib/factory_bot_rails/railtie.rb b/lib/factory_bot_rails/railtie.rb index de539cf2..0e7dfdb8 100644 --- a/lib/factory_bot_rails/railtie.rb +++ b/lib/factory_bot_rails/railtie.rb @@ -10,7 +10,6 @@ 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.set_fixture_replacement" do @@ -21,18 +20,6 @@ class Railtie < Rails::Railtie FactoryBot.definition_file_paths = definition_file_paths end - 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" - - config.validator.add_validator FactoryValidator::ActiveRecordValidator.new - end - end - end - config.after_initialize do |app| FactoryBot.find_definitions Reloader.new(app).run diff --git a/spec/factory_bot_rails/factory_validator/active_record_validator_spec.rb b/spec/factory_bot_rails/factory_validator/active_record_validator_spec.rb deleted file mode 100644 index 000476cc..00000000 --- a/spec/factory_bot_rails/factory_validator/active_record_validator_spec.rb +++ /dev/null @@ -1,24 +0,0 @@ -# frozen_string_literal: true - -describe FactoryBotRails::FactoryValidator do - before { FactoryBot.reload } - - describe "ActiveRecordValidator" do - context "when defined with a class that descends from ActiveRecord::Base" do - it "raises an error for a sequence generating its primary key" do - define_model "Article", an_id: :integer do - self.primary_key = :an_id - end - - FactoryBot.define do - factory :article do - sequence(:an_id) - end - end - - expect { FactoryBot.create(:article) } - .to raise_error(FactoryBot::AttributeDefinitionError) - end - end - end -end