Sha256: 9b03b5e497315ac6723336cdc7cc4fe4b8e59c47de89f03655f237f56217d981

Contents?: true

Size: 1.85 KB

Versions: 20

Compression:

Stored size: 1.85 KB

Contents

# The following is taken from custom_error_message plugin by David Easley
# (http://rubyforge.org/projects/custom-err-msg/)
module ActiveRecord
  class Errors

    # Redefine the ActiveRecord::Errors::full_messages method:
    #  Returns all the full error messages in an array. 'Base' messages are handled as usual.
    #  Non-base messages are prefixed with the attribute name as usual UNLESS they begin with '^'
    #  in which case the attribute name is omitted.
    #  E.g. validates_acceptance_of :accepted_terms, :message => '^Please accept the terms of service'
    #
    #
    # Переопределяет метод ActiveRecord::Errors::full_messages. Сообщения об ошибках для атрибутов
    # теперь не имеют префикса с названием атрибута если в сообщении об ошибке первым символом указан "^".
    #
    # Так, например,
    # 
    #   validates_acceptance_of :accepted_terms, :message => 'нужно принять соглашение'
    # 
    # даст сообщение
    # 
    #   Accepted terms нужно принять соглашение
    # 
    # однако,
    # 
    #   validates_acceptance_of :accepted_terms, :message => '^Нужно принять соглашение'
    # 
    # даст сообщение
    # 
    #   Нужно принять соглашение
    def full_messages
      full_messages = []

      @errors.each_key do |attr|
        @errors[attr].each do |msg|
          next if msg.nil?

          if attr == "base"
            full_messages << msg
          elsif msg =~ /^\^/
            full_messages << msg[1..-1]
          else
            full_messages << @base.class.human_attribute_name(attr) + " " + msg
          end
        end
      end

      return full_messages
    end
  end
end

Version data entries

20 entries across 20 versions & 2 rubygems

Version Path
yaroslav-russian-0.0.4 lib/russian/active_record_ext/custom_error_message.rb
yaroslav-russian-0.0.5 lib/russian/active_record_ext/custom_error_message.rb
yaroslav-russian-0.0.6 lib/russian/active_record_ext/custom_error_message.rb
yaroslav-russian-0.0.7 lib/russian/active_record_ext/custom_error_message.rb
yaroslav-russian-0.0.8 lib/russian/active_record_ext/custom_error_message.rb
yaroslav-russian-0.0.9 lib/russian/active_record_ext/custom_error_message.rb
yaroslav-russian-0.1.0 lib/russian/active_record_ext/custom_error_message.rb
yaroslav-russian-0.1.1 lib/russian/active_record_ext/custom_error_message.rb
yaroslav-russian-0.1.2 lib/russian/active_record_ext/custom_error_message.rb
yaroslav-russian-0.1.3 lib/russian/active_record_ext/custom_error_message.rb
russian-0.0.4 lib/russian/active_record_ext/custom_error_message.rb
russian-0.1.2 lib/russian/active_record_ext/custom_error_message.rb
russian-0.1.3 lib/russian/active_record_ext/custom_error_message.rb
russian-0.0.5 lib/russian/active_record_ext/custom_error_message.rb
russian-0.0.8 lib/russian/active_record_ext/custom_error_message.rb
russian-0.0.6 lib/russian/active_record_ext/custom_error_message.rb
russian-0.1.1 lib/russian/active_record_ext/custom_error_message.rb
russian-0.1.0 lib/russian/active_record_ext/custom_error_message.rb
russian-0.0.7 lib/russian/active_record_ext/custom_error_message.rb
russian-0.0.9 lib/russian/active_record_ext/custom_error_message.rb