Class: ActiveRecord::Errors

Inherits:
Object
  • Object
show all
Extended by:
GetText
Includes:
GetText
Defined in:
lib/gettext_activerecord/validations.rb

Overview

activerecord-1.14.3/lib/active_record/validations.rb

Constant Summary collapse

@@default_error_messages =

To use other backends, gettext_activerecord doesn’t use backend architecture. You can use GetText with other backends.

{
    :inclusion => N_("%{attribute} is not included in the list"),
    :exclusion => N_("%{attribute} is reserved"),
    :invalid => N_("%{attribute} is invalid"),
    :confirmation => N_("%{attribute} doesn't match confirmation"),
    :accepted  => N_("%{attribute} must be accepted"),
    :empty => N_("%{attribute} can't be empty"),
    :blank => N_("%{attribute} can't be blank"),
    :too_long => N_("%{attribute} is too long (maximum is %{count} characters)"),  
    :too_short => N_("%{attribute} is too short (minimum is %{count} characters)"), 
    :wrong_length => N_("%{attribute} is the wrong length (should be %{count} characters)"),
    :taken => N_("%{attribute} has already been taken"),
    :not_a_number => N_("%{attribute} is not a number"),
    :greater_than => N_("%{attribute} must be greater than %{count}"),
    :greater_than_or_equal_to => N_("%{attribute} must be greater than or equal to %{count}"),
    :equal_to => N_("%{attribute} must be equal to %{count}"),
    :less_than => N_("%{attribute} must be less than %{count}"),
    :less_than_or_equal_to => N_("%{attribute} must be less than or equal to %{count}"),
    :odd => N_("%{attribute} must be odd"),
    :even => N_("%{attribute} must be even")
}

Class Method Summary collapse

Instance Method Summary collapse

Methods included from GetText

update_pofiles, update_pofiles_org

Class Method Details

.default_error_messages_with_gettext_activerecordObject



79
80
81
# File 'lib/gettext_activerecord/validations.rb', line 79

def default_error_messages_with_gettext_activerecord
  @@default_error_messages || {}
end

Instance Method Details

#each_with_gettext_activerecordObject

:nodoc:



109
110
111
# File 'lib/gettext_activerecord/validations.rb', line 109

def each_with_gettext_activerecord #:nodoc:
  @errors.each_key { |attr| @errors[attr].each { |msg| yield attr, localize_error_message(attr, msg, false) } }
end

#full_messages_with_gettext_activerecordObject

Returns all the full error messages in an array.

  • If the error messages include %fn, it returns formatted text such as “foo %fn” => “foo Field”

  • else, the error messages are prepended the field name such as “foo” => “Field foo” (Same as default behavior).

As L10n, first one is recommanded because the order of subject,verb and others are not same in languages.



135
136
137
138
139
140
141
142
143
144
145
# File 'lib/gettext_activerecord/validations.rb', line 135

def full_messages_with_gettext_activerecord
  full_messages = []
  errors = localize_error_messages
  errors.each_key do |attr|
    errors[attr].each do |msg|
  next if msg.nil?
      full_messages << msg
    end
  end
  full_messages
end

#on_with_gettext_activerecord(attribute) ⇒ Object

Returns error messages.

  • Returns nil, if no errors are associated with the specified attribute.

  • Returns the error message, if one error is associated with the specified attribute.

  • Returns an array of error messages, if more than one error is associated with the specified attribute.

And for GetText,

  • If the error messages include %fn, it returns formatted text such as “foo %fn” => “foo Field”

  • else, the error messages are prepended the field name such as “foo” => “foo” (Same as default behavior).

Note that this behaviour is different from full_messages.



122
123
124
125
126
127
# File 'lib/gettext_activerecord/validations.rb', line 122

def on_with_gettext_activerecord(attribute)
  # e.g.) foo field: "%{fn} foo" => "Foo foo", "foo" => "foo". 
  errors = localize_error_messages(false)[attribute.to_s]
  return nil if errors.nil?
  errors.size == 1 ? errors.first : errors
end