Repository: rails/rails
File: activemodel/lib/active_model/errors.rb (Ruby)

Code before:
    #   person = Person.create(address: '123 First St.')
    #   person.errors.full_messages
    #   # => ["Name is too short (minimum is 5 characters)", "Name can't be blank", "Email can't be blank"]
    def full_messages
      @errors.map(&:full_message)
    end
    alias :to_a :full_messages

    # Returns all the full error messages for a given attribute
    # and type (optional) in an array.
    #
    #   class Person
    #     validates_presence_of :name, :email
    #     validates_length_of :name, in: 5..30
    #   end
    #
    #   person = Person.create()
    #   person.errors.full_messages_for(:name)
    #   # => ["Name is too short (minimum is 5 characters)", "Name can't be blank"]
    #
    #   person.errors.full_messages_for(:name, :invalid)
    #   # => ["Name is invalid"]
    def full_messages_for(attribute, type = nil)
      error_args = [attribute, type].compact

      where(*error_args).map(&:full_message).freeze
    end

    # Returns all the error messages for a given attribute
    # and type (optional) in an array.
    #
    #   class Person
    #     validates_presence_of :name, :email
    #     validates_length_of :name, in: 5..30
    #   end
    #
    #   person = Person.create()
    #   person.errors.messages_for(:name)
    #   # => ["is too short (minimum is 5 characters)", "can't be blank"]
    #
    #   person.errors.messages_for(:name, :invalid)
    #   # => ["is invalid"]
    def messages_for(attribute, type = nil)
      error_args = [attribute, type].compact

      where(*error_args).map(&:message)
    end

    # Returns a full message for a given attribute.
    #
    #   person.errors.full_message(:name, 'is invalid') # => "Name is invalid"

Review comment (suggestion):
This will cause 2 arrays allocation, which is bad. I am not sure why you are doing that.

 ```suggestion
      where(attribute, type).map(&:full_message).freeze
```

Same for `messages_for` below.

Code after:
    def full_messages
      @errors.map(&:full_message)
    end
    alias :to_a :full_messages

    # Returns all the full error messages for a given attribute
    # and type (optional) in an array.
    #
    #   class Person
    #     validates_presence_of :name, :email
    #     validates_length_of :name, in: 5..30
    #   end
    #
    #   person = Person.create()
    #   person.errors.full_messages_for(:name)
    #   # => ["Name is too short (minimum is 5 characters)", "Name can't be blank"]
    #
    #   person.errors.full_messages_for(:name, :invalid)
    #   # => ["Name is invalid"]
    def full_messages_for(attribute, type = nil)
      where(attribute, type).map(&:full_message).freeze
    end

    # Returns all the error messages for a given attribute
    # and type (optional) in an array.
    #
    #   class Person
    #     validates_presence_of :name, :email
    #     validates_length_of :name, in: 5..30
    #   end
    #
    #   person = Person.create()
    #   person.errors.messages_for(:name)
    #   # => ["is too short (minimum is 5 characters)", "can't be blank"]
    #
    #   person.errors.messages_for(:name, :invalid)
    #   # => ["is invalid"]
    def messages_for(attribute, type = nil)
      where(attribute, type).map(&:message)
    end

    # Returns a full message for a given attribute.
    #
    #   person.errors.full_message(:name, 'is invalid') # => "Name is invalid"
    def full_message(attribute, message)
      Error.full_message(attribute, message, @base)
    end

    # Translates an error message in its default scope
    # (<tt>activemodel.errors.messages</tt>).
    #