From ef2472631ee1ba7c9f534703274e9ee8ed9e45c9 Mon Sep 17 00:00:00 2001 From: Martin Meyerhoff Date: Thu, 8 Apr 2021 09:38:14 +0200 Subject: [PATCH] Make sure essences have their contents on initialization If we do not pass the content when building an essence, `after_initialize` hooks do not have access to the content definitions. --- app/models/alchemy/content/factory.rb | 2 +- spec/models/alchemy/essence_headline_spec.rb | 21 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/app/models/alchemy/content/factory.rb b/app/models/alchemy/content/factory.rb index 4c1f7ff253..444eb84932 100644 --- a/app/models/alchemy/content/factory.rb +++ b/app/models/alchemy/content/factory.rb @@ -117,7 +117,7 @@ def definition # def build_essence(attributes = {}) self.essence = essence_class(essence_type).new( - { ingredient: default_value }.merge(attributes) + { content: self, ingredient: default_value }.merge(attributes) ) end diff --git a/spec/models/alchemy/essence_headline_spec.rb b/spec/models/alchemy/essence_headline_spec.rb index 5d02ae9a78..c924330cee 100644 --- a/spec/models/alchemy/essence_headline_spec.rb +++ b/spec/models/alchemy/essence_headline_spec.rb @@ -66,4 +66,25 @@ end end end + + describe "creating from a content" do + it "should have the size and level fields filled with correct defaults" do + element = create(:alchemy_element) + + allow(element).to receive(:content_definition_for) do + { + "name" => "headline", + "type" => "EssenceHeadline", + "settings" => { + "sizes" => [3], + "levels" => [2, 3], + }, + }.with_indifferent_access + end + + content = Alchemy::Content.create(element: element, name: "headline") + expect(content.essence.size).to eq(3) + expect(content.essence.level).to eq(2) + end + end end