Sha256: 5f87f7c51fd8bba676dcf4f57c8fc6aef2c09afd41a32cdd0ed71dec34222af2

Contents?: true

Size: 814 Bytes

Versions: 8

Compression:

Stored size: 814 Bytes

Contents

# frozen_string_literal: true

module CanCan
  module UnauthorizedMessageResolver
    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

    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
  end
end

Version data entries

8 entries across 8 versions & 2 rubygems

Version Path
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/cancancan-3.6.1/lib/cancan/unauthorized_message_resolver.rb
cancancan-3.5.0 lib/cancan/unauthorized_message_resolver.rb
cancancan-3.4.0 lib/cancan/unauthorized_message_resolver.rb
cancancan-3.3.0 lib/cancan/unauthorized_message_resolver.rb
cancancan-3.2.2 lib/cancan/unauthorized_message_resolver.rb
cancancan-3.2.1 lib/cancan/unauthorized_message_resolver.rb
cancancan-3.2.0 lib/cancan/unauthorized_message_resolver.rb
cancancan-3.1.0 lib/cancan/unauthorized_message_resolver.rb