Skip to content

Commit

Permalink
Refactor missing template error handling
Browse files Browse the repository at this point in the history
This extracts the error handling for missing templates into a helper
method.
  • Loading branch information
mamhoff committed May 8, 2024
1 parent 6e874f9 commit 48be942
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
27 changes: 13 additions & 14 deletions app/helpers/alchemy/pages_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,7 @@ def render_page_layout
def render_site_layout(&block)
render current_alchemy_site, &block
rescue ActionView::MissingTemplate => error
if Rails.application.config.consider_all_requests_local?
raise error
else
warning("Site layout for #{current_alchemy_site.try(:name)} not found. Please run `rails g alchemy:site_layouts`")
""
end
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 @@ -92,14 +87,7 @@ def render_menu(menu_type, options = {})

render("alchemy/menus/#{menu_type}/wrapper", menu: root_node, options: options)
rescue ActionView::MissingTemplate => error
if Rails.application.config.consider_all_requests_local?
raise error
else
warning <<~WARN
Menu partial not found for #{menu_type}.
#{error}
WARN
end
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 @@ -177,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
4 changes: 2 additions & 2 deletions spec/helpers/alchemy/pages_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ module Alchemy
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).and_return(default_site)
expect(helper).to receive(:current_alchemy_site).twice.and_return(default_site)
expect { helper.render_site_layout }.to raise_error(ActionView::MissingTemplate)
end
end
Expand Down Expand Up @@ -82,7 +82,7 @@ module Alchemy
context "in production environment" do
before { allow(Rails.application.config).to receive(:consider_all_requests_local?).and_return(false) }

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

context "in dev or test environment" do
Expand Down

0 comments on commit 48be942

Please sign in to comment.