Module: CanCan::UnauthorizedMessageResolver

Included in:
Ability
Defined in:
lib/cancan/unauthorized_message_resolver.rb

Instance Method Summary collapse

Instance Method Details

#translate_subject(subject) ⇒ Object


15
16
17
18
19
20
21
22
# File 'lib/cancan/unauthorized_message_resolver.rb', line 15

def translate_subject(subject)
  klass = (subject.class == Class ? subject : subject.class)
  if klass.respond_to?(:model_name)
    klass.model_name.human
  else
    klass.to_s.underscore.humanize.downcase
  end
end

#unauthorized_message(action, subject) ⇒ Object


5
6
7
8
9
10
11
12
13
# File 'lib/cancan/unauthorized_message_resolver.rb', line 5

def unauthorized_message(action, subject)
  subject = subject.values.last if subject.is_a?(Hash)
  keys = unauthorized_message_keys(action, subject)
  variables = {}
  variables[:action] = I18n.translate("actions.#{action}", default: action.to_s)
  variables[:subject] = translate_subject(subject)
  message = I18n.translate(keys.shift, **variables.merge(scope: :unauthorized, default: keys + ['']))
  message.blank? ? nil : message
end