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

Add hoc sequences in create_list #1650

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions lib/factory_bot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
require "factory_bot/decorator/new_constructor"
require "factory_bot/linter"
require "factory_bot/version"
require "factory_bot/attribute_sequence"

module FactoryBot
Deprecation = ActiveSupport::Deprecation.new("7.0", "factory_bot")
Expand Down
24 changes: 24 additions & 0 deletions lib/factory_bot/attribute_sequence.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module FactoryBot
# @api private
class AttributeSequence
attr_reader :expression

def initialize(&block)
@expression = block
end

def evaluate(index)
expression.call(index)
Copy link
Contributor

@smaboshe smaboshe Oct 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am curious to know what is the result if expression is nil? What is the default behaviour if a block is not given?

end

def self.evaluate_attributes(traits_and_overrides, i)
traits_and_overrides.map do |attribute|
next attribute unless attribute.is_a?(Hash)

attribute.transform_values do |value|
value.is_a?(self) ? value.evaluate(i) : value
end
end
end
end
end
4 changes: 3 additions & 1 deletion lib/factory_bot/strategy_syntax_method_registrar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ def define_list_strategy_method

Array.new(amount) do |i|
block_with_index = StrategySyntaxMethodRegistrar.with_index(block, i)
send(strategy_name, name, *traits_and_overrides, &block_with_index)
traits_and_evaluated_overrides =
FactoryBot::AttributeSequence.evaluate_attributes(traits_and_overrides, i)
send(strategy_name, name, *traits_and_evaluated_overrides, &block_with_index)
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions lib/factory_bot/syntax/methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ def generate_list(name, count)
Internal.sequence_by_name(name).next
end
end

def build_sequence(&block)
FactoryBot::AttributeSequence.new(&block)
end
end
end
end
10 changes: 10 additions & 0 deletions spec/acceptance/create_list_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@
end
end

context "with sequencial attributes" do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
context "with sequencial attributes" do
context "with sequential attributes" do

subject { FactoryBot.create_list(:post, 20, title: FactoryBot.build_sequence{ |n| "title_#{n}" }) }

it "create sequencial attribute values" do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
it "create sequencial attribute values" do
it "creates sequential attribute values" do

subject.each_with_index do |record, i|
expect(record.title).to eq "title_#{i}"
end
end
end

context "with a block" do
subject do
FactoryBot.create_list(:post, 20, title: "The Listing of the Block") do |post|
Expand Down