Sha256: e6cb1382790e436954c2f2e77c1b9c79e3619e4c504e71a3daa63eee724f4d6a

Contents?: true

Size: 1.42 KB

Versions: 6

Compression:

Stored size: 1.42 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, 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: #{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?(@context)
          @instance
        end
      end

    end
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
shoulda-matchers-2.4.0.rc1 lib/shoulda/matchers/active_model/validation_message_finder.rb
shoulda-matchers-2.3.0 lib/shoulda/matchers/active_model/validation_message_finder.rb
challah-1.0.0 vendor/bundle/gems/shoulda-matchers-2.2.0/lib/shoulda/matchers/active_model/validation_message_finder.rb
shoulda-matchers-2.2.0 lib/shoulda/matchers/active_model/validation_message_finder.rb
challah-1.0.0.beta3 vendor/bundle/gems/shoulda-matchers-2.1.0/lib/shoulda/matchers/active_model/validation_message_finder.rb
shoulda-matchers-2.1.0 lib/shoulda/matchers/active_model/validation_message_finder.rb