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

Fix i18n lookup for unauthorized message #416

Merged
merged 2 commits into from
Nov 10, 2017
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/cancan/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ def unauthorized_message(action, subject)
keys = unauthorized_message_keys(action, subject)
variables = { action: action.to_s }
variables[:subject] = (subject.class == Class ? subject : subject.class).to_s.underscore.humanize.downcase
message = I18n.translate(nil, variables.merge(scope: :unauthorized, default: keys + ['']))
message = I18n.translate(keys.shift, variables.merge(scope: :unauthorized, default: keys + ['']))
message.blank? ? nil : message
end

Expand Down
2 changes: 1 addition & 1 deletion spec/cancan/ability_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ class Container < Hash

it 'has variables for action and subject' do
# old syntax for now in case testing with old I18n
I18n.backend.store_translations :en, unauthorized: { manage: { all: '%{action} %{subject}' } }
I18n.backend.store_translations :en, unauthorized: { manage: { all: '%<action>s %<subject>s' } }
expect(@ability.unauthorized_message(:update, Array)).to eq('update array')
expect(@ability.unauthorized_message(:update, ArgumentError)).to eq('update argument error')
expect(@ability.unauthorized_message(:edit, 1..3)).to eq('edit range')
Expand Down