Class: ActiveRecord::Errors

Inherits:
Object
  • Object
show all
Includes:
GetText
Defined in:
lib/gettext/active_record.rb

Overview

activerecord-1.14.3/lib/active_record/validations.rb

Constant Summary collapse

@@default_error_messages_d =
{
  default_error_messages[:too_long] => /#{Regexp.escape(default_error_messages[:too_long]).sub(/%d/, '(\d+)')}/,
  default_error_messages[:too_short] => /#{Regexp.escape(default_error_messages[:too_short]).sub(/%d/, '(\d+)')}/,
  default_error_messages[:wrong_length] => /#{Regexp.escape(default_error_messages[:wrong_length]).sub(/%d/, '(\d+)')}/,
  default_error_messages[:greater_than] => /#{Regexp.escape(default_error_messages[:greater_than]).sub(/%d/, '(\d+)')}/,
  default_error_messages[:greater_than_or_equal_to] => /#{Regexp.escape(default_error_messages[:greater_than_or_equal_to]).sub(/%d/, '(\d+)')}/,
  default_error_messages[:equal_to] => /#{Regexp.escape(default_error_messages[:equal_to]).sub(/%d/, '(\d+)')}/,
  default_error_messages[:less_than] => /#{Regexp.escape(default_error_messages[:less_than]).sub(/%d/, '(\d+)')}/,
  default_error_messages[:less_than_or_equal_to] => /#{Regexp.escape(default_error_messages[:less_than_or_equal_to]).sub(/%d/, '(\d+)')}/,
  default_error_messages[:odd] => /#{Regexp.escape(default_error_messages[:odd]).sub(/%d/, '(\d+)')}/,
  default_error_messages[:even] => /#{Regexp.escape(default_error_messages[:even]).sub(/%d/, '(\d+)')}/,
}

Constants included from GetText

GetText::CACHE_BOUND_TARGET_MAX_SIZE, GetText::VERSION

Class Method Summary collapse

Instance Method Summary collapse

Methods included from GetText

N_, #Nn_, _, #add_default_locale_path, bindtextdomain, #bindtextdomain_to, bound_target, bound_targets, cached=, cached?, cgi, cgi=, clear_cache, create_mofiles, current_textdomain_info, each_textdomain, find_targets, gettext, included, locale, locale=, msgmerge, msgmerge_all, n_, ngettext, #npgettext, ns_, nsgettext, output_charset, output_charset=, p_, pgettext, remove_all_textdomains, rgettext, rmsgfmt, rmsgmerge, s_, set_cgi, set_locale, set_locale_all, set_output_charset, setlocale, sgettext, textdomain, #textdomain_to, update_pofiles

Class Method Details

.[]=(id, msg) ⇒ Object

:nodoc:



238
239
240
241
242
243
# File 'lib/gettext/active_record.rb', line 238

def @@default_error_messages.[]=(id, msg) #:nodoc:
  @@default_error_messages.update({id => msg})
  if [:message, :too_long, :too_short, :wrong_length].include?(id)
	@@default_error_messages_d[msg] = /\A#{Regexp.escape(msg).sub(/%d/, '(\d+)')}\Z/ 
  end
end

Instance Method Details

#full_messages_with_gettextObject

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.



343
344
345
346
347
348
349
350
351
352
353
# File 'lib/gettext/active_record.rb', line 343

def full_messages_with_gettext
  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

#initialize_with_gettext(base) ⇒ Object

:nodoc:



232
233
234
235
# File 'lib/gettext/active_record.rb', line 232

def initialize_with_gettext(base) # :nodoc:
  initialize_without_gettext(base)
  bindtextdomain("rails")
end

#localize_error_message(attr, msg, append_field) ⇒ Object

:nodoc:



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/gettext/active_record.rb', line 281

def localize_error_message(attr, msg, append_field) # :nodoc:
  custom_msg = nil
  #Ugly but... :-<
  @@default_error_messages_d.dup.merge(@base.custom_error_messages_d).each do |key, regexp|
    if regexp =~ msg
      custom_msg = @base.gettext(key)
      custom_msg = _(msg) if custom_msg == msg 
      custom_msg = _(custom_msg) % $1
      custom_msg = _(custom_msg) % {:val => $1}
      break
    end
  end

  unless custom_msg
    custom_msg = @base.gettext(msg)
    custom_msg = _(msg) if custom_msg == msg 
  end

  if attr == "base"
    full_message = custom_msg
  elsif /%\{fn\}/ =~ custom_msg
    full_message = custom_msg % {:fn => @base.class.human_attribute_name(attr)}
  elsif append_field
    full_message = @base.class.human_attribute_name(attr) + " " + custom_msg
  else
    full_message = custom_msg
  end
  full_message
end

#localize_error_messages(append_field = true) ⇒ Object

:nodoc:



311
312
313
314
315
316
317
318
319
320
# File 'lib/gettext/active_record.rb', line 311

def localize_error_messages(append_field = true) # :nodoc:
  # e.g.) foo field: "%{fn} foo" => "Foo foo", "foo" => "Foo foo". 
  errors = {}
  each {|attr, msg|
    next if msg.nil?
    errors[attr] ||= []
    errors[attr] << localize_error_message(attr, msg, append_field)
  }
  errors
end

#on_with_gettext(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.



330
331
332
333
334
335
# File 'lib/gettext/active_record.rb', line 330

def on_with_gettext(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