From 9eb591e6814012e0be44bf1e2d8d1b7f70432730 Mon Sep 17 00:00:00 2001 From: Martin Meyerhoff Date: Thu, 15 Apr 2021 19:07:15 +0200 Subject: [PATCH] Allow copying contents when they're not in the elements.yml (#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. --- app/models/alchemy/content/factory.rb | 16 +++++++--------- spec/models/alchemy/content_spec.rb | 12 ++++++++++++ 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/app/models/alchemy/content/factory.rb b/app/models/alchemy/content/factory.rb index 7844a89b2e..f349b67cd7 100644 --- a/app/models/alchemy/content/factory.rb +++ b/app/models/alchemy/content/factory.rb @@ -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 @@ -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 @@ -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 diff --git a/spec/models/alchemy/content_spec.rb b/spec/models/alchemy/content_spec.rb index 187f905542..51bdd83048 100644 --- a/spec/models/alchemy/content_spec.rb +++ b/spec/models/alchemy/content_spec.rb @@ -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