Sha256: 04f470c47dbf0ecf66de750105d6d4bb23b88ef866b0696dc68a9be046001172

Contents?: true

Size: 1.2 KB

Versions: 10

Compression:

Stored size: 1.2 KB

Contents

module Shoulda
  module Matchers
    module ActiveModel
      # Finds message information from exceptions thrown by #valid?
      class ExceptionMessageFinder
        def initialize(instance, attribute)
          @instance = instance
          @attribute = attribute
        end

        def allow_description(allowed_values)
          "doesn't raise when #{@attribute} is set to #{allowed_values}"
        end

        def messages_description
          if has_messages?
            messages.join
          else
            'no exception'
          end
        end

        def has_messages?
          messages.any?
        end

        def messages
          @messages ||= validate_and_rescue
        end

        def source_description
          'exception'
        end

        def expected_message_from(attribute_message)
          "#{human_attribute_name} #{attribute_message}"
        end

        private

        def validate_and_rescue
          @instance.valid?
          []
        rescue ::ActiveModel::StrictValidationFailed => exception
          [exception.message]
        end

        def human_attribute_name
          @instance.class.human_attribute_name(@attribute)
        end
      end

    end
  end
end


Version data entries

10 entries across 10 versions & 2 rubygems

Version Path
challah-1.0.0.beta3 vendor/bundle/gems/shoulda-matchers-1.5.6/lib/shoulda/matchers/active_model/exception_message_finder.rb
challah-1.0.0.beta2 vendor/bundle/gems/shoulda-matchers-1.5.6/lib/shoulda/matchers/active_model/exception_message_finder.rb
challah-1.0.0.beta vendor/bundle/gems/shoulda-matchers-1.5.6/lib/shoulda/matchers/active_model/exception_message_finder.rb
shoulda-matchers-2.0.0 lib/shoulda/matchers/active_model/exception_message_finder.rb
shoulda-matchers-1.5.6 lib/shoulda/matchers/active_model/exception_message_finder.rb
shoulda-matchers-1.5.5 lib/shoulda/matchers/active_model/exception_message_finder.rb
shoulda-matchers-1.5.4 lib/shoulda/matchers/active_model/exception_message_finder.rb
shoulda-matchers-1.5.2 lib/shoulda/matchers/active_model/exception_message_finder.rb
shoulda-matchers-1.5.1 lib/shoulda/matchers/active_model/exception_message_finder.rb
shoulda-matchers-1.5.0 lib/shoulda/matchers/active_model/exception_message_finder.rb