Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise missing template errors from render_site_layout and render_menu #2861

Merged
merged 3 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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