Skip to content

Commit

Permalink
Remove the option to have a parent page for resources - Fixes SpinaCM…
Browse files Browse the repository at this point in the history
…S#565 (SpinaCMS#568)

* Remove the option to have a parent page for resources - Fixes SpinaCMS#565
  • Loading branch information
Bramjetten authored Jun 23, 2020
1 parent ebcd144 commit b7548d6
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 39 deletions.
2 changes: 1 addition & 1 deletion app/controllers/spina/admin/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def index

def new
@resource = Resource.find_by(id: params[:resource_id])
@page = Page.new(resource: @resource, parent: Page.find_by(id: params[:parent_id]) || @resource&.parent_page)
@page = Page.new(resource: @resource, parent: Page.find_by(id: params[:parent_id]))
add_index_breadcrumb
if current_theme.new_page_templates.any? { |template| template[0] == params[:view_template] }
@page.view_template = params[:view_template]
Expand Down
7 changes: 7 additions & 0 deletions app/models/spina/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ class Page < ApplicationRecord
scope :live, -> { active.where(draft: false) }
scope :in_menu, -> { where(show_in_menu: true) }

# Copy resource from parent
before_save :set_resource_from_parent, if: -> { parent.present? }

# Save children to update all materialized_paths
after_save :save_children
after_save :touch_navigations
Expand Down Expand Up @@ -97,6 +100,10 @@ def view_template_page_parts(theme)

private

def set_resource_from_parent
self.resource_id = parent.resource_id
end

def touch_navigations
navigations.update_all(updated_at: Time.zone.now)
end
Expand Down
14 changes: 1 addition & 13 deletions app/models/spina/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ module Spina
class Resource < ApplicationRecord
has_many :pages, dependent: :restrict_with_exception

belongs_to :parent_page, class_name: "Spina::Page", optional: true

after_save :scope_pages_to_parent_page

def pages
case order_by
when "title"
Expand All @@ -14,14 +10,6 @@ def pages
super.order(created_at: :desc)
end
end

private

def scope_pages_to_parent_page
pages.roots.each do |root_page|
root_page.update(parent: parent_page)
end
end


end
end
26 changes: 14 additions & 12 deletions app/views/spina/admin/pages/_form_advanced.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@
= f.text_field :link_url, placeholder: Spina::Page.human_attribute_name(:link_url_placeholder)

.well
.horizontal-form-group
.horizontal-form-label
= Spina::Page.human_attribute_name :ancestry
.horizontal-form-content
.select-dropdown.ancestry
= f.select :parent_id, page_ancestry_options(f.object), include_blank: Spina::Page.human_attribute_name(:no_parent)
- unless @page.resource.present?
.horizontal-form-group
.horizontal-form-label
= Spina::Page.human_attribute_name :ancestry
.horizontal-form-content
.select-dropdown.ancestry
= f.select :parent_id, page_ancestry_options(f.object), include_blank: Spina::Page.human_attribute_name(:no_parent)

.horizontal-form-group{style: ('display: none' if @page.custom_page?)}
.horizontal-form-label
Expand All @@ -49,9 +50,10 @@
- options = options_for_select(current_theme.view_templates.map { |template| [template[:title], template[:name], {'data-page-parts' => template[:page_parts]}] }, @page.view_template)
= f.select :view_template, options

.horizontal-form-group
.horizontal-form-label
= Spina::Page.human_attribute_name :resource
.horizontal-form-content
.select-dropdown
= f.select :resource_id, Spina::Resource.order(:label).pluck(:label, :id), include_blank: t('spina.website.pages')
- if @page.root?
.horizontal-form-group
.horizontal-form-label
= Spina::Page.human_attribute_name :resource
.horizontal-form-content
.select-dropdown
= f.select :resource_id, Spina::Resource.order(:label).pluck(:label, :id), include_blank: t('spina.website.pages')
7 changes: 0 additions & 7 deletions app/views/spina/admin/resources/edit.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,3 @@
.select-dropdown
- options = options_for_select(current_theme.view_templates.map { |template| [template[:title], template[:name]] }, @resource.view_template)
= f.select :view_template, options, include_blank: true

.horizontal-form-group
.horizontal-form-label
= Spina::Resource.human_attribute_name :parent_page
.horizontal-form-content
.select-dropdown
= f.select :parent_page_id, page_ancestry_options(Spina::Page.new), include_blank: Spina::Resource.human_attribute_name(:no_parent)
2 changes: 1 addition & 1 deletion app/views/spina/admin/resources/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
.well
.dd#pages_list
%ol.dd-list
- (@resource.parent_page.try(:children) || @resource.pages.roots).each do |page|
- @resource.pages.roots.each do |page|
%li.dd-item{data: {controller: "page-collapse", url: spina.children_admin_page_path(page)}}
.dd-item-inner
- if page.children.any?
Expand Down
2 changes: 0 additions & 2 deletions db/migrate/11_create_spina_resources.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ def change
t.string :name, null: false, unique: true
t.string :label
t.string :view_template
t.integer :parent_page_id
t.string :order_by
t.timestamps
end
add_column :spina_pages, :resource_id, :integer, null: true
add_index :spina_pages, :resource_id
add_index :spina_resources, :parent_page_id
end
end
3 changes: 0 additions & 3 deletions docs/themes/5_resources.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,3 @@ Every resource can have the following attributes:
- label
- view_template
- order_by
- parent_page_id

When defining a parent page, all pages within that resource will be scoped to that parent page. This means that all generated URL's will be prefixed with the parent page's URL. An example: you can create a regular page called `blog` and have a resource called `blogposts`. Your blog view template could then list all pages that are inside the blog resource. In Spina you would have a nice separate menu called "Blogposts" where you can easily manage a list of blogposts.

0 comments on commit b7548d6

Please sign in to comment.