diff --git a/lib/rom/factory/dsl.rb b/lib/rom/factory/dsl.rb index b580999..970fb56 100644 --- a/lib/rom/factory/dsl.rb +++ b/lib/rom/factory/dsl.rb @@ -77,17 +77,15 @@ def call # @param [Symbol] The name of the registered builder # # @api public - def create(name, *args) - _factories[name, *args] - end + def create(name, *args) = _factories[name, *args] # Create a sequence attribute # # @param [Symbol] name The attribute name # # @api private - def sequence(meth, &block) - define_sequence(meth, block) if _valid_names.include?(meth) + def sequence(meth, &) + define_sequence(meth, &) if _valid_names.include?(meth) end # Set timestamp attributes @@ -169,18 +167,22 @@ def trait(name, parents = [], &) # @option options [Array] traits Traits to apply to the association # # @api public - def association(name, *traits, **options) + def association(name, *seq_traits, traits: EMPTY_ARRAY, **options) assoc = _relation.associations[name] if assoc.is_a?(::ROM::SQL::Associations::OneToOne) && options.fetch(:count, 1) > 1 ::Kernel.raise ::ArgumentError, "count cannot be greater than 1 on a OneToOne" end - traits = options.fetch(:traits, EMPTY_ARRAY) if traits.empty? - builder = -> { _factories.for_relation(assoc.target) } - _attributes << attributes::Association.new(assoc, builder, *traits, **options) + _attributes << attributes::Association.new( + assoc, + builder, + *seq_traits, + *traits, + **options + ) end # @api private @@ -204,8 +206,8 @@ def respond_to_missing?(method_name, include_private = false) end # @api private - def define_sequence(name, block) - _attributes << attributes::Callable.new(name, self, attributes::Sequence.new(name, &block)) + def define_sequence(name, &) + _attributes << attributes::Callable.new(name, self, attributes::Sequence.new(name, &)) end # @api private @@ -219,9 +221,7 @@ def define_attr(name, *args, &block) end # @api private - def attributes - ::ROM::Factory::Attributes - end + def attributes = ::ROM::Factory::Attributes end end end diff --git a/spec/integration/rom/factory_spec.rb b/spec/integration/rom/factory_spec.rb index a21eb3f..6338746 100644 --- a/spec/integration/rom/factory_spec.rb +++ b/spec/integration/rom/factory_spec.rb @@ -1029,35 +1029,61 @@ class Admin < ROM::Struct context "using associations" do context "with traits" do - before do - factories.define(:user) do |f| - f.first_name "Jane" - f.last_name "Doe" - f.email "jane@doe.org" - f.timestamps - f.association(:tasks, :important, count: 2) - end + context "sequential" do + before do + factories.define(:user) do |f| + f.first_name "Jane" + f.last_name "Doe" + f.email "jane@doe.org" + f.timestamps + f.association(:tasks, :important, count: 2) + end - factories.define(:task) do |f| - f.sequence(:title) { |n| "Task #{n}" } - f.trait :important do |t| - t.sequence(:title) { |n| "Important Task #{n}" } + factories.define(:task) do |f| + f.sequence(:title) { |n| "Task #{n}" } + f.trait :important do |t| + t.sequence(:title) { |n| "Important Task #{n}" } + end end end - end - it "creates associated records with the given trait" do - user = factories[:user] + it "creates associated records with the given trait" do + user = factories[:user] - expect(user.tasks.count).to be(2) + expect(user.tasks.count).to be(2) - t1, t2 = user.tasks + t1, t2 = user.tasks - expect(t1.user_id).to be(user.id) - expect(t1.title).to eql("Important Task 1") + expect(t1.user_id).to be(user.id) + expect(t1.title).to eql("Important Task 1") + + expect(t2.user_id).to be(user.id) + expect(t2.title).to eql("Important Task 2") + end + end + + context "keyword" do + before do + factories.define(:user) do |f| + f.first_name "Jane" + f.last_name "Doe" + f.email "jane@doe.org" + f.timestamps + f.association(:tasks, count: 2, traits: [:important]) + end - expect(t2.user_id).to be(user.id) - expect(t2.title).to eql("Important Task 2") + factories.define(:task) do |f| + f.sequence(:title) { |n| "Task #{n}" } + f.trait :important do |t| + t.sequence(:title) { |n| "Important Task #{n}" } + end + end + end + + it "creates associated records with the given trait" do + user = factories[:user] + expect(user.tasks.count).to be(2) + end end end