From 4b43ce8ac7422203c85343500357a632b3cfe9e4 Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Wed, 6 May 2020 14:38:15 +0200 Subject: [PATCH] Remove root page (#1817) * Remove Page#systempage? * Remove Page.root We do not need a single root page. All pages that share the same language are a nested set. * Format page_scopes.rb * Format pages controller spec file * Add upgrader task to remove root page * Rubocop -a --- .../alchemy/admin/pages_controller.rb | 6 +- app/models/alchemy/page.rb | 34 +--- app/models/alchemy/page/page_elements.rb | 26 +-- app/models/alchemy/page/page_naming.rb | 11 +- app/models/alchemy/page/page_natures.rb | 6 - app/models/alchemy/page/page_scopes.rb | 48 +++-- .../pages/_create_language_form.html.erb | 8 - lib/alchemy/seeder.rb | 1 - .../test_support/factories/page_factory.rb | 25 +-- lib/alchemy/upgrader/five_point_zero.rb | 12 ++ lib/tasks/alchemy/upgrade.rake | 6 + spec/models/alchemy/page_spec.rb | 174 ++++++------------ .../alchemy/admin/pages_controller_spec.rb | 42 ++--- 13 files changed, 152 insertions(+), 247 deletions(-) diff --git a/app/controllers/alchemy/admin/pages_controller.rb b/app/controllers/alchemy/admin/pages_controller.rb index 4340bb570e..ffdcf5ffa4 100644 --- a/app/controllers/alchemy/admin/pages_controller.rb +++ b/app/controllers/alchemy/admin/pages_controller.rb @@ -222,13 +222,11 @@ def flush private def copy_of_language_root - page_copy = Page.copy( + Page.copy( language_root_to_copy_from, language_id: params[:languages][:new_lang_id], language_code: @current_language.code, ) - page_copy.move_to_child_of Page.root - page_copy end def language_root_to_copy_from @@ -363,7 +361,7 @@ def page_needs_lock? def paste_from_clipboard if params[:paste_from_clipboard] source = Page.find(params[:paste_from_clipboard]) - parent = Page.find_by(id: params[:page][:parent_id]) || Page.root + parent = Page.find_by(id: params[:page][:parent_id]) Page.copy_and_paste(source, parent, params[:page][:name]) end end diff --git a/app/models/alchemy/page.rb b/app/models/alchemy/page.rb index f72f4478cb..088c335527 100644 --- a/app/models/alchemy/page.rb +++ b/app/models/alchemy/page.rb @@ -114,32 +114,28 @@ class Page < BaseRecord has_many :nodes, class_name: "Alchemy::Node", inverse_of: :page 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 }, unless: -> { layoutpage? } + + validates_presence_of :page_layout + validates_format_of :page_layout, with: /\A[a-z0-9_-]+\z/, unless: -> { page_layout.blank? } + validates_presence_of :parent, unless: -> { layoutpage? || language_root? } before_save :set_language_code, - if: -> { language.present? }, - unless: :systempage? + if: -> { language.present? } before_save :set_restrictions_to_child_pages, - if: :restricted_changed?, - unless: :systempage? + if: :restricted_changed? before_save :inherit_restricted_status, - if: -> { parent && parent.restricted? }, - unless: :systempage? + if: -> { parent && parent.restricted? } before_save :set_published_at, - if: -> { public_on.present? && published_at.nil? }, - unless: :systempage? + if: -> { public_on.present? && published_at.nil? } before_save :set_fixed_attributes, if: -> { fixed_attributes.any? } before_create :set_language, - if: -> { language.nil? }, - unless: :systempage? + if: -> { language.nil? } after_update :create_legacy_url, if: :should_create_legacy_url? @@ -163,18 +159,6 @@ class Page < BaseRecord # Class methods # class << self - # The root page of the page tree - # - # Internal use only. You wouldn't use this page ever. - # - # Automatically created when accessed the first time. - # - def root - super || create!(name: "Root") - end - - alias_method :rootpage, :root - # Used to store the current page previewed in the edit page template. # def current_preview=(page) diff --git a/app/models/alchemy/page/page_elements.rb b/app/models/alchemy/page/page_elements.rb index 310878f6c1..4c89de7416 100644 --- a/app/models/alchemy/page/page_elements.rb +++ b/app/models/alchemy/page/page_elements.rb @@ -33,7 +33,7 @@ module Page::PageElements join_table: ElementToPage.table_name after_create :generate_elements, - unless: -> { systempage? || autogenerate_elements == false } + unless: -> { autogenerate_elements == false } after_update :trash_not_allowed_elements!, if: :has_page_layout_changed? @@ -81,11 +81,11 @@ def copy_elements(source, target) # def available_element_definitions(only_element_named = nil) @_element_definitions ||= if only_element_named - definition = Element.definition_by_name(only_element_named) - element_definitions_by_name(definition["nestable_elements"]) - else - element_definitions - end + definition = Element.definition_by_name(only_element_named) + element_definitions_by_name(definition["nestable_elements"]) + else + element_definitions + end return [] if @_element_definitions.blank? @@ -107,13 +107,13 @@ def available_element_names # def available_elements_within_current_scope(parent) @_available_elements = if parent - parents_unique_nested_elements = parent.nested_elements.where(unique: true).pluck(:name) - available_element_definitions(parent.name).reject do |e| - parents_unique_nested_elements.include? e["name"] + parents_unique_nested_elements = parent.nested_elements.where(unique: true).pluck(:name) + available_element_definitions(parent.name).reject do |e| + parents_unique_nested_elements.include? e["name"] + end + else + available_element_definitions end - else - available_element_definitions - end end # All element definitions defined for page's page layout @@ -181,7 +181,7 @@ def feed_elements # def richtext_contents_ids Alchemy::Content.joins(:element) - .where(Element.table_name => {page_id: id, folded: false}) + .where(Element.table_name => { page_id: id, folded: false }) .select(&:has_tinymce?) .collect(&:id) end diff --git a/app/models/alchemy/page/page_naming.rb b/app/models/alchemy/page/page_naming.rb index 729ad4ee77..3a19e17d00 100644 --- a/app/models/alchemy/page/page_naming.rb +++ b/app/models/alchemy/page/page_naming.rb @@ -9,17 +9,16 @@ module Page::PageNaming included do before_validation :set_urlname, if: :renamed?, - unless: -> { systempage? || name.blank? } + unless: -> { name.blank? } validates :name, presence: true validates :urlname, - uniqueness: {scope: [:language_id, :layoutpage], if: -> { urlname.present? }}, - exclusion: {in: RESERVED_URLNAMES}, - length: {minimum: 3, if: -> { urlname.present? }} + uniqueness: { scope: [:language_id, :layoutpage], if: -> { urlname.present? } }, + exclusion: { in: RESERVED_URLNAMES }, + length: { minimum: 3, if: -> { urlname.present? } } before_save :set_title, - unless: -> { systempage? }, if: -> { title.blank? } after_update :update_descendants_urlnames, @@ -117,7 +116,7 @@ def nested_url_name(value) # Returns [], if there is no parent, the parent is # the root page itself, or url_nesting is off. def ancestor_slugs - return [] if !Config.get(:url_nesting) || parent.nil? || parent.root? + return [] if !Config.get(:url_nesting) || parent.nil? visible_ancestors.map(&:slug).compact end diff --git a/app/models/alchemy/page/page_natures.rb b/app/models/alchemy/page/page_natures.rb index 60f6390515..ff753a3650 100644 --- a/app/models/alchemy/page/page_natures.rb +++ b/app/models/alchemy/page/page_natures.rb @@ -21,12 +21,6 @@ def rootpage? !new_record? && parent_id.blank? end - def systempage? - return true if Page.count.zero? - - rootpage? || (parent_id == Page.root.id && !language_root?) - end - def folded?(user_id) return unless Alchemy.user_class < ActiveRecord::Base diff --git a/app/models/alchemy/page/page_scopes.rb b/app/models/alchemy/page/page_scopes.rb index d4ffe48e2e..aa01f95259 100644 --- a/app/models/alchemy/page/page_scopes.rb +++ b/app/models/alchemy/page/page_scopes.rb @@ -21,11 +21,12 @@ module Page::PageScopes # All pages locked by given user # - scope :locked_by, ->(user) { - if user.class.respond_to? :primary_key - locked.where(locked_by: user.send(user.class.primary_key)) - end - } + scope :locked_by, + ->(user) { + if user.class.respond_to? :primary_key + locked.where(locked_by: user.send(user.class.primary_key)) + end + } # All not locked pages # @@ -45,29 +46,27 @@ module Page::PageScopes # All pages that are a published language root # - scope :public_language_roots, -> { - published.language_roots.where( - language_code: Language.published.pluck(:language_code), - ) - } + scope :public_language_roots, + -> { + published.language_roots.where( + language_code: Language.published.pluck(:language_code), + ) + } # Last 5 pages that where recently edited by given user # - scope :all_last_edited_from, ->(user) { - where(updater_id: user.id).order("updated_at DESC").limit(5) - } + scope :all_last_edited_from, + ->(user) { + where(updater_id: user.id).order("updated_at DESC").limit(5) + } # Returns all pages that have the given +language_id+ # - scope :with_language, ->(language_id) { - where(language_id: language_id) - } + scope :with_language, ->(language_id) { where(language_id: language_id) } # Returns all content pages. # - scope :contentpages, -> { - where(layoutpage: [false, nil]).where.not(parent_id: nil) - } + scope :contentpages, -> { where(layoutpage: [false, nil]) } # Returns all public contentpages that are not locked. # @@ -79,9 +78,7 @@ module Page::PageScopes # # Used for flushing all pages caches at once. # - scope :flushable_layoutpages, -> { - not_locked.layoutpages.where.not(parent_id: Page.unscoped.root.id) - } + scope :flushable_layoutpages, -> { not_locked.layoutpages } # All searchable pages # @@ -89,9 +86,10 @@ module Page::PageScopes # All pages from +Alchemy::Site.current+ # - scope :from_current_site, -> { - where(Language.table_name => {site_id: Site.current || Site.default}).joins(:language) - } + scope :from_current_site, + -> { + where(Language.table_name => { site_id: Site.current || Site.default }).joins(:language) + } # All pages for xml sitemap # diff --git a/app/views/alchemy/admin/pages/_create_language_form.html.erb b/app/views/alchemy/admin/pages/_create_language_form.html.erb index 5e2ccc088c..482099da00 100644 --- a/app/views/alchemy/admin/pages/_create_language_form.html.erb +++ b/app/views/alchemy/admin/pages/_create_language_form.html.erb @@ -1,4 +1,3 @@ -<% if root = Alchemy::Page.rootpage %>
<%= render_message do %>

<%= Alchemy.t(:language_does_not_exist) %>

@@ -37,7 +36,6 @@ <%= form.hidden_field :language_id, value: @language.id %> <%= form.hidden_field :language_code, value: @language.code %> <%= form.hidden_field :language_root, value: true %> - <%= form.hidden_field :parent_id, value: root.id %> <%= form.hidden_field :public, value: Alchemy::Language.all.size == 1 %> <%= form.submit Alchemy.t("create_tree_as_new_language", language: @language.name), autofocus: true %> <% end %> @@ -50,9 +48,3 @@ <%- end -%>
-<% else %> -<%= render_message :error do %> -

Root page not found.

-

Please run bin/rake db:seed task.

-<% end %> -<% end %> diff --git a/lib/alchemy/seeder.rb b/lib/alchemy/seeder.rb index b2d7a47e3e..b5f981f648 100644 --- a/lib/alchemy/seeder.rb +++ b/lib/alchemy/seeder.rb @@ -45,7 +45,6 @@ def seed_pages contentpages.each do |page| create_page(page, { - parent: Alchemy::Page.root, language: Alchemy::Language.default, language_root: true, }) diff --git a/lib/alchemy/test_support/factories/page_factory.rb b/lib/alchemy/test_support/factories/page_factory.rb index b04b085067..036c00ee98 100644 --- a/lib/alchemy/test_support/factories/page_factory.rb +++ b/lib/alchemy/test_support/factories/page_factory.rb @@ -9,28 +9,21 @@ sequence(:name) { |n| "A Page #{n}" } page_layout { "standard" } - parent_id do - (Alchemy::Page.find_by(language_root: true) || - FactoryBot.create(:alchemy_page, :language_root, language: language)).id + parent do + Alchemy::Page.find_by(language_root: true, language: language) || + FactoryBot.create(:alchemy_page, :language_root, language: language) end # This speeds up creating of pages dramatically. # Pass autogenerate_elements: true to generate elements autogenerate_elements { false } - trait :root do - name { "Root" } - language { nil } - parent_id { nil } - page_layout { nil } - end - trait :language_root do - name { "Startseite" } + name { language.frontpage_name } page_layout { language.page_layout } language_root { true } public_on { Time.current } - parent_id { Alchemy::Page.root.id } + parent { nil } end trait :public do @@ -38,14 +31,6 @@ public_on { Time.current } end - trait :system do - name { "Systempage" } - parent_id { Alchemy::Page.root.id } - language_root { false } - page_layout { nil } - language { nil } - end - trait :layoutpage do layoutpage { true } page_layout { "footer" } diff --git a/lib/alchemy/upgrader/five_point_zero.rb b/lib/alchemy/upgrader/five_point_zero.rb index ae57b3f9b2..338df576ea 100644 --- a/lib/alchemy/upgrader/five_point_zero.rb +++ b/lib/alchemy/upgrader/five_point_zero.rb @@ -24,6 +24,18 @@ def remove_layout_roots log "No layout root pages found.", :skip end end + + def remove_root_page + desc "Remove root page" + root_page = Alchemy::Page.find_by(parent_id: nil, name: "Root") + if root_page + Alchemy::Page.where(parent_id: root_page.id).update_all(parent_id: nil) + root_page.delete + log "Done.", :success + else + log "Root page not found.", :skip + end + end end end end diff --git a/lib/tasks/alchemy/upgrade.rake b/lib/tasks/alchemy/upgrade.rake index 5a53e3658b..17256f0db1 100644 --- a/lib/tasks/alchemy/upgrade.rake +++ b/lib/tasks/alchemy/upgrade.rake @@ -21,6 +21,7 @@ namespace :alchemy do task database: [ "alchemy:upgrade:5.0:install_gutentag_migrations", "alchemy:upgrade:5.0:remove_layout_roots", + "alchemy:upgrade:5.0:remove_root_page", "alchemy:install:migrations", "db:migrate", "alchemy:db:seed", @@ -48,6 +49,11 @@ namespace :alchemy do task remove_layout_roots: [:environment] do Alchemy::Upgrader::FivePointZero.remove_layout_roots end + + desc "Remove root page" + task remove_root_page: [:environment] do + Alchemy::Upgrader::FivePointZero.remove_root_page + end end end end diff --git a/spec/models/alchemy/page_spec.rb b/spec/models/alchemy/page_spec.rb index b68fa3720a..f18af476ec 100644 --- a/spec/models/alchemy/page_spec.rb +++ b/spec/models/alchemy/page_spec.rb @@ -4,7 +4,6 @@ module Alchemy describe Page do - let(:rootpage) { Page.root } let(:language) { create(:alchemy_language, :english, default: true) } let(:klingon) { create(:alchemy_language, :klingon) } let(:language_root) { create(:alchemy_page, :language_root) } @@ -31,20 +30,6 @@ module Alchemy end end - it "should validate the page_layout" do - contentpage.page_layout = nil - expect(contentpage).not_to be_valid - contentpage.valid? - expect(contentpage.errors[:page_layout].size).to eq(1) - end - - it "should validate the parent_id" do - contentpage.parent_id = nil - expect(contentpage).not_to be_valid - contentpage.valid? - expect(contentpage.errors[:parent_id].size).to eq(1) - end - context "with page having same urlname" do before { with_same_urlname } @@ -55,7 +40,7 @@ module Alchemy end context "with url_nesting set to true" do - let(:other_parent) { create(:alchemy_page, parent_id: Page.root.id, visible: true) } + let(:other_parent) { create(:alchemy_page, visible: true) } before do stub_alchemy_config(:url_nesting, true) @@ -64,7 +49,7 @@ module Alchemy it "should only validate urlname dependent of parent" do contentpage.urlname = "existing_twice" - contentpage.parent_id = other_parent.id + contentpage.parent = other_parent expect(contentpage).to be_valid end @@ -75,24 +60,22 @@ module Alchemy end end - context "creating the rootpage without parent_id and page_layout" do - let(:rootpage) { build(:alchemy_page, parent_id: nil, page_layout: nil, name: "Rootpage") } + context "a page not being a language_root and without parent" do + let(:page) { build(:alchemy_page, parent: nil, layoutpage: nil) } - before do - Page.delete_all - end + it { expect(page).to_not be_valid } + end - it "should be valid" do - expect(rootpage).to be_valid - end + context "a page being a language_root and without parent" do + let(:page) { build(:alchemy_page, parent: nil, layoutpage: true) } + + it { expect(page).to be_valid } end - context "saving a systempage" do - let(:systempage) { build(:alchemy_page, :system) } + context "a page without page_layout" do + let(:page) { build(:alchemy_page, page_layout: nil) } - it "should not validate the page_layout" do - expect(systempage).to be_valid - end + it { expect(page).to_not be_valid } end end @@ -100,7 +83,7 @@ module Alchemy context "callbacks" do let(:page) do - create(:alchemy_page, name: "My Testpage", language: language, parent_id: language_root.id) + create(:alchemy_page, name: "My Testpage", language: language, parent: language_root) end context "before_save" do @@ -190,7 +173,7 @@ module Alchemy context "after_move" do let(:parent_1) { create(:alchemy_page, name: "Parent 1", visible: true) } let(:parent_2) { create(:alchemy_page, name: "Parent 2", visible: true) } - let(:page) { create(:alchemy_page, parent_id: parent_1.id, name: "Page", visible: true) } + let(:page) { create(:alchemy_page, parent: parent_1, name: "Page", visible: true) } it "updates the urlname" do expect(page.urlname).to eq("parent-1/page") @@ -228,7 +211,7 @@ module Alchemy context "with children getting restricted set to true" do before do page.save - @child1 = create(:alchemy_page, name: "Child 1", parent_id: page.id) + @child1 = create(:alchemy_page, name: "Child 1", parent: page) page.reload page.restricted = true page.save @@ -240,15 +223,16 @@ module Alchemy end end - context "with restricted parent gets created" do + context "with restricted parent" do + let(:new_page) { create(:alchemy_page, name: "New Page", parent: page) } + before do page.save - page.parent.update!(restricted: true) - @new_page = create(:alchemy_page, name: "New Page", parent_id: page.id) + page.update!(restricted: true) end - it "is also be restricted" do - expect(@new_page.restricted?).to be_truthy + it "child is also restricted" do + expect(new_page.restricted?).to be_truthy end end @@ -264,18 +248,6 @@ module Alchemy end end - context "Creating a systempage" do - let!(:page) { create(:alchemy_page, :system) } - - it "does not get the language code from language" do - expect(page.language_code).to be_nil - end - - it "does not autogenerate the elements" do - expect(page.elements).to be_empty - end - end - context "after changing the page layout" do let(:news_element) { news_page.elements.find_by(name: "news") } @@ -342,7 +314,7 @@ module Alchemy describe ".locked" do it "should return 1 page that is blocked by a user at the moment" do - create(:alchemy_page, :public, :locked, name: "First Public Child", parent_id: language_root.id, language: language) + create(:alchemy_page, :public, :locked, name: "First Public Child", parent: language_root, language: language) expect(Page.locked.size).to eq(1) end end @@ -378,7 +350,7 @@ module Alchemy describe ".contentpages" do let!(:layoutpage) do - create :alchemy_page, :public, { + create :alchemy_page, { name: "layoutpage", layoutpage: true, language: klingon, @@ -394,9 +366,9 @@ module Alchemy end let!(:contentpage) do - create :alchemy_page, :public, { + create :alchemy_page, { name: "contentpage", - parent_id: language_root.id, + parent: language_root, language: language, } end @@ -525,53 +497,53 @@ module Alchemy describe ".create" do context "before/after filter" do it "should automatically set the title from its name" do - page = create(:alchemy_page, name: "My Testpage", language: language, parent_id: language_root.id) + page = create(:alchemy_page, name: "My Testpage", language: language, parent: language_root) expect(page.title).to eq("My Testpage") end it "should get a webfriendly urlname" do - page = create(:alchemy_page, name: "klingon$&stößel ", language: language, parent_id: language_root.id) + page = create(:alchemy_page, name: "klingon$&stößel ", language: language, parent: language_root) expect(page.urlname).to eq("klingon-stoessel") end context "with no name set" do it "should not set a urlname" do - page = Page.create(name: "", language: language, parent_id: language_root.id) + page = Page.create(name: "", language: language, parent: language_root) expect(page.urlname).to be_blank end end it "should generate a three letter urlname from two letter name" do - page = create(:alchemy_page, name: "Au", language: language, parent_id: language_root.id) + page = create(:alchemy_page, name: "Au", language: language, parent: language_root) expect(page.urlname).to eq("-au") end it "should generate a three letter urlname from two letter name with umlaut" do - page = create(:alchemy_page, name: "Aü", language: language, parent_id: language_root.id) + page = create(:alchemy_page, name: "Aü", language: language, parent: language_root) expect(page.urlname).to eq("aue") end it "should generate a three letter urlname from one letter name" do - page = create(:alchemy_page, name: "A", language: language, parent_id: language_root.id) + page = create(:alchemy_page, name: "A", language: language, parent: language_root) expect(page.urlname).to eq("--a") end it "should add a user stamper" do - page = create(:alchemy_page, name: "A", language: language, parent_id: language_root.id) + page = create(:alchemy_page, name: "A", language: language, parent: language_root) expect(page.class.stamper_class.to_s).to eq("DummyUser") end context "with language given" do it "does not set the language from parent" do expect_any_instance_of(Page).not_to receive(:set_language) - Page.create!(name: "A", parent_id: language_root.id, page_layout: "standard", language: language) + Page.create!(name: "A", parent: language_root, page_layout: "standard", language: language) end end context "with no language given" do it "sets the language from parent" do expect_any_instance_of(Page).to receive(:set_language) - Page.create!(name: "A", parent_id: language_root.id, page_layout: "standard") + Page.create!(name: "A", parent: language_root, page_layout: "standard") end end end @@ -579,30 +551,30 @@ module Alchemy describe ".language_roots" do it "should return 1 language_root" do - create(:alchemy_page, :public, name: "First Public Child", parent_id: language_root.id, language: language) + create(:alchemy_page, :public, name: "First Public Child", parent: language_root, language: language) expect(Page.language_roots.size).to eq(1) end end describe ".layoutpages" do - it "should return 1 layoutpage" do - create(:alchemy_page, :public, layoutpage: true, name: "Layoutpage", parent_id: rootpage.id, language: language) + it "should return layoutpages" do + create(:alchemy_page, :public, layoutpage: true, name: "Layoutpage", language: language) expect(Page.layoutpages.size).to eq(1) end end describe ".not_locked" do it "should return pages that are not blocked by a user at the moment" do - create(:alchemy_page, :public, :locked, name: "First Public Child", parent_id: language_root.id, language: language) - create(:alchemy_page, :public, name: "Second Public Child", parent_id: language_root.id, language: language) - expect(Page.not_locked.size).to eq(3) + create(:alchemy_page, :public, :locked, name: "First Public Child", parent: language_root, language: language) + create(:alchemy_page, :public, name: "Second Public Child", parent: language_root, language: language) + expect(Page.not_locked.size).to eq(2) end end describe ".not_restricted" do - it "should return 2 accessible pages" do - create(:alchemy_page, :public, name: "First Public Child", restricted: true, parent_id: language_root.id, language: language) - expect(Page.not_restricted.size).to eq(2) + it "should return accessible pages" do + create(:alchemy_page, :public, name: "First Public Child", restricted: true, parent: language_root, language: language) + expect(Page.not_restricted.size).to eq(1) end end @@ -612,21 +584,21 @@ module Alchemy let!(:public_one) do create :alchemy_page, :public, name: "First Public Child", - parent_id: language_root.id, + parent: language_root, language: language end let!(:public_two) do create :alchemy_page, :public, name: "Second Public Child", - parent_id: language_root.id, + parent: language_root, language: language end let!(:non_public_page) do create :alchemy_page, name: "Non Public Child", - parent_id: language_root.id, + parent: language_root, language: language end @@ -639,43 +611,21 @@ module Alchemy describe ".public_language_roots" do it "should return pages that public language roots" do - create(:alchemy_page, :public, name: "First Public Child", parent_id: language_root.id, language: language) + create(:alchemy_page, :public, name: "First Public Child", parent: language_root, language: language) expect(Page.public_language_roots.size).to eq(1) end end describe ".restricted" do it "should return 1 restricted page" do - create(:alchemy_page, :public, name: "First Public Child", restricted: true, parent_id: language_root.id, language: language) + create(:alchemy_page, :public, name: "First Public Child", restricted: true, parent: language_root, language: language) expect(Page.restricted.size).to eq(1) end end - describe ".root" do - context "when root page is present" do - let!(:root_page) { Page.root } - - it "returns root page" do - expect(Page.root).to eq(root_page) - end - end - - context "when no root page is present yet" do - before do - Page.delete_all - end - - it "creates and returns root page" do - expect { - Page.root - }.to change { Page.count }.by(1) - end - end - end - describe ".visible" do it "should return 1 visible page" do - create(:alchemy_page, :public, name: "First Public Child", visible: true, parent_id: language_root.id, language: language) + create(:alchemy_page, :public, name: "First Public Child", visible: true, parent: language_root, language: language) expect(Page.visible.size).to eq(1) end end @@ -1080,7 +1030,7 @@ module Alchemy create :alchemy_page, name: "First child", language: language, - parent_id: language_root.id + parent: language_root end context "with existing public child" do @@ -1088,7 +1038,7 @@ module Alchemy create :alchemy_page, :public, name: "First public child", language: language, - parent_id: language_root.id + parent: language_root end it "should return first_public_child" do @@ -1167,10 +1117,6 @@ module Alchemy it "it returns the page layout definition as hash." do expect(language_root.definition["name"]).to eq("index") end - - it "it returns an empty hash for root page." do - expect(rootpage.definition).to eq({}) - end end end @@ -1620,10 +1566,10 @@ module Alchemy context "urlname updating" do let(:parentparent) { create(:alchemy_page, name: "parentparent", visible: true) } - let(:parent) { create(:alchemy_page, parent_id: parentparent.id, name: "parent", visible: true) } - let(:page) { create(:alchemy_page, parent_id: parent.id, name: "page", visible: true) } - let(:invisible) { create(:alchemy_page, parent_id: page.id, name: "invisible", visible: false) } - let(:contact) { create(:alchemy_page, parent_id: invisible.id, name: "contact", visible: true) } + let(:parent) { create(:alchemy_page, parent: parentparent, name: "parent", visible: true) } + let(:page) { create(:alchemy_page, parent: parent, name: "page", visible: true) } + let(:invisible) { create(:alchemy_page, parent: page, name: "invisible", visible: false) } + let(:contact) { create(:alchemy_page, parent: invisible, name: "contact", visible: true) } let(:language_root) { parentparent.parent } context "with activated url_nesting" do @@ -1635,12 +1581,6 @@ module Alchemy expect(page.urlname).to eq("parentparent/parent/page") end - it "should not include the root page" do - Page.root.update_column(:urlname, "root") - language_root.update(urlname: "new-urlname") - expect(language_root.urlname).not_to match(/root/) - end - it "should not include the language root page" do expect(page.urlname).not_to match(/startseite/) end @@ -1702,7 +1642,7 @@ module Alchemy describe "#update_node!" do let(:original_url) { "sample-url" } - let(:page) { create(:alchemy_page, language: language, parent_id: language_root.id, urlname: original_url, restricted: false) } + let(:page) { create(:alchemy_page, language: language, parent: language_root, urlname: original_url, restricted: false) } let(:node) { TreeNode.new(10, 11, 12, 13, "another-url", true) } context "when nesting is enabled" do diff --git a/spec/requests/alchemy/admin/pages_controller_spec.rb b/spec/requests/alchemy/admin/pages_controller_spec.rb index 1bb2cd073e..1414e60d11 100644 --- a/spec/requests/alchemy/admin/pages_controller_spec.rb +++ b/spec/requests/alchemy/admin/pages_controller_spec.rb @@ -87,7 +87,7 @@ module Alchemy end describe "#tree" do - let(:user) { create(:alchemy_dummy_user, :as_editor) } + let(:user) { create(:alchemy_dummy_user, :as_editor) } let(:page_1) { create(:alchemy_page, visible: true, name: "one") } let(:page_2) { create(:alchemy_page, visible: true, name: "two", parent_id: page_1.id) } let(:page_3) { create(:alchemy_page, visible: true, name: "three", parent_id: page_2.id) } @@ -254,7 +254,7 @@ module Alchemy before do allow_any_instance_of(described_class).to receive(:get_clipboard).with("pages") do - [{"id" => page.id.to_s, "action" => "copy"}] + [{ "id" => page.id.to_s, "action" => "copy" }] end end @@ -305,12 +305,12 @@ module Alchemy end describe "#order" do - let(:page_1) { create(:alchemy_page, visible: true) } - let(:page_2) { create(:alchemy_page, visible: true) } - let(:page_3) { create(:alchemy_page, visible: true) } - let(:page_item_1) { {id: page_1.id, slug: page_1.slug, restricted: false, visible: page_1.visible?, children: [page_item_2]} } - let(:page_item_2) { {id: page_2.id, slug: page_2.slug, restricted: false, visible: page_2.visible?, children: [page_item_3]} } - let(:page_item_3) { {id: page_3.id, slug: page_3.slug, restricted: false, visible: page_3.visible? } } + let(:page_1) { create(:alchemy_page, visible: true) } + let(:page_2) { create(:alchemy_page, visible: true) } + let(:page_3) { create(:alchemy_page, visible: true) } + let(:page_item_1) { { id: page_1.id, slug: page_1.slug, restricted: false, visible: page_1.visible?, children: [page_item_2] } } + let(:page_item_2) { { id: page_2.id, slug: page_2.slug, restricted: false, visible: page_2.visible?, children: [page_item_3] } } + let(:page_item_3) { { id: page_3.id, slug: page_3.slug, restricted: false, visible: page_3.visible? } } let(:set_of_pages) { [page_item_1] } it "stores the new order" do @@ -421,6 +421,7 @@ module Alchemy parent_id: parent.id, name: "new Page", page_layout: "standard", + language_id: parent.language_id, } end @@ -488,8 +489,11 @@ module Alchemy let(:page_in_clipboard) { create(:alchemy_page) } it "should call Page#copy_and_paste" do - expect(Page).to receive(:copy_and_paste). - with(page_in_clipboard, parent, page_params[:name]) + expect(Page).to receive(:copy_and_paste).with( + page_in_clipboard, + parent, + page_params[:name], + ) post admin_pages_path( page: page_params, paste_from_clipboard: page_in_clipboard.id, @@ -499,14 +503,13 @@ module Alchemy end describe "#copy_language_tree" do - let(:params) { {languages: {new_lang_id: "2", old_lang_id: "1"}} } + let(:params) { { languages: { new_lang_id: "2", old_lang_id: "1" } } } let(:language_root_to_copy_from) { build_stubbed(:alchemy_page, :language_root) } - let(:copy_of_language_root) { build_stubbed(:alchemy_page, :language_root) } - let(:root_page) { mock_model("Page") } + let(:copy_of_language_root) { build_stubbed(:alchemy_page, :language_root) } + let(:root_page) { mock_model("Page") } before do allow(Page).to receive(:copy).and_return(copy_of_language_root) - allow(Page).to receive(:root).and_return(root_page) allow(Page).to receive(:language_root_for).and_return(language_root_to_copy_from) allow_any_instance_of(Page).to receive(:move_to_child_of) allow_any_instance_of(Page).to receive(:copy_children_to) @@ -514,12 +517,7 @@ module Alchemy end it "should copy the language root page over to the other language" do - expect(Page).to receive(:copy).with(language_root_to_copy_from, {language_id: "2", language_code: "de"}) - post copy_language_tree_admin_pages_path(params) - end - - it "should move the newly created language-root-page below the absolute root page" do - expect(copy_of_language_root).to receive(:move_to_child_of).with(root_page) + expect(Page).to receive(:copy).with(language_root_to_copy_from, { language_id: "2", language_code: "de" }) post copy_language_tree_admin_pages_path(params) end @@ -541,7 +539,7 @@ module Alchemy end describe "#edit" do - let!(:page) { create(:alchemy_page) } + let!(:page) { create(:alchemy_page) } let!(:other_user) { create(:alchemy_dummy_user, :as_author) } context "if page is locked by another user" do @@ -631,7 +629,7 @@ module Alchemy end describe "#destroy" do - let(:clipboard) { [{"id" => page.id.to_s}] } + let(:clipboard) { [{ "id" => page.id.to_s }] } let(:page) { create(:alchemy_page, :public) } before do