Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow instantiating a content on an unpersisted valid element #2056

Merged
merged 2 commits into from
Mar 26, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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