Sha256: ed16bb3569c9d1271804ce28721dd2412b22abb36e7695359d07436e491b9a1c

Contents?: true

Size: 1.27 KB

Versions: 13

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

13 entries across 13 versions & 1 rubygems

Version Path
shoulda-matchers-6.4.0 lib/shoulda/matchers/active_model/validation_message_finder.rb
shoulda-matchers-6.3.1 lib/shoulda/matchers/active_model/validation_message_finder.rb
shoulda-matchers-6.3.0 lib/shoulda/matchers/active_model/validation_message_finder.rb
shoulda-matchers-6.2.0 lib/shoulda/matchers/active_model/validation_message_finder.rb
shoulda-matchers-6.1.0 lib/shoulda/matchers/active_model/validation_message_finder.rb
shoulda-matchers-6.0.0 lib/shoulda/matchers/active_model/validation_message_finder.rb
shoulda-matchers-5.3.0 lib/shoulda/matchers/active_model/validation_message_finder.rb
shoulda-matchers-5.2.0 lib/shoulda/matchers/active_model/validation_message_finder.rb
shoulda-matchers-5.1.0 lib/shoulda/matchers/active_model/validation_message_finder.rb
shoulda-matchers-5.0.0 lib/shoulda/matchers/active_model/validation_message_finder.rb
shoulda-matchers-5.0.0.rc1 lib/shoulda/matchers/active_model/validation_message_finder.rb
shoulda-matchers-4.5.1 lib/shoulda/matchers/active_model/validation_message_finder.rb
shoulda-matchers-4.5.0 lib/shoulda/matchers/active_model/validation_message_finder.rb