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

Create new contents on demand #2049

Merged
merged 2 commits into from
Mar 22, 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
25 changes: 25 additions & 0 deletions app/decorators/alchemy/element_editor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ def to_partial_path
"alchemy/admin/elements/element"
end

# Returns content editor instances for defined contents
#
# Creates contents on demand if the content is not yet present on the element
#
# @return Array<Alchemy::ContentEditor>
def contents
element.definition.fetch(:contents, []).map do |content|
Alchemy::ContentEditor.new(find_or_create_content(content[:name]))
end
end

# CSS classes for the element editor partial.
def css_classes
[
Expand Down Expand Up @@ -78,5 +89,19 @@ def deprecation_notice
default: Alchemy.t(:element_deprecated))
end
end

private

def find_or_create_content(name)
find_content(name) || create_content(name)
end

def find_content(name)
element.contents.find { |content| content.name == name }
end

def create_content(name)
Alchemy::Content.create(element: element, name: name)
end
end
end
2 changes: 1 addition & 1 deletion app/views/alchemy/admin/elements/_element.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<div id="element_<%= element.id %>_errors" class="element_errors"></div>

<div id="element_<%= element.id %>_content" class="element-content-editors">
<%= render element.contents.map { |content| Alchemy::ContentEditor.new(content) } %>
<%= render element.contents %>
</div>

<% if element.taggable? %>
Expand Down
38 changes: 38 additions & 0 deletions spec/decorators/alchemy/element_editor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,44 @@
end
end

describe "#contents" do
let(:element) { create(:alchemy_element, :with_contents, name: "headline") }

subject(:contents) { element_editor.contents }

it "returns a ContentEditor instance for each content defined" do
aggregate_failures do
contents.each do |content|
expect(content).to be_an(Alchemy::ContentEditor)
end
end
end

context "with a content defined but not existing yet" do
before do
expect(element).to receive(:definition).at_least(:once) do
{
name: "headline",
contents: [
{
name: "headline",
type: "EssenceText",
},
{
name: "foo",
type: "EssenceText",
},
],
}.with_indifferent_access
end
end

it "creates the missing content" do
expect { subject }.to change { element.contents.count }.by(1)
end
end
end

describe "#to_partial_path" do
subject { element_editor.to_partial_path }

Expand Down