Skip to content

Commit

Permalink
Make sure essences have their contents on initialization
Browse files Browse the repository at this point in the history
If we do not pass the content when building an essence,
`after_initialize` hooks do not have access to the content definitions.
  • Loading branch information
mamhoff committed Apr 8, 2021
1 parent e50e2ac commit ef24726
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/models/alchemy/content/factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
21 changes: 21 additions & 0 deletions spec/models/alchemy/essence_headline_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit ef24726

Please sign in to comment.