Finds message information from a model's errors method.
# File lib/shoulda/matchers/active_model/validation_message_finder.rb, line 9 def initialize(instance, attribute, context=nil) @instance = instance @attribute = attribute @context = context end
# File lib/shoulda/matchers/active_model/validation_message_finder.rb, line 15 def allow_description(allowed_values) "allow #{@attribute} to be set to #{allowed_values}" end
# File lib/shoulda/matchers/active_model/validation_message_finder.rb, line 19 def expected_message_from(attribute_message) attribute_message end
# File lib/shoulda/matchers/active_model/validation_message_finder.rb, line 23 def has_messages? errors.present? end
# File lib/shoulda/matchers/active_model/validation_message_finder.rb, line 39 def messages Array.wrap(messages_for_attribute) end
# File lib/shoulda/matchers/active_model/validation_message_finder.rb, line 31 def messages_description if errors.empty? 'no errors' else "errors: #{pretty_error_messages(validated_instance)}" end end
# File lib/shoulda/matchers/active_model/validation_message_finder.rb, line 27 def source_description 'errors' end
# File lib/shoulda/matchers/active_model/validation_message_finder.rb, line 53 def errors validated_instance.errors end
# File lib/shoulda/matchers/active_model/validation_message_finder.rb, line 45 def messages_for_attribute if errors.respond_to?(:[]) errors[@attribute] else errors.on(@attribute) end end
# File lib/shoulda/matchers/active_model/validation_message_finder.rb, line 61 def validate_instance @instance.valid?(@context) @instance end
# File lib/shoulda/matchers/active_model/validation_message_finder.rb, line 57 def validated_instance @validated_instance ||= validate_instance end