Sha256: c2717138e5338b4fa02f18ed0bbb408aa6c7a460d2d6d0ec12406d1f339fce78

Contents?: true

Size: 1.37 KB

Versions: 10

Compression:

Stored size: 1.37 KB

Contents

module Shoulda
  module Matchers
    module ActiveModel

      # Finds message information from a model's #errors method.
      class ValidationMessageFinder
        include Helpers

        def initialize(instance, attribute)
          @instance = instance
          @attribute = attribute
        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: #{pretty_error_messages(validated_instance)}"
          end
        end

        def messages
          Array.wrap(messages_for_attribute)
        end

        private

        def messages_for_attribute
          if errors.respond_to?(:[])
            errors[@attribute]
          else
            errors.on(@attribute)
          end
        end

        def errors
          validated_instance.errors
        end

        def validated_instance
          @validated_instance ||= validate_instance
        end

        def validate_instance
          @instance.valid?
          @instance
        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/validation_message_finder.rb
challah-1.0.0.beta2 vendor/bundle/gems/shoulda-matchers-1.5.6/lib/shoulda/matchers/active_model/validation_message_finder.rb
challah-1.0.0.beta vendor/bundle/gems/shoulda-matchers-1.5.6/lib/shoulda/matchers/active_model/validation_message_finder.rb
shoulda-matchers-2.0.0 lib/shoulda/matchers/active_model/validation_message_finder.rb
shoulda-matchers-1.5.6 lib/shoulda/matchers/active_model/validation_message_finder.rb
shoulda-matchers-1.5.5 lib/shoulda/matchers/active_model/validation_message_finder.rb
shoulda-matchers-1.5.4 lib/shoulda/matchers/active_model/validation_message_finder.rb
shoulda-matchers-1.5.2 lib/shoulda/matchers/active_model/validation_message_finder.rb
shoulda-matchers-1.5.1 lib/shoulda/matchers/active_model/validation_message_finder.rb
shoulda-matchers-1.5.0 lib/shoulda/matchers/active_model/validation_message_finder.rb