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 on non-existing locale #2319

Merged
merged 2 commits into from
Apr 26, 2022
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
7 changes: 4 additions & 3 deletions app/controllers/alchemy/api/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@ class Api::BaseController < Alchemy::BaseController
layout false
respond_to :json

rescue_from CanCan::AccessDenied, with: :render_not_authorized
rescue_from CanCan::AccessDenied, with: :render_not_authorized
rescue_from ActiveRecord::RecordNotFound, with: :render_not_found
rescue_from ActionController::RoutingError, with: :render_not_found

private

def render_not_authorized
render json: {error: "Not authorized"}, status: 403
render json: { error: "Not authorized" }, status: 403
end

def render_not_found
render json: {error: "Record not found"}, status: 404
render json: { error: "Record not found" }, status: 404
end
end
end
3 changes: 2 additions & 1 deletion lib/alchemy/controller_actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ def set_alchemy_language(lang = nil)

def load_alchemy_language_from_params
if params[:locale].present?
Language.find_by_code(params[:locale])
Language.find_by_code(params[:locale]) ||
raise(ActionController::RoutingError, "Language not found")
end
end

Expand Down
9 changes: 4 additions & 5 deletions spec/libraries/controller_actions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,12 @@
context "for language that does not exist" do
before do
allow(controller).to receive(:params).and_return(locale: "fo")
controller.send :set_alchemy_language
end

it "should set the language to default" do
expect(assigns(:language)).to eq(default_language)
expect(Alchemy::Language.current).to eq(default_language)
expect(controller.session).to include_language_information_for(default_language)
it "should raise an error" do
expect do
controller.send :set_alchemy_language
end.to raise_exception(ActionController::RoutingError, "Language not found")
end
end
end
Expand Down