Skip to content

Commit

Permalink
Refactor DSL
Browse files Browse the repository at this point in the history
  • Loading branch information
flash-gordon committed Jan 21, 2025
1 parent e8a1600 commit ad803ca
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 35 deletions.
28 changes: 14 additions & 14 deletions lib/rom/factory/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -169,18 +167,22 @@ def trait(name, parents = [], &)
# @option options [Array<Symbol>] 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
Expand All @@ -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
Expand All @@ -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
68 changes: 47 additions & 21 deletions spec/integration/rom/factory_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 "[email protected]"
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 "[email protected]"
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 "[email protected]"
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

Expand Down

0 comments on commit ad803ca

Please sign in to comment.