Skip to content

Commit

Permalink
Merge pull request #2056 from mamhoff/element-without-ids
Browse files Browse the repository at this point in the history
Allow instantiating a content on an unpersisted valid element
  • Loading branch information
tvdeyen authored Mar 26, 2021
2 parents 1a9380d + abcaf18 commit 365ea46
Showing 1 changed file with 15 additions and 17 deletions.
32 changes: 15 additions & 17 deletions app/models/alchemy/content/factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def new(attributes = {})
super(
name: definition[:name],
essence_type: normalize_essence_type(definition[:type]),
element_id: element.id
element: element
).tap(&:build_essence)
end

Expand All @@ -53,18 +53,16 @@ def create(attributes = {})
# @copy.element_id # => 3
#
def copy(source, differences = {})
new_content = Content.new(
Content.new(
source.attributes.
except(*SKIPPED_ATTRIBUTES_ON_COPY).
merge(differences.with_indifferent_access),
)
new_essence = source.essence.class.create!(
source.essence.attributes.
except(*SKIPPED_ATTRIBUTES_ON_COPY),
)
new_content.tap do |content|
content.essence = new_essence
content.save
merge(differences.with_indifferent_access)
).tap do |new_content|
new_content.build_essence(
source.essence.attributes.
except(*SKIPPED_ATTRIBUTES_ON_COPY)
)
new_content.save
end
end

Expand Down Expand Up @@ -117,18 +115,18 @@ def definition
#
# If an optional type is passed, this type of essence gets created.
#
def build_essence(type = essence_type)
self.essence = essence_class(type).new({
ingredient: default_value,
})
def build_essence(attributes = {})
self.essence = essence_class(essence_type).new(
{ ingredient: default_value }.merge(attributes)
)
end

# Creates essence from definition.
#
# If an optional type is passed, this type of essence gets created.
#
def create_essence!(type = nil)
build_essence(type).save!
def create_essence!(attrs = {})
build_essence(attrs).save!
save!
end

Expand Down

0 comments on commit 365ea46

Please sign in to comment.