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+)')}/, }
Constants included from GetText
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, 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_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.
298 299 300 301 302 303 304 305 306 307 308 |
# File 'lib/gettext/active_record.rb', line 298 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:
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 (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.to_i 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:
267 268 269 270 271 272 273 274 275 276 |
# File 'lib/gettext/active_record.rb', line 267 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.
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 = (false)[attribute.to_s] return nil if errors.nil? errors.size == 1 ? errors.first : errors end |