Skip to content

Commit

Permalink
Make sure an essence knows its content when being copied
Browse files Browse the repository at this point in the history
When copying an Essence, it needs to know its content - otherwise it
can't figure out what options it has.
  • Loading branch information
mamhoff committed Mar 26, 2021
1 parent dd20104 commit 3197901
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions app/models/alchemy/content/factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 3197901

Please sign in to comment.