Skip to content

Commit

Permalink
Exclude view templates from page collections/resources (SpinaCMS#769)
Browse files Browse the repository at this point in the history
* Add exclude_from to view_templates

* No more preview on hover

* Add exclude_from example to demo theme
  • Loading branch information
Bramjetten authored Aug 26, 2021
1 parent 96386f9 commit e3bc02f
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 29 deletions.
32 changes: 32 additions & 0 deletions app/components/spina/pages/new_page_button_component.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<% if view_templates.many? %>

<%= render Spina::UserInterface::DropdownComponent.new do |dropdown| %>
<% dropdown.button(classes: "btn btn-primary w-full") do %>
<%= helpers.heroicon("plus", style: :solid, class: "w-7 h-7 -ml-2") %>
<%=t 'spina.pages.new' %>
<% end %>

<% dropdown.menu do %>
<% view_templates.each do |template| %>
<%= link_to helpers.spina.new_admin_page_path(view_template: template.name, resource_id: @resource&.id), class: "block px-4 py-2 text-sm leading-5 text-gray-700 hover:bg-gray-100 hover:text-gray-900 focus:outline-none focus:bg-gray-100 focus:text-gray-900", data: {turbo_frame: "modal", action: "reveal#hide"} do %>
<div class="font-medium text-gray-700">
<%= template.title %>

<% if template.recommended %>
<span class="text-green-500 text-xs"><%=t 'spina.pages.recommended' %></span>
<% end %>
</div>
<div class="text-gray-400"><%= template.description %></div>
<% end %>
<% end %>
<% end %>
<% end %>

<% else %>

<%= link_to helpers.spina.new_admin_page_path(view_template: view_template.name, resource_id: resource&.id), class: "btn btn-primary w-full", data: {turbo_frame: "modal"} do %>
<%= helpers.heroicon("plus", style: :solid, class: "w-7 h-7 -ml-2") %>
<%=t 'spina.pages.new' %>
<% end %>

<% end %>
19 changes: 19 additions & 0 deletions app/components/spina/pages/new_page_button_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Spina::Pages
class NewPageButtonComponent < Spina::ApplicationComponent
attr_reader :view_templates, :resource

def initialize(view_templates = [], resource: nil)
@view_templates = view_templates
@resource = resource
end

def view_template
view_templates.first
end

def render?
view_templates.any?
end

end
end
3 changes: 1 addition & 2 deletions app/controllers/spina/admin/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ class PagesController < AdminController
def index
add_breadcrumb I18n.t('spina.website.pages'), spina.admin_pages_path


if params[:resource_id]
@resource = Resource.find(params[:resource_id])
@page_templates = Spina::Current.theme.new_page_templates(recommended: @resource.view_template)
@page_templates = Spina::Current.theme.new_page_templates(resource: @resource)
@pages = @resource.pages.active.roots.includes(:translations)
else
@pages = Page.active.sorted.roots.main.includes(:translations)
Expand Down
2 changes: 1 addition & 1 deletion app/views/spina/admin/pages/edit_template.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<%= Spina::Page.human_attribute_name :view_template %>
</label>

<%= f.select :view_template, options_for_select(current_theme.view_templates.map { |template| [template[:title], template[:name], {'data-page-parts' => template[:page_parts]}] }, @page.view_template), {}, class: "mt-1 form-select w-full" %>
<%= f.select :view_template, current_theme.new_page_templates(resource: @page.resource).map{ |template| [template.title, template.name] }, {}, class: "mt-1 form-select w-full" %>
</div>
</div>
</div>
Expand Down
22 changes: 1 addition & 21 deletions app/views/spina/admin/pages/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,7 @@
<% end %>

<div class="ml-3">
<%= render Spina::UserInterface::DropdownComponent.new do |dropdown| %>
<% dropdown.button(classes: "btn btn-primary w-full") do %>
<%= heroicon("plus", style: :solid, class: "w-7 h-7 -ml-2") %>
<%=t 'spina.pages.new' %>
<% end %>

<% dropdown.menu do %>
<% @page_templates.each do |template| %>
<%= link_to spina.new_admin_page_path(view_template: template.name, resource_id: @resource&.id), class: "block px-4 py-2 text-sm leading-5 text-gray-700 hover:bg-gray-100 hover:text-gray-900 focus:outline-none focus:bg-gray-100 focus:text-gray-900", data: {turbo_frame: "modal", action: "reveal#hide"} do %>
<div class="font-medium text-gray-700">
<%= template.title %>

<% if template.recommended %>
<span class="text-green-500 text-xs"><%=t 'spina.pages.recommended' %></span>
<% end %>
</div>
<div class="text-gray-400"><%= template.description %></div>
<% end %>
<% end %>
<% end %>
<% end %>
<%= render Spina::Pages::NewPageButtonComponent.new(@page_templates, resource: @resource) %>
</div>
<% end %>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
theme.view_templates = [
{name: 'homepage', title: 'Homepage', parts: %w(headline body image_collection)},
{name: 'show', title: 'Default', parts: %w(body image repeater)},
{name: 'demo', title: 'Demo', parts: %w(body image_collection image repeater)}
{name: 'demo', title: 'Demo', parts: %w(body image_collection image repeater), exclude_from: %w(articles)}
]

# Custom pages
Expand Down
6 changes: 4 additions & 2 deletions lib/spina/theme.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,17 @@ def initialize
@public_theme = false
end

def new_page_templates(recommended: "")
def new_page_templates(resource: nil)
page_collection = resource&.name || "main"
@view_templates.map do |view_template|
next if is_custom_undeletable_page?(view_template[:name])
next if view_template[:exclude_from]&.include?(page_collection)

OpenStruct.new({
name: view_template[:name],
title: view_template[:title],
description: view_template[:description],
recommended: view_template[:name] == recommended
recommended: view_template[:name] == resource&.view_template
})
end.compact.sort_by do |page_template|
[page_template.recommended ? 0 : 1]
Expand Down
6 changes: 4 additions & 2 deletions test/dummy/config/initializers/themes/demo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,14 @@
name: 'demo',
title: 'Demo',
description: 'Example including all parts',
parts: ['repeater', 'repeater2', 'attachment', 'option', 'body', 'image_collection', 'image', 'portrait', 'landscape', 'wide']
parts: ['repeater', 'repeater2', 'attachment', 'option', 'body', 'image_collection', 'image', 'portrait', 'landscape', 'wide'],
exclude_from: %w(guides)
}, {
name: 'blogpost',
title: "Blogpost",
description: 'Article template',
parts: ['body']
parts: ['body'],
exclude_from: %w(main)
}]

theme.custom_pages = [{
Expand Down

0 comments on commit e3bc02f

Please sign in to comment.