Sha256: 21dc3c223e618eeb708c6aa408657ede3181e7a29a1544228bc8496f5595654a

Contents?: true

Size: 1.31 KB

Versions: 3

Compression:

Stored size: 1.31 KB

Contents

# frozen_string_literal: true

module Pundit
  module Matchers
    module Utils
      module AllActions
        # Adds #message method which generates failed assertion message
        # for *_all_actions matchers.
        #
        # Expects methods to be defined:
        # * matcher - instance which has `AllActions::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 all actions #{expected_kind}, " \
              "but #{mismatches_are(missed_expected_actions)} #{opposite_kind}"
          end

          private

          attr_reader :matcher, :expected_kind, :opposite_kind

          def policy
            matcher.policy
          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/all_actions/error_message_formatter.rb
pundit-matchers-2.2.0 lib/pundit/matchers/utils/all_actions/error_message_formatter.rb
pundit-matchers-2.1.0 lib/pundit/matchers/utils/all_actions/error_message_formatter.rb