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+)')}/,
}

Constants included from GetText

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, ns_, nsgettext, output_charset, output_charset=, 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:



210
211
212
213
214
215
# File 'lib/gettext/active_record.rb', line 210

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.



298
299
300
301
302
303
304
305
306
307
308
# File 'lib/gettext/active_record.rb', line 298

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:



204
205
206
207
# File 'lib/gettext/active_record.rb', line 204

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

#localize_error_message(attr, msg, append_field) ⇒ Object

:nodoc:



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/gettext/active_record.rb', line 239

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.to_i
      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:



267
268
269
270
271
272
273
274
275
276
# File 'lib/gettext/active_record.rb', line 267

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.



286
287
288
289
290
291
# File 'lib/gettext/active_record.rb', line 286

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