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

Reject sequence definitions for Active Record primary keys #419

Merged
merged 1 commit into from
Sep 5, 2023

Commits on Sep 5, 2023

  1. Reject sequence definitions for Active Record primary keys

    Alternative to [thoughtbot/factory_bot#1586][]
    Depends on [thoughtbot/factory_bot#1587][]
    
    First, introduce the `FactoryBotRails::FactoryValidator` class to serve
    as a generic `factory_bot.compile_factory` event observer.
    
    Throughout the lifecycle of the `FactoryBotRails::Railtie`, afford
    various initializers with opportunities to add purpose-built validators
    to the instance's internal set.
    
    When `factory_bot.compile_factory` events are published, iterate through
    the list of validators and forward along the value of the
    [event.payload][] to the validator's `#validate!` method.
    
    Next, introduce the Active Record-specific
    `FactoryBotRails::FactoryValidator::ActiveRecordValidator` class. Only
    require the module whenever [Active Record's engine is loaded][on_load].
    
    The `ActiveRecordValidator#validate!` method rejects attributes that
    define primary key generation logic for `ActiveRecord::Base`
    descendants.
    
    In order to test this behavior, add a development dependency on
    `sqlite3` and `activerecord`, along with some model and database table
    generating helper methods.
    
    [thoughtbot/factory_bot#1586]: thoughtbot/factory_bot#1586
    [thoughtbot/factory_bot#1587]: thoughtbot/factory_bot#1587
    [event.payload]: module-ActiveSupport::Notifications-label-Subscribers
    [on_load]: https://guides.rubyonrails.org/engines.html#avoid-loading-rails-frameworks
    seanpdoyle committed Sep 5, 2023
    Configuration menu
    Copy the full SHA
    b965bb8 View commit details
    Browse the repository at this point in the history