diff --git a/app/models/alchemy/content/factory.rb b/app/models/alchemy/content/factory.rb index 44c197125a..4c1f7ff253 100644 --- a/app/models/alchemy/content/factory.rb +++ b/app/models/alchemy/content/factory.rb @@ -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 @@ -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 @@ -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