Sha256: 3c3e6199ed42d744844e1b8122cae08cc765b9f21da21e7f2f31a65f7504d012

Contents?: true

Size: 1.2 KB

Versions: 11

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

11 entries across 10 versions & 4 rubygems

Version Path
challah-1.0.0.beta vendor/bundle/gems/shoulda-matchers-1.4.2/lib/shoulda/matchers/active_model/exception_message_finder.rb
challah-0.9.1.beta.3 vendor/bundle/gems/shoulda-matchers-1.4.2/lib/shoulda/matchers/active_model/exception_message_finder.rb
devise_sociable-0.1.0 vendor/bundle/gems/shoulda-matchers-1.4.2/lib/shoulda/matchers/active_model/exception_message_finder.rb
challah-0.9.1.beta vendor/bundle/gems/shoulda-matchers-1.4.2/lib/shoulda/matchers/active_model/exception_message_finder.rb
challah-0.9.0 vendor/bundle/gems/shoulda-matchers-1.4.2/lib/shoulda/matchers/active_model/exception_message_finder.rb
shoulda-matchers-1.4.2 lib/shoulda/matchers/active_model/exception_message_finder.rb
challah-rolls-0.2.0 vendor/bundle/gems/challah-0.8.3/vendor/bundle/gems/shoulda-matchers-1.4.1/lib/shoulda/matchers/active_model/exception_message_finder.rb
challah-rolls-0.2.0 vendor/bundle/gems/shoulda-matchers-1.4.1/lib/shoulda/matchers/active_model/exception_message_finder.rb
challah-0.8.3 vendor/bundle/gems/shoulda-matchers-1.4.1/lib/shoulda/matchers/active_model/exception_message_finder.rb
shoulda-matchers-1.4.1 lib/shoulda/matchers/active_model/exception_message_finder.rb
shoulda-matchers-1.4.0 lib/shoulda/matchers/active_model/exception_message_finder.rb