Module: FormInput::Parameter::LocaleMethods

Included in:
FormInput::Parameter
Defined in:
lib/form_input/core.rb

Overview

Methods affected by localization, put in separate module for easier overloading.

Instance Method Summary collapse

Instance Method Details

#[](name) ⇒ Object

Get value of arbitrary option. Automatically resolves call blocks.



324
325
326
327
328
# File 'lib/form_input/core.rb', line 324

def []( name )
  o = opts[ name ]
  o = instance_exec( &o ) if o.is_a?( Proc )
  o
end

#format_error_message(msg, count = nil, singular = nil, plural = "#{singular}s") ⇒ Object

Format the error report message. Default implementation includes simple pluralizer. String %p in the message is automatically replaced with error title. Can be redefined to provide correctly localized error messages.



333
334
335
336
337
338
# File 'lib/form_input/core.rb', line 333

def format_error_message( msg, count = nil, singular = nil, plural = "#{singular}s" )
  msg = DEFAULT_ERROR_MESSAGES[ msg ] || msg.to_s
  msg += " #{count}" if count
  msg += " #{ count == 1 ? singular : plural }" if singular
  msg.gsub( '%p', error_title )
end

#report(msg, *args) ⇒ Object

Report an error concerning this parameter. String %p in the message is automatically replaced with error title. In case of multiple errors, the message is added to the end of the list, making it less important than the other errors. Returns self for chaining.



344
345
346
347
# File 'lib/form_input/core.rb', line 344

def report( msg, *args )
  form.report( name, format_error_message( msg, *args ) ) if form
  self
end

#report!(msg, *args) ⇒ Object

Report an error concerning this parameter. String %p in the message is automatically replaced with error title. In case of multiple errors, the message is added to the beginning of the list, making it more important than the other errors. Returns self for chaining.



353
354
355
356
# File 'lib/form_input/core.rb', line 353

def report!( msg, *args )
  form.report!( name, format_error_message( msg, *args ) ) if form
  self
end