Skip to content

Commit

Permalink
wip - support building project child structs
Browse files Browse the repository at this point in the history
  • Loading branch information
solnic committed Nov 24, 2023
1 parent 858758e commit 0bd6ed6
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 29 deletions.
48 changes: 25 additions & 23 deletions lib/rom/factory/attributes/association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ def value?
def factories
builder.factories
end

# @api private
def foreign_key
assoc.foreign_key
end

# @api private
def count
options.fetch(:count, 1)
end
end

# @api private
Expand All @@ -69,11 +79,6 @@ def call(attrs, persist: true)
assoc.associate(tuple, struct)
end
end

# @api private
def foreign_key
assoc.foreign_key
end
end

# @api private
Expand All @@ -100,11 +105,6 @@ def call(attrs = EMPTY_HASH, parent, persist: true)
def dependency?(rel)
assoc.source == rel
end

# @api private
def count
options.fetch(:count)
end
end

# @api private
Expand All @@ -129,11 +129,6 @@ def call(attrs = EMPTY_HASH, parent, persist: true)

{name => struct}
end

# @api private
def count
options.fetch(:count, 1)
end
end

class ManyToMany < Core
Expand All @@ -151,14 +146,22 @@ def call(attrs = EMPTY_HASH, parent, persist: true)
end

# Delegate to through factory if it exists
if through_factory? && persist
structs.each do |child|
factories[through_factory_name, user: parent, address: child]
if persist
if through_factory?
structs.each do |child|
factories[through_factory_name, user: parent, address: child]
end
else
assoc.persist([parent], structs)
end

{name => result(structs)}
else
assoc.persist([parent], structs) if persist
result(structs)
end
end

def result(structs)
{name => structs}
end

Expand All @@ -180,16 +183,15 @@ def through_factory_name

private

def count
options.fetch(:count, 1)
end

def tpk
assoc.target.primary_key
end
end

class OneToOneThrough < ManyToMany
def result(structs)
{name => structs[0]}
end
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion lib/rom/factory/builder/persistable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ def initialize(builder, relation = builder.relation)

# @api private
def create(*traits, **attrs)
tuple = tuple(*traits, **attrs)
validate_keys(traits, attrs)

tuple = tuple(*traits, **attrs)
persisted = persist(tuple)

if tuple_evaluator.has_associations?(traits)
Expand Down
14 changes: 13 additions & 1 deletion lib/rom/factory/tuple_evaluator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ def struct(*traits, **attrs)
attributes.merge!(materialized_callables)

associations = assoc_names
.map { |key| [key, attributes[key]] if attributes.key?(key) }
.map { |key|
if (assoc = @attributes[key]) && assoc.count.positive?
[key, build_assoc_attrs(key, attributes[relation.primary_key], attributes[key])]
end
}
.compact
.to_h

Expand All @@ -62,6 +66,14 @@ def struct(*traits, **attrs)
model.new(attributes)
end

def build_assoc_attrs(key, fk, value)
if value.is_a?(Array)
value.map { |el| build_assoc_attrs(key, fk, el) }
else
{attributes[key].foreign_key => fk}.merge(value.to_h)
end
end

# @api private
def persist_associations(tuple, parent, traits = [])
assoc_names(traits).each do |name|
Expand Down
4 changes: 0 additions & 4 deletions spec/integration/rom/factory_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,17 +137,13 @@

context "when building a struct" do
it "persists the relation properly with pre-existing assoc record" do
skip "TODO: This does not work, cannot figure out why"

address = factories.structs[:address]
user = factories.structs[:user, address: address]

expect(user.address).to have_attributes(full_address: "123 Elm St.")
end

it "persists the relation properly without pre-existing assoc record" do
skip "TODO: This does not work, cannot figure out why"

user = factories.structs[:user]

expect(user.address).to have_attributes(full_address: "123 Elm St.")
Expand Down

0 comments on commit 0bd6ed6

Please sign in to comment.