From a8185ce5f74e335a030b69ba16875261a856b760 Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Sat, 16 Jan 2021 17:08:48 +0100 Subject: [PATCH 1/3] Extract presenters out of PageLayout repository The PageLayout class should only be the layouts repository class. --- .../alchemy/admin/pages_controller.rb | 4 +- app/models/alchemy/page.rb | 4 +- app/models/alchemy/page/page_layouts.rb | 116 ++++++++++++++++++ .../alchemy/admin/languages/_form.html.erb | 2 +- .../admin/languages/_language.html.erb | 2 +- .../admin/pages/_page_layout_filter.html.erb | 2 +- lib/alchemy/page_layout.rb | 113 ----------------- spec/libraries/page_layout_spec.rb | 84 ------------- spec/models/alchemy/page/page_layouts_spec.rb | 70 +++++++++++ 9 files changed, 194 insertions(+), 203 deletions(-) create mode 100644 app/models/alchemy/page/page_layouts.rb create mode 100644 spec/models/alchemy/page/page_layouts_spec.rb diff --git a/app/controllers/alchemy/admin/pages_controller.rb b/app/controllers/alchemy/admin/pages_controller.rb index 08ff3106c3..1b4b14a2ce 100644 --- a/app/controllers/alchemy/admin/pages_controller.rb +++ b/app/controllers/alchemy/admin/pages_controller.rb @@ -78,7 +78,7 @@ def info def new @page ||= Page.new(layoutpage: params[:layoutpage] == "true", parent_id: params[:parent_id]) - @page_layouts = PageLayout.layouts_for_select(@current_language.id, @page.layoutpage?) + @page_layouts = Page.layouts_for_select(@current_language.id, @page.layoutpage?) @clipboard = get_clipboard("pages") @clipboard_items = Page.all_from_clipboard_for_select(@clipboard, @current_language.id, @page.layoutpage?) end @@ -398,7 +398,7 @@ def serialized_page_tree def load_languages_and_layouts @language = @current_language @languages_with_page_tree = Language.on_current_site.with_root_page - @page_layouts = PageLayout.layouts_for_select(@language.id) + @page_layouts = Page.layouts_for_select(@language.id) end def set_preview_mode diff --git a/app/models/alchemy/page.rb b/app/models/alchemy/page.rb index da4866500a..9856af6848 100644 --- a/app/models/alchemy/page.rb +++ b/app/models/alchemy/page.rb @@ -36,6 +36,7 @@ # require_dependency "alchemy/page/fixed_attributes" +require_dependency "alchemy/page/page_layouts" require_dependency "alchemy/page/page_scopes" require_dependency "alchemy/page/page_natures" require_dependency "alchemy/page/page_naming" @@ -152,6 +153,7 @@ class Page < BaseRecord after_update :touch_nodes # Concerns + include PageLayouts include PageScopes include PageNatures include PageNaming @@ -271,7 +273,7 @@ def all_from_clipboard_for_select(clipboard, language_id, layoutpage = false) return [] if clipboard.blank? clipboard_pages = all_from_clipboard(clipboard) - allowed_page_layouts = Alchemy::PageLayout.selectable_layouts(language_id, layoutpage) + allowed_page_layouts = Alchemy::Page.selectable_layouts(language_id, layoutpage) allowed_page_layout_names = allowed_page_layouts.collect { |p| p["name"] } clipboard_pages.select { |cp| allowed_page_layout_names.include?(cp.page_layout) } end diff --git a/app/models/alchemy/page/page_layouts.rb b/app/models/alchemy/page/page_layouts.rb new file mode 100644 index 0000000000..526e274a96 --- /dev/null +++ b/app/models/alchemy/page/page_layouts.rb @@ -0,0 +1,116 @@ +# frozen_string_literal: true + +module Alchemy + class Page < BaseRecord + # Module concerning page layouts + # + module PageLayouts + extend ActiveSupport::Concern + + module ClassMethods + # Returns page layouts ready for Rails' select form helper. + # + def layouts_for_select(language_id, only_layoutpages = false) + @map_array = [] + mapped_layouts_for_select(selectable_layouts(language_id, only_layoutpages)) + end + + # Returns page layouts including given layout ready for Rails' select form helper. + # + def layouts_with_own_for_select(page_layout_name, language_id, only_layoutpages = false) + layouts = selectable_layouts(language_id, only_layoutpages) + if layouts.detect { |l| l["name"] == page_layout_name }.nil? + @map_array = [[human_layout_name(page_layout_name), page_layout_name]] + else + @map_array = [] + end + mapped_layouts_for_select(layouts) + end + + deprecate :layouts_with_own_for_select, deprecator: Alchemy::Deprecation + + # Returns all layouts that can be used for creating a new page. + # + # It removes all layouts from available layouts that are unique and already taken and that are marked as hide. + # + # @param [Fixnum] + # language_id of current used Language. + # @param [Boolean] (false) + # Pass true to only select layouts for global/layout pages. + # + def selectable_layouts(language_id, only_layoutpages = false) + @language_id = language_id + Alchemy::PageLayout.all.select do |layout| + if only_layoutpages + layout["layoutpage"] && layout_available?(layout) + else + !layout["layoutpage"] && layout_available?(layout) + end + end + end + + # Translates name for given layout + # + # === Translation example + # + # en: + # alchemy: + # page_layout_names: + # products_overview: Products Overview + # + # @param [String] + # The layout name + # + def human_layout_name(layout) + Alchemy.t(layout, scope: "page_layout_names", default: layout.to_s.humanize) + end + + private + + # Maps given layouts for Rails select form helper. + # + def mapped_layouts_for_select(layouts) + layouts.each do |layout| + @map_array << [human_layout_name(layout["name"]), layout["name"]] + end + @map_array + end + + # Returns true if the given layout is unique and not already taken or it should be hidden. + # + def layout_available?(layout) + !layout["hide"] && !already_taken?(layout) && available_on_site?(layout) + end + + # Returns true if this layout is unique and already taken by another page. + # + def already_taken?(layout) + layout["unique"] && page_with_layout_existing?(layout["name"]) + end + + # Returns true if one page already has the given layout + # + def page_with_layout_existing?(layout) + Alchemy::Page.where(page_layout: layout, language_id: @language_id).pluck(:id).any? + end + + # Returns true if given layout is available for current site. + # + # If no site layouts are defined it always returns true. + # + # == Example + # + # # config/alchemy/site_layouts.yml + # - name: default_site + # page_layouts: [default_intro] + # + def available_on_site?(layout) + return false unless Alchemy::Site.current + + Alchemy::Site.current.definition.blank? || + Alchemy::Site.current.definition.fetch("page_layouts", []).include?(layout["name"]) + end + end + end + end +end diff --git a/app/views/alchemy/admin/languages/_form.html.erb b/app/views/alchemy/admin/languages/_form.html.erb index 6d6c0f1374..8ee463d007 100644 --- a/app/views/alchemy/admin/languages/_form.html.erb +++ b/app/views/alchemy/admin/languages/_form.html.erb @@ -14,7 +14,7 @@ <%= f.input :frontpage_name %> <%= f.input :page_layout, collection: Alchemy::PageLayout.all, - label_method: ->(p) { Alchemy::PageLayout.human_layout_name(p['name']) }, + label_method: ->(p) { Alchemy::Page.human_layout_name(p['name']) }, value_method: ->(p) { p['name'] }, input_html: {class: 'alchemy_selectbox'} %> <%= f.input :public %> diff --git a/app/views/alchemy/admin/languages/_language.html.erb b/app/views/alchemy/admin/languages/_language.html.erb index 5a33f3dc39..b90b2d2bb0 100644 --- a/app/views/alchemy/admin/languages/_language.html.erb +++ b/app/views/alchemy/admin/languages/_language.html.erb @@ -18,7 +18,7 @@ <%= language.frontpage_name %> - <%= Alchemy::PageLayout.human_layout_name(language.page_layout) %> + <%= Alchemy::Page.human_layout_name(language.page_layout) %> <%= language.public? ? render_icon(:check) : nil %> diff --git a/app/views/alchemy/admin/pages/_page_layout_filter.html.erb b/app/views/alchemy/admin/pages/_page_layout_filter.html.erb index ff0a4056d5..8ee5c2c193 100644 --- a/app/views/alchemy/admin/pages/_page_layout_filter.html.erb +++ b/app/views/alchemy/admin/pages/_page_layout_filter.html.erb @@ -6,7 +6,7 @@ options_for_select( @current_language.site.page_layout_names.map do |layout| [ - Alchemy::PageLayout.human_layout_name(layout), + Alchemy::Page.human_layout_name(layout), layout ] end, diff --git a/lib/alchemy/page_layout.rb b/lib/alchemy/page_layout.rb index d03294b0e8..c76fc0d075 100644 --- a/lib/alchemy/page_layout.rb +++ b/lib/alchemy/page_layout.rb @@ -41,112 +41,8 @@ def get(name) all.detect { |a| a["name"].casecmp(name).zero? } end - def get_all_by_attributes(attributes) - return [] if attributes.blank? - - if attributes.is_a? Hash - layouts = [] - attributes.stringify_keys.each do |key, value| - result = all.select { |l| l.key?(key) && l[key].to_s.casecmp(value.to_s).zero? } - layouts += result unless result.empty? - end - layouts - else - [] - end - end - - # Returns page layouts ready for Rails' select form helper. - # - def layouts_for_select(language_id, only_layoutpages = false) - @map_array = [] - mapped_layouts_for_select(selectable_layouts(language_id, only_layoutpages)) - end - - # Returns all layouts that can be used for creating a new page. - # - # It removes all layouts from available layouts that are unique and already taken and that are marked as hide. - # - # @param [Fixnum] - # language_id of current used Language. - # @param [Boolean] (false) - # Pass true to only select layouts for global/layout pages. - # - def selectable_layouts(language_id, only_layoutpages = false) - @language_id = language_id - all.select do |layout| - if only_layoutpages - layout["layoutpage"] && layout_available?(layout) - else - !layout["layoutpage"] && layout_available?(layout) - end - end - end - - # Returns all names of elements defined in given page layout. - # - def element_names_for(page_layout) - if definition = get(page_layout) - definition.fetch("elements", []) - else - Rails.logger.warn "\n+++ Warning: No layout definition for #{page_layout} found! in page_layouts.yml\n" - [] - end - end - - # Translates name for given layout - # - # === Translation example - # - # en: - # alchemy: - # page_layout_names: - # products_overview: Products Overview - # - # @param [String] - # The layout name - # - def human_layout_name(layout) - Alchemy.t(layout, scope: "page_layout_names", default: layout.to_s.humanize) - end - private - # Returns true if the given layout is unique and not already taken or it should be hidden. - # - def layout_available?(layout) - !layout["hide"] && !already_taken?(layout) && available_on_site?(layout) - end - - # Returns true if this layout is unique and already taken by another page. - # - def already_taken?(layout) - layout["unique"] && page_with_layout_existing?(layout["name"]) - end - - # Returns true if one page already has the given layout - # - def page_with_layout_existing?(layout) - Alchemy::Page.where(page_layout: layout, language_id: @language_id).pluck(:id).any? - end - - # Returns true if given layout is available for current site. - # - # If no site layouts are defined it always returns true. - # - # == Example - # - # # config/alchemy/site_layouts.yml - # - name: default_site - # page_layouts: [default_intro] - # - def available_on_site?(layout) - return false unless Alchemy::Site.current - - Alchemy::Site.current.definition.blank? || - Alchemy::Site.current.definition.fetch("page_layouts", []).include?(layout["name"]) - end - # Reads the layout definitions from +config/alchemy/page_layouts.yml+. # def read_definitions_file @@ -168,15 +64,6 @@ def read_definitions_file def layouts_file_path Rails.root.join "config/alchemy/page_layouts.yml" end - - # Maps given layouts for Rails select form helper. - # - def mapped_layouts_for_select(layouts) - layouts.each do |layout| - @map_array << [human_layout_name(layout["name"]), layout["name"]] - end - @map_array - end end end end diff --git a/spec/libraries/page_layout_spec.rb b/spec/libraries/page_layout_spec.rb index 1740889d32..44623d305b 100644 --- a/spec/libraries/page_layout_spec.rb +++ b/spec/libraries/page_layout_spec.rb @@ -67,89 +67,5 @@ module Alchemy expect(PageLayout.get("default")).to eq({"name" => "default"}) end end - - describe ".get_all_by_attributes" do - subject { PageLayout.get_all_by_attributes(unique: true) } - - it "should return all page layout with the given attribute" do - expect(subject.map { |page_layout| page_layout["name"] }.to_a).to eq(["index", "news", "contact", "erb_layout"]) - end - end - - describe ".selectable_layouts" do - let(:site) { create(:alchemy_site) } - let(:language) { create(:alchemy_language, code: :de) } - before { language } - subject { PageLayout.selectable_layouts(language.id) } - - it "should not display hidden page layouts" do - subject.each { |l| expect(l["hide"]).not_to eq(true) } - end - - context "with already taken layouts" do - before do - allow(PageLayout).to receive(:all).and_return([{"unique" => true}]) - allow(Page).to receive(:where).and_return double(pluck: [1]) - end - - it "should not include unique layouts" do - subject.each { |l| expect(l["unique"]).not_to eq(true) } - end - end - - context "with sites layouts present" do - let(:definition) do - {"name" => "default_site", "page_layouts" => %w(index)} - end - - before do - allow_any_instance_of(Site).to receive(:definition).and_return(definition) - end - - it "should only return layouts for site" do - expect(subject.length).to eq(1) - expect(subject.first["name"]).to eq("index") - end - end - end - - describe ".element_names_for" do - it "should return all element names for the given pagelayout" do - allow(PageLayout).to receive(:get).with("default").and_return({"name" => "default", "elements" => ["element_1", "element_2"]}) - expect(PageLayout.element_names_for("default")).to eq(["element_1", "element_2"]) - end - - context "when given page_layout name does not exist" do - it "should return an empty array" do - expect(PageLayout.element_names_for("layout_does_not_exist!")).to eq([]) - end - end - - context "when page_layout definition does not contain the elements key" do - it "should return an empty array" do - allow(PageLayout).to receive(:get).with("layout_without_elements_key").and_return({"name" => "layout_without_elements_key"}) - expect(PageLayout.element_names_for("layout_without_elements_key")).to eq([]) - end - end - end - - describe ".human_layout_name" do - let(:layout) { {"name" => "contact"} } - subject { PageLayout.human_layout_name(layout["name"]) } - - context "with no translation present" do - it "returns the name capitalized" do - is_expected.to eq("Contact") - end - end - - context "with translation present" do - before { expect(Alchemy).to receive(:t).and_return("Kontakt") } - - it "returns the translated name" do - is_expected.to eq("Kontakt") - end - end - end end end diff --git a/spec/models/alchemy/page/page_layouts_spec.rb b/spec/models/alchemy/page/page_layouts_spec.rb new file mode 100644 index 0000000000..10685adb25 --- /dev/null +++ b/spec/models/alchemy/page/page_layouts_spec.rb @@ -0,0 +1,70 @@ +# frozen_string_literal: true + +require "rails_helper" + +RSpec.describe Alchemy::Page::PageLayouts do + describe ".layouts_with_own_for_select" do + it "should not hold a layout twice" do + layouts = Alchemy::Page.layouts_with_own_for_select("standard", 1, false) + layouts = layouts.collect(&:last) + expect(layouts.count { |l| l == "standard" }).to eq(1) + end + end + + describe ".selectable_layouts" do + let(:site) { create(:alchemy_site) } + let(:language) { create(:alchemy_language, code: :de) } + before { language } + subject { Alchemy::Page.selectable_layouts(language.id) } + + it "should not display hidden page layouts" do + subject.each { |l| expect(l["hide"]).not_to eq(true) } + end + + context "with already taken layouts" do + before do + allow(Alchemy::PageLayout).to receive(:all).and_return([{ "unique" => true }]) + allow(Alchemy::Page).to receive(:where).and_return double(pluck: [1]) + end + + it "should not include unique layouts" do + subject.each { |l| expect(l["unique"]).not_to eq(true) } + end + end + + context "with sites layouts present" do + let(:definition) do + { "name" => "default_site", "page_layouts" => %w(index) } + end + + before do + allow_any_instance_of(Alchemy::Site).to receive(:definition).and_return(definition) + end + + it "should only return layouts for site" do + expect(subject.length).to eq(1) + expect(subject.first["name"]).to eq("index") + end + end + end + + describe ".human_layout_name" do + let(:layout) { { "name" => "contact" } } + + subject { Alchemy::Page.human_layout_name(layout["name"]) } + + context "with no translation present" do + it "returns the name capitalized" do + is_expected.to eq("Contact") + end + end + + context "with translation present" do + before { expect(Alchemy).to receive(:t).and_return("Kontakt") } + + it "returns the translated name" do + is_expected.to eq("Kontakt") + end + end + end +end From 00620aae1668d25bfe1a2a1f324158cdd8d644a5 Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Sat, 16 Jan 2021 17:14:13 +0100 Subject: [PATCH 2/3] Add page layouts repository class option This allows to register your own Alchemy::PageLayout repository class. --- app/models/alchemy/page/page_layouts.rb | 14 +++++++++++++- spec/models/alchemy/page_spec.rb | 10 ++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/app/models/alchemy/page/page_layouts.rb b/app/models/alchemy/page/page_layouts.rb index 526e274a96..0ba5a4f07e 100644 --- a/app/models/alchemy/page/page_layouts.rb +++ b/app/models/alchemy/page/page_layouts.rb @@ -8,6 +8,14 @@ module PageLayouts extend ActiveSupport::Concern module ClassMethods + # Register a custom page layouts repository + # + # The default repository is Alchemy::PageLayout + # + def layouts_repository=(klass) + @_layouts_repository = klass + end + # Returns page layouts ready for Rails' select form helper. # def layouts_for_select(language_id, only_layoutpages = false) @@ -40,7 +48,7 @@ def layouts_with_own_for_select(page_layout_name, language_id, only_layoutpages # def selectable_layouts(language_id, only_layoutpages = false) @language_id = language_id - Alchemy::PageLayout.all.select do |layout| + layouts_repository.all.select do |layout| if only_layoutpages layout["layoutpage"] && layout_available?(layout) else @@ -67,6 +75,10 @@ def human_layout_name(layout) private + def layouts_repository + @_layouts_repository ||= PageLayout + end + # Maps given layouts for Rails select form helper. # def mapped_layouts_for_select(layouts) diff --git a/spec/models/alchemy/page_spec.rb b/spec/models/alchemy/page_spec.rb index 6c662ca95d..18df080b8d 100644 --- a/spec/models/alchemy/page_spec.rb +++ b/spec/models/alchemy/page_spec.rb @@ -225,6 +225,16 @@ module Alchemy # ClassMethods (a-z) + describe ".layouts_repository=" do + let(:dummy_repo) { Class.new } + + it "should be able to set another repository class" do + expect(Alchemy::Page.layouts_repository = dummy_repo).to eq(dummy_repo) + end + + after { Alchemy::Page.instance_variable_set(:@_layouts_repository, nil) } + end + describe ".url_path_class" do subject { described_class.url_path_class } From b53240877c8d4061350eca0870bd09f16a4b8bec Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Wed, 17 Nov 2021 22:49:02 +0100 Subject: [PATCH 3/3] Add page layouts repository class option This allows to register your own Alchemy::PageLayout repository class. --- .../alchemy/admin/pages_controller.rb | 4 +-- app/models/alchemy/page.rb | 4 +-- app/models/alchemy/page/page_layouts.rb | 12 +++---- spec/models/alchemy/page/page_layouts_spec.rb | 31 ++++++++++++++++--- spec/models/alchemy/page_spec.rb | 13 ++++++++ 5 files changed, 49 insertions(+), 15 deletions(-) diff --git a/app/controllers/alchemy/admin/pages_controller.rb b/app/controllers/alchemy/admin/pages_controller.rb index 1b4b14a2ce..00122a7061 100644 --- a/app/controllers/alchemy/admin/pages_controller.rb +++ b/app/controllers/alchemy/admin/pages_controller.rb @@ -78,9 +78,9 @@ def info def new @page ||= Page.new(layoutpage: params[:layoutpage] == "true", parent_id: params[:parent_id]) - @page_layouts = Page.layouts_for_select(@current_language.id, @page.layoutpage?) + @page_layouts = Page.layouts_for_select(@current_language.id, layoutpages: @page.layoutpage?) @clipboard = get_clipboard("pages") - @clipboard_items = Page.all_from_clipboard_for_select(@clipboard, @current_language.id, @page.layoutpage?) + @clipboard_items = Page.all_from_clipboard_for_select(@clipboard, @current_language.id, layoutpages: @page.layoutpage?) end def create diff --git a/app/models/alchemy/page.rb b/app/models/alchemy/page.rb index 9856af6848..d276b3101c 100644 --- a/app/models/alchemy/page.rb +++ b/app/models/alchemy/page.rb @@ -269,11 +269,11 @@ def all_from_clipboard(clipboard) where(id: clipboard.collect { |p| p["id"] }) end - def all_from_clipboard_for_select(clipboard, language_id, layoutpage = false) + def all_from_clipboard_for_select(clipboard, language_id, layoutpages: false) return [] if clipboard.blank? clipboard_pages = all_from_clipboard(clipboard) - allowed_page_layouts = Alchemy::Page.selectable_layouts(language_id, layoutpage) + allowed_page_layouts = Alchemy::Page.selectable_layouts(language_id, layoutpages: layoutpages) allowed_page_layout_names = allowed_page_layouts.collect { |p| p["name"] } clipboard_pages.select { |cp| allowed_page_layout_names.include?(cp.page_layout) } end diff --git a/app/models/alchemy/page/page_layouts.rb b/app/models/alchemy/page/page_layouts.rb index 0ba5a4f07e..1ab41120f7 100644 --- a/app/models/alchemy/page/page_layouts.rb +++ b/app/models/alchemy/page/page_layouts.rb @@ -18,15 +18,15 @@ def layouts_repository=(klass) # Returns page layouts ready for Rails' select form helper. # - def layouts_for_select(language_id, only_layoutpages = false) + def layouts_for_select(language_id, layoutpages: false) @map_array = [] - mapped_layouts_for_select(selectable_layouts(language_id, only_layoutpages)) + mapped_layouts_for_select(selectable_layouts(language_id, layoutpages: layoutpages)) end # Returns page layouts including given layout ready for Rails' select form helper. # - def layouts_with_own_for_select(page_layout_name, language_id, only_layoutpages = false) - layouts = selectable_layouts(language_id, only_layoutpages) + def layouts_with_own_for_select(page_layout_name, language_id, layoutpages: false) + layouts = selectable_layouts(language_id, layoutpages: layoutpages) if layouts.detect { |l| l["name"] == page_layout_name }.nil? @map_array = [[human_layout_name(page_layout_name), page_layout_name]] else @@ -46,10 +46,10 @@ def layouts_with_own_for_select(page_layout_name, language_id, only_layoutpages # @param [Boolean] (false) # Pass true to only select layouts for global/layout pages. # - def selectable_layouts(language_id, only_layoutpages = false) + def selectable_layouts(language_id, layoutpages: false) @language_id = language_id layouts_repository.all.select do |layout| - if only_layoutpages + if layoutpages layout["layoutpage"] && layout_available?(layout) else !layout["layoutpage"] && layout_available?(layout) diff --git a/spec/models/alchemy/page/page_layouts_spec.rb b/spec/models/alchemy/page/page_layouts_spec.rb index 10685adb25..187b8e613a 100644 --- a/spec/models/alchemy/page/page_layouts_spec.rb +++ b/spec/models/alchemy/page/page_layouts_spec.rb @@ -5,22 +5,35 @@ RSpec.describe Alchemy::Page::PageLayouts do describe ".layouts_with_own_for_select" do it "should not hold a layout twice" do - layouts = Alchemy::Page.layouts_with_own_for_select("standard", 1, false) - layouts = layouts.collect(&:last) - expect(layouts.count { |l| l == "standard" }).to eq(1) + Alchemy::Deprecation.silence do + layouts = Alchemy::Page.layouts_with_own_for_select("standard", 1, layoutpages: false) + layouts = layouts.collect(&:last) + expect(layouts.count { |l| l == "standard" }).to eq(1) + end + end + + it "should return layoutpages" do + Alchemy::Deprecation.silence do + layouts = Alchemy::Page.layouts_with_own_for_select("footer", 1, layoutpages: true) + layouts = layouts.collect(&:last) + expect(layouts.count { |l| l == "footer" }).to eq(1) + end end end describe ".selectable_layouts" do let(:site) { create(:alchemy_site) } - let(:language) { create(:alchemy_language, code: :de) } - before { language } + let!(:language) { create(:alchemy_language, code: :de) } subject { Alchemy::Page.selectable_layouts(language.id) } it "should not display hidden page layouts" do subject.each { |l| expect(l["hide"]).not_to eq(true) } end + it "should not display layoutpages" do + subject.each { |l| expect(l["layoutpage"]).not_to eq(true) } + end + context "with already taken layouts" do before do allow(Alchemy::PageLayout).to receive(:all).and_return([{ "unique" => true }]) @@ -46,6 +59,14 @@ expect(subject.first["name"]).to eq("index") end end + + context "with layoutpages set to true" do + subject { Alchemy::Page.selectable_layouts(language.id, layoutpages: true) } + + it "should only return layoutpages" do + subject.each { |l| expect(l["layoutpage"]).to eq(true) } + end + end end describe ".human_layout_name" do diff --git a/spec/models/alchemy/page_spec.rb b/spec/models/alchemy/page_spec.rb index 18df080b8d..3332c1c7fb 100644 --- a/spec/models/alchemy/page_spec.rb +++ b/spec/models/alchemy/page_spec.rb @@ -287,6 +287,19 @@ class AnotherUrlPathClass; end expect(Page.all_from_clipboard_for_select(clipboard, language.id)).to eq([page_1]) end end + + context "with clipboard holding layoutpages and pages." do + let(:page_1) { create(:alchemy_page, :layoutpage, language: language) } + let(:page_2) { create(:alchemy_page, language: language) } + + it "should only return layoutpages" do + clipboard = [ + { "id" => page_1.id.to_s, "action" => "copy" }, + { "id" => page_2.id.to_s, "action" => "copy" }, + ] + expect(Page.all_from_clipboard_for_select(clipboard, language.id, layoutpages: true)).to eq([page_1]) + end + end end describe ".locked" do