Skip to content

Commit

Permalink
Remove layout root pages (AlchemyCMS#1812)
Browse files Browse the repository at this point in the history
* Remove layout root page

We do not need this hidden page. All pages that share the same language and have `layoutpage` set to true are the layoutpages for the current language.

* Pass the language in new page form

We want to be sure that the language we instantiate the page with gets passed with the form

* Only set the language from parent if present

And use the current page, not the default (that is the current anyway if no current language has been explicitely set).

* Add upgrader for removing layout root pages
  • Loading branch information
tvdeyen authored May 5, 2020
1 parent 9a67a36 commit 12c97fe
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 162 deletions.
2 changes: 1 addition & 1 deletion app/controllers/alchemy/admin/layoutpages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class LayoutpagesController < Alchemy::Admin::BaseController
helper Alchemy::Admin::PagesHelper

def index
@layout_root = Page.find_or_create_layout_root_for(@current_language.id)
@layout_pages = Page.layoutpages.where(language: @current_language)
@languages = Language.on_current_site
end

Expand Down
9 changes: 2 additions & 7 deletions app/models/alchemy/language.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ class Language < BaseRecord
throw(:abort)
end

scope :published, -> { where(public: true) }
scope :with_root_page, -> { joins(:pages).where(Page.table_name => {language_root: true}) }
scope :published, -> { where(public: true) }
scope :with_root_page, -> { joins(:pages).where(Page.table_name => { language_root: true }) }

class << self
def on_site(site)
Expand Down Expand Up @@ -110,11 +110,6 @@ def root_page
@root_page ||= pages.language_roots.first
end

# Layout root page
def layout_root_page
@layout_root_page ||= Page.layout_root_for(id)
end

# All available locales matching this language
#
# Matching either the code (+language_code+ + +country_code+) or the +language_code+
Expand Down
28 changes: 5 additions & 23 deletions app/models/alchemy/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class Page < BaseRecord
validates_presence_of :language, on: :create, unless: :root
validates_presence_of :page_layout, unless: :systempage?
validates_format_of :page_layout, with: /\A[a-z0-9_-]+\z/, unless: -> { systempage? || page_layout.blank? }
validates_presence_of :parent_id, if: proc { Page.count > 1 }
validates_presence_of :parent_id, if: proc { Page.count > 1 }, unless: -> { layoutpage? }

before_save :set_language_code,
if: -> { language.present? },
Expand All @@ -137,8 +137,8 @@ class Page < BaseRecord
before_save :set_fixed_attributes,
if: -> { fixed_attributes.any? }

before_create :set_language_from_parent_or_default,
if: -> { language_id.blank? },
before_create :set_language,
if: -> { language.nil? },
unless: :systempage?

after_update :create_legacy_url,
Expand Down Expand Up @@ -218,24 +218,6 @@ def copy(source, differences = {})
end
end

def layout_root_for(language_id)
where({ parent_id: Page.root.id, layoutpage: true, language_id: language_id }).limit(1).first
end

def find_or_create_layout_root_for(language_id)
layoutroot = layout_root_for(language_id)
return layoutroot if layoutroot

language = Language.find(language_id)
Page.create!(
name: "Layoutroot for #{language.name}",
layoutpage: true,
language: language,
autogenerate_elements: false,
parent_id: Page.root.id,
)
end

def copy_and_paste(source, new_parent, new_name)
page = copy(source, {
parent_id: new_parent.id,
Expand Down Expand Up @@ -565,8 +547,8 @@ def select_page(pages, options = {})
.limit(1).first
end

def set_language_from_parent_or_default
self.language = parent.language || Language.default
def set_language
self.language = parent&.language || Language.current
set_language_code
end

Expand Down
4 changes: 2 additions & 2 deletions app/views/alchemy/admin/layoutpages/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<%= render 'alchemy/admin/partials/language_tree_select' %>
<%= toolbar_button(
icon: :plus,
url: alchemy.new_admin_page_path(parent_id: @layout_root.id, layoutpage: true),
url: alchemy.new_admin_page_path(language: @current_language, layoutpage: true),
hotkey: 'alt+n',
dialog_options: {
title: Alchemy.t('Add global page'),
Expand All @@ -31,5 +31,5 @@
<% end %>

<ul class="list" id="layoutpages">
<%= render partial: "layoutpage", collection: @layout_root.children %>
<%= render partial: "layoutpage", collection: @layout_pages %>
</ul>
1 change: 1 addition & 0 deletions app/views/alchemy/admin/pages/_new_page_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<%= alchemy_form_for([:admin, @page]) do |f| %>
<%= f.hidden_field(:parent_id) %>
<%= f.hidden_field(:language_id) %>
<%= f.hidden_field(:layoutpage) %>
<%= f.input :page_layout,
collection: @page_layouts,
Expand Down
20 changes: 8 additions & 12 deletions lib/alchemy/seeder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,8 @@ def seed_pages
def seed_layoutpages
desc "Seeding Alchemy layout pages from #{page_seeds_file}"
language = Alchemy::Language.default
layout_root = Alchemy::Page.find_or_create_layout_root_for(language.id)
layoutpages.each do |page|
create_page(page, {
parent: layout_root,
language: language,
})
create_page(page, { language: language })
end
end

Expand Down Expand Up @@ -117,14 +113,14 @@ def create_default_language!
default_language = Alchemy::Config.get(:default_language)
if default_language
Alchemy::Language.create!(
name: default_language["name"],
language_code: default_language["code"],
locale: default_language["code"],
name: default_language["name"],
language_code: default_language["code"],
locale: default_language["code"],
frontpage_name: default_language["frontpage_name"],
page_layout: default_language["page_layout"],
public: true,
default: true,
site: Alchemy::Site.default,
page_layout: default_language["page_layout"],
public: true,
default: true,
site: Alchemy::Site.default,
)
else
raise DefaultLanguageNotFoundError
Expand Down
4 changes: 1 addition & 3 deletions lib/alchemy/test_support/factories/page_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

parent_id do
(Alchemy::Page.find_by(language_root: true) ||
FactoryBot.create(:alchemy_page, :language_root, language: language)).id
FactoryBot.create(:alchemy_page, :language_root, language: language)).id
end

# This speeds up creating of pages dramatically.
Expand Down Expand Up @@ -47,8 +47,6 @@
end

trait :layoutpage do
name { "Footer" }
parent_id { Alchemy::Page.find_or_create_layout_root_for(Alchemy::Language.current.id).id }
layoutpage { true }
page_layout { "footer" }
end
Expand Down
13 changes: 13 additions & 0 deletions lib/alchemy/upgrader/five_point_zero.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@ def install_gutentag_migrations
Alchemy::Upgrader::Tasks::HardenGutentagMigrations.new.patch_migrations
`bundle exec rake db:migrate`
end

def remove_layout_roots
desc "Remove layout root pages"
layout_roots = Alchemy::Page.where(layoutpage: true).where("name LIKE 'Layoutroot for%'")
if layout_roots.size.positive?
log "Removing #{layout_roots.size} layout root pages."
layout_roots.delete_all
Alchemy::Page.where(layoutpage: true).update_all(parent_id: nil)
log "Done.", :success
else
log "No layout root pages found.", :skip
end
end
end
end
end
6 changes: 6 additions & 0 deletions lib/tasks/alchemy/upgrade.rake
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ namespace :alchemy do
desc "Alchemy Upgrader: Prepares the database."
task database: [
"alchemy:upgrade:5.0:install_gutentag_migrations",
"alchemy:upgrade:5.0:remove_layout_roots",
"alchemy:install:migrations",
"db:migrate",
"alchemy:db:seed",
Expand All @@ -42,6 +43,11 @@ namespace :alchemy do
task install_gutentag_migrations: [:environment] do
Alchemy::Upgrader::FivePointZero.install_gutentag_migrations
end

desc "Remove layout root pages"
task remove_layout_roots: [:environment] do
Alchemy::Upgrader::FivePointZero.remove_layout_roots
end
end
end
end
10 changes: 7 additions & 3 deletions spec/controllers/alchemy/admin/layoutpages_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@ module Alchemy
context "with a language present" do
let!(:language) { create(:alchemy_language) }

it "should assign @layout_root" do
get :index
expect(assigns(:layout_root)).to be_a(Page)
context "and layoutpages present" do
let!(:layoutpages) { create_list(:alchemy_page, 2, :layoutpage, language: language) }

it "should assign @layout_pages" do
get :index
expect(assigns(:layout_pages)).to match_array(layoutpages)
end
end

it "should assign @languages" do
Expand Down
Loading

0 comments on commit 12c97fe

Please sign in to comment.