Skip to content

Commit

Permalink
Fix using passed values in callable attributes
Browse files Browse the repository at this point in the history
It turned out it's not always possible to use the passed value in a block. Passing values requires more attention, there are open issues about it, but it's a nice improvement anyway
  • Loading branch information
flash-gordon committed May 11, 2024
1 parent 1ff1ce6 commit 1463c30
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/rom/factory/tuple_evaluator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,9 @@ def evaluate(traits, attrs, opts)

# @api private
def evaluate_values(attrs)
attributes.values.tsort.each_with_object({}) do |attr, h|
deps = attr.dependency_names.map { |k| h[k] }.compact
result = attr.(attrs, *deps)
attributes.values.tsort.each_with_object(attrs.dup) do |attr, h|
deps = attr.dependency_names.filter_map { |k| h[k] }
result = attr.(h, *deps)

if result
h.update(result)
Expand All @@ -162,7 +162,7 @@ def evaluate_values(attrs)
end

def evaluate_traits(trait_list, attrs, opts)
return {} if trait_list.empty?
return EMPTY_HASH if trait_list.empty?

traits = trait_list.map { |v| v.is_a?(Hash) ? v : {v => true} }.reduce(:merge)

Expand Down
37 changes: 37 additions & 0 deletions spec/integration/rom/factory_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,43 @@

expect(user.email).to eql("#{user.first_name}.#{user.last_name}@test-1.org")
end

it "can use passed values in the block" do
factories.define(:user, relation: :users) do |f|
f.last_name { fake(:name) }
f.email { |first_name, last_name| "#{first_name}.#{last_name}@example.com" }
f.timestamps
end

user = factories[:user, first_name: "Jane"]

expect(user.email).to eql("Jane.#{user.last_name}@example.com")
end

it "can use passed values in the block" do
factories.define(:user, relation: :users) do |f|
f.first_name nil
f.last_name { fake(:name) }
f.email { |first_name| "#{first_name}@example.com" }
f.timestamps
end

user = factories[:user, first_name: "Jane"]

expect(user.email).to eql("Jane@example.com")
end

it "can use passed values in the block" do
factories.define(:user, relation: :users) do |f|
f.last_name { fake(:name) }
f.email { |first_name| "#{first_name}@example.com" }
f.timestamps
end

user = factories[:user, first_name: "Jane"]

expect(user.email).to eql("Jane@example.com")
end
end

context "changing values of dependant attributes" do
Expand Down

0 comments on commit 1463c30

Please sign in to comment.