Sha256: d4d02076950616905e5479bd22dbb89e60366acc0f85872459d4301bf97dcf57

Contents?: true

Size: 1.27 KB

Versions: 16

Compression:

Stored size: 1.27 KB

Contents

module Shoulda
  module Matchers
    module ActiveModel
      # @private
      class ValidationMessageFinder
        include Helpers

        def initialize(instance, attribute, context=nil)
          @instance = instance
          @attribute = attribute
          @context = context
        end

        def allow_description(allowed_values)
          "allow #{@attribute} to be set to #{allowed_values}"
        end

        def expected_message_from(attribute_message)
          attribute_message
        end

        def has_messages?
          errors.present?
        end

        def source_description
          'errors'
        end

        def messages_description
          if errors.empty?
            ' no errors'
          else
            " errors:\n#{pretty_error_messages(validated_instance)}"
          end
        end

        def messages
          Array(messages_for_attribute)
        end

        private

        def messages_for_attribute
          errors[@attribute]
        end

        def errors
          validated_instance.errors
        end

        def validated_instance
          @validated_instance ||= validate_instance
        end

        def validate_instance
          @instance.valid?(*@context)
          @instance
        end
      end

    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
shoulda-matchers-4.4.1 lib/shoulda/matchers/active_model/validation_message_finder.rb
shoulda-matchers-4.4.0 lib/shoulda/matchers/active_model/validation_message_finder.rb
shoulda-matchers-4.3.0 lib/shoulda/matchers/active_model/validation_message_finder.rb
shoulda-matchers-4.2.0 lib/shoulda/matchers/active_model/validation_message_finder.rb
shoulda-matchers-4.1.2 lib/shoulda/matchers/active_model/validation_message_finder.rb
shoulda-matchers-4.1.1 lib/shoulda/matchers/active_model/validation_message_finder.rb
shoulda-matchers-4.1.0 lib/shoulda/matchers/active_model/validation_message_finder.rb
shoulda-matchers-4.0.1 lib/shoulda/matchers/active_model/validation_message_finder.rb
shoulda-matchers-3.1.3 lib/shoulda/matchers/active_model/validation_message_finder.rb
shoulda-matchers-4.0.0.rc1 lib/shoulda/matchers/active_model/validation_message_finder.rb
shoulda-matchers-3.1.2 lib/shoulda/matchers/active_model/validation_message_finder.rb
shoulda-matchers-3.1.1 lib/shoulda/matchers/active_model/validation_message_finder.rb
shoulda-matchers-3.1.0 lib/shoulda/matchers/active_model/validation_message_finder.rb
shoulda-matchers-3.0.1 lib/shoulda/matchers/active_model/validation_message_finder.rb
shoulda-matchers-3.0.0 lib/shoulda/matchers/active_model/validation_message_finder.rb
shoulda-matchers-3.0.0.rc1 lib/shoulda/matchers/active_model/validation_message_finder.rb