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.



367
368
369
370
371
# File 'lib/form_input/core.rb', line 367

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.



376
377
378
379
380
381
# File 'lib/form_input/core.rb', line 376

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.



387
388
389
390
# File 'lib/form_input/core.rb', line 387

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.



396
397
398
399
# File 'lib/form_input/core.rb', line 396

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