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 copying contents when they're not in the elements.yml #2068

Merged
merged 1 commit into from
Apr 15, 2021
Merged
Show file tree
Hide file tree
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
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