Skip to content

Conversation

kakubin
Copy link

@kakubin kakubin commented May 6, 2024

New feature useful in the following case

ActiveRecord::Schema.define do
  create_table :todo_lists do; end

  create_table :todo_items do |t|
    t.references :todo_list, null: false
    t.integer :priority, default: 1, null: false
    t.index [:todo_list_id, :priority], unique: true
  end
end

FactoryBot.define do
  factory :todo_item do
    scoped_sequence(:priority, :todo_list_id)
  end
end

attributes_for(:todo_item, todo_list_id: 1) # => {:todo_list_id=>1, :priority=>1}
attributes_for(:todo_item, todo_list_id: 1) # => {:todo_list_id=>1, :priority=>2} # generate attribute value `priority` sequentially
attributes_for(:todo_item, todo_list_id: 2) # => {:todo_list_id=>2, :priority=>1} # when value of scope attribute is different, another sequential number is generated.
attributes_for(:todo_item, todo_list_id: 1) # => {:todo_list_id=>1, :priority=>3}

@kakubin kakubin requested a review from mike-burns as a code owner May 6, 2024 11:19
@kakubin
Copy link
Author

kakubin commented Jun 12, 2024

https://github.com/kakubin/factory_bot-scoped_sequence

@kakubin kakubin closed this Jun 12, 2024
@kakubin kakubin deleted the scoped_sequence branch June 12, 2024 13:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant