Skip to content

Commit

Permalink
Add page layouts repository class option
Browse files Browse the repository at this point in the history
This allows to register your own Alchemy::PageLayout repository class.
  • Loading branch information
tvdeyen committed Jan 18, 2023
1 parent 00620aa commit b532408
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 15 deletions.
4 changes: 2 additions & 2 deletions app/controllers/alchemy/admin/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions app/models/alchemy/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions app/models/alchemy/page/page_layouts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
31 changes: 26 additions & 5 deletions spec/models/alchemy/page/page_layouts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }])
Expand All @@ -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
Expand Down
13 changes: 13 additions & 0 deletions spec/models/alchemy/page_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b532408

Please sign in to comment.