Class: ActiveRecord::Errors
- Inherits:
-
Object
- Object
- ActiveRecord::Errors
- 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 =
{ [:too_long] => /#{Regexp.escape([:too_long]).sub(/%d/, '(\d+)')}/, [:too_short] => /#{Regexp.escape([:too_short]).sub(/%d/, '(\d+)')}/, [:wrong_length] => /#{Regexp.escape([:wrong_length]).sub(/%d/, '(\d+)')}/, [:greater_than] => /#{Regexp.escape([:greater_than]).sub(/%d/, '(\d+)')}/, [:greater_than_or_equal_to] => /#{Regexp.escape([:greater_than_or_equal_to]).sub(/%d/, '(\d+)')}/, [:equal_to] => /#{Regexp.escape([:equal_to]).sub(/%d/, '(\d+)')}/, [:less_than] => /#{Regexp.escape([:less_than]).sub(/%d/, '(\d+)')}/, [:less_than_or_equal_to] => /#{Regexp.escape([:less_than_or_equal_to]).sub(/%d/, '(\d+)')}/, [:odd] => /#{Regexp.escape([:odd]).sub(/%d/, '(\d+)')}/, [:even] => /#{Regexp.escape([:even]).sub(/%d/, '(\d+)')}/, }
Constants included from GetText
GetText::CACHE_BOUND_TARGET_MAX_SIZE, GetText::VERSION
Class Method Summary collapse
-
.[]=(id, msg) ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#full_messages_with_gettext ⇒ Object
Returns all the full error messages in an array.
-
#initialize_with_gettext(base) ⇒ Object
:nodoc:.
-
#localize_error_message(attr, msg, append_field) ⇒ Object
:nodoc:.
-
#localize_error_messages(append_field = true) ⇒ Object
:nodoc:.
-
#on_with_gettext(attribute) ⇒ Object
Returns error messages.
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_gettext ⇒ Object
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 = [] errors = errors.each_key do |attr| errors[attr].each do |msg| next if msg.nil? << msg end end 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 (attr, msg, append_field) # :nodoc: custom_msg = nil #Ugly but... :-< @@default_error_messages_d.dup.merge(@base.).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" = custom_msg elsif /%\{fn\}/ =~ custom_msg = custom_msg % {:fn => @base.class.human_attribute_name(attr)} elsif append_field = @base.class.human_attribute_name(attr) + " " + custom_msg else = custom_msg end 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 (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] << (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 = (false)[attribute.to_s] return nil if errors.nil? errors.size == 1 ? errors.first : errors end |