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 a8185ce commit 00620aa
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
14 changes: 13 additions & 1 deletion app/models/alchemy/page/page_layouts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions spec/models/alchemy/page_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down

0 comments on commit 00620aa

Please sign in to comment.