Sha256: a8bc11a5ac7aed54b27e705138cfea0ee529c4429447c66ac8da9dd174efc489

Contents?: true

Size: 1.59 KB

Versions: 3

Compression:

Stored size: 1.59 KB

Contents

# frozen_string_literal: true

module Pundit
  module Matchers
    module Utils
      module OnlyActions
        # Adds #message method which generates failed assertion message
        # for *_only_actions matchers.
        #
        # Expects methods to be defined:
        # * matcher - instance which has `OnlyActions::ActionsMatcher` as a parent class
        # * expected_kind - string with expected actions type (can be "forbidden" or "permitted")
        # * opposite_kind - string with oposite then expected actions type (can be "permitted" or "forbidden")
        module ErrorMessageFormatter
          def message
            "#{policy_name} expected to have only actions #{matcher.expected_actions} #{expected_kind}, but " \
              "#{unless missed_expected_actions.empty?
                   "#{mismatches_are(missed_expected_actions)} #{opposite_kind} and "
                 end}" \
              "#{mismatches_are(unexpected_actions)} #{expected_kind} too"
          end

          private

          attr_reader :matcher, :expected_kind, :opposite_kind

          def policy
            matcher.policy
          end

          def unexpected_actions
            matcher.unexpected_actions
          end

          def missed_expected_actions
            matcher.missed_expected_actions
          end

          def policy_name
            policy.class.name
          end

          def mismatches_are(mismatches)
            if mismatches.count == 1
              "#{mismatches} is"
            else
              "#{mismatches} are"
            end
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
pundit-matchers-2.3.0 lib/pundit/matchers/utils/only_actions/error_message_formatter.rb
pundit-matchers-2.2.0 lib/pundit/matchers/utils/only_actions/error_message_formatter.rb
pundit-matchers-2.1.0 lib/pundit/matchers/utils/only_actions/error_message_formatter.rb