Skip to content

Commit

Permalink
Merge pull request #2861 from mamhoff/error-on-non-existing-menu
Browse files Browse the repository at this point in the history
Raise missing template errors from `render_site_layout` and `render_menu`
  • Loading branch information
tvdeyen authored May 8, 2024
2 parents f6b6ad5 + 48be942 commit 6c42bf4
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 12 deletions.
23 changes: 15 additions & 8 deletions app/helpers/alchemy/pages_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,8 @@ def render_page_layout
#
def render_site_layout(&block)
render current_alchemy_site, &block
rescue ActionView::MissingTemplate
warning("Site layout for #{current_alchemy_site.try(:name)} not found. Please run `rails g alchemy:site_layouts`")
""
rescue ActionView::MissingTemplate => error
error_or_warning(error, "Site layout for #{current_alchemy_site.try(:name)} not found. Please run `rails g alchemy:site_layouts`")
end

# Renders a menu partial
Expand All @@ -87,11 +86,8 @@ def render_menu(menu_type, options = {})
end

render("alchemy/menus/#{menu_type}/wrapper", menu: root_node, options: options)
rescue ActionView::MissingTemplate => e
warning <<~WARN
Menu partial not found for #{menu_type}.
#{e}
WARN
rescue ActionView::MissingTemplate => error
error_or_warning(error, "Menu partial for #{menu_type} not found. Please run `rails g alchemy:menus`")
end

# Returns page links in a breadcrumb beginning from root to current page.
Expand Down Expand Up @@ -169,5 +165,16 @@ def meta_keywords
def meta_robots
"#{@page.robot_index? ? "" : "no"}index, #{@page.robot_follow? ? "" : "no"}follow"
end

private

def error_or_warning(error, message)
if Rails.application.config.consider_all_requests_local?
raise error, message
else
Rails.logger.error message
""
end
end
end
end
31 changes: 27 additions & 4 deletions spec/helpers/alchemy/pages_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,22 @@ module Alchemy
end

context "with missing partial" do
it "returns empty string and logges warning" do
expect(helper).to receive(:current_alchemy_site).twice.and_return(default_site)
expect(helper.render_site_layout).to eq("")
context "in production environment" do
before { allow(Rails.application.config).to receive(:consider_all_requests_local?).and_return(false) }

it "returns empty string and logges warning" do
expect(helper).to receive(:current_alchemy_site).twice.and_return(default_site)
expect(helper.render_site_layout).to eq("")
end
end

context "in dev or test environment" do
before { allow(Rails.application.config).to receive(:consider_all_requests_local?).and_return(true) }

it "raises missing template error" do
expect(helper).to receive(:current_alchemy_site).twice.and_return(default_site)
expect { helper.render_site_layout }.to raise_error(ActionView::MissingTemplate)
end
end
end
end
Expand All @@ -66,7 +79,17 @@ module Alchemy
context "but the template does not exist" do
let(:menu_type) { "unknown" }

it { is_expected.to be_nil }
context "in production environment" do
before { allow(Rails.application.config).to receive(:consider_all_requests_local?).and_return(false) }

it { is_expected.to eq("") }
end

context "in dev or test environment" do
before { allow(Rails.application.config).to receive(:consider_all_requests_local?).and_return(true) }

it { expect { subject }.to raise_error(ActionView::MissingTemplate) }
end
end
end

Expand Down

0 comments on commit 6c42bf4

Please sign in to comment.