Skip to content

Commit

Permalink
Allow copying contents when they're not in the elements.yml (AlchemyC…
Browse files Browse the repository at this point in the history
…MS#2068)

When changing the elements.yml, we will often still have contents from
previous iterations of that file in the database. Copying must still
work.
  • Loading branch information
mamhoff authored and robinboening committed Jul 2, 2021
1 parent dcd426b commit edf44a8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
16 changes: 7 additions & 9 deletions app/models/alchemy/content/factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ def new(attributes = {})
return super if attributes.empty? || element.nil?

definition = element.content_definition_for(attributes[:name])
if definition.blank?
if definition.blank? && attributes[:essence_type].nil?
raise ContentDefinitionError, "No definition found in elements.yml for #{attributes.inspect} and #{element.inspect}"
end

super(
name: definition[:name],
essence_type: normalize_essence_type(definition[:type]),
name: attributes[:name],
essence_type: attributes[:essence_type] || normalize_essence_type(definition[:type]),
element: element
).tap(&:build_essence)
end
Expand Down Expand Up @@ -116,7 +116,7 @@ def definition
# If an optional type is passed, this type of essence gets created.
#
def build_essence(attributes = {})
self.essence = essence_class(essence_type).new(
self.essence = essence_class.new(
{ content: self, ingredient: default_value }.merge(attributes)
)
end
Expand All @@ -132,12 +132,10 @@ def create_essence!(attrs = {})

private

# Returns a class constant from definition's type field.
# Returns a class constant from definition's type field or the essence_type column
#
# If an optional type is passed, this type of essence gets constantized.
#
def essence_class(type = nil)
Content.normalize_essence_type(type || definition["type"]).constantize
def essence_class
(essence_type || Content.normalize_essence_type(definition["type"])).constantize
end
end
end
12 changes: 12 additions & 0 deletions spec/models/alchemy/content_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,18 @@ module Alchemy
expect(subject.essence).to be_instance_of(EssenceText)
expect(subject.essence).to_not be_persisted
end

context "when given an essence_type attribute that is not in the definition" do
let(:arguments) { { element: element, essence_type: "Alchemy::EssenceText", name: "not_in_elements_yml" } }

subject { Content.new(arguments) }

it "does not raise an error" do
expect { subject }.not_to raise_exception
expect(subject.name).to eq("not_in_elements_yml")
expect(subject.essence).to be_a(Alchemy::EssenceText)
end
end
end

describe ".create" do
Expand Down

0 comments on commit edf44a8

Please sign in to comment.