Class: Formalizer::FormField
- Inherits:
-
Object
- Object
- Formalizer::FormField
- Defined in:
- lib/formalizer/form_field.rb
Overview
Holds attributes for a single field in a form.
Attributes
-
id
- Unique textal identifier for the field. See generate_id -
field_type
- One of possible options described in TYPES constant -
name
- field name in current locale. For custom locale use method: name(locale) -
enumeration
- if field_type is :enum or :multiple, list of options for enumeration in current localefor custom locale use method: enumeration(locale)
-
validation
- a hash with validation rules, TBD -
default_value
- the default value in current locale. for custom locale use method: defaut_value(locale)
Constant Summary collapse
- TYPES =
{ boolean: 1, number: 2, text: 3, enum: 4, multiple: 5 }
- LOCALIZED_TYPES =
Prefixes that can be localized in constructor params. See initialze and examples below
[ :name, :enumeration, :default_value ]
Instance Attribute Summary collapse
-
#default_value(locale = I18n.locale) ⇒ Object
readonly
Returns the value of attribute default_value.
-
#enumeration(locale = I18n.locale) ⇒ Object
readonly
Returns the value of attribute enumeration.
-
#field_type ⇒ Object
readonly
Returns the value of attribute field_type.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#name(locale = I18n.locale) ⇒ Object
readonly
getters.
-
#validation ⇒ Object
readonly
Returns the value of attribute validation.
-
#value(locale = I18n.locale) ⇒ Object
Returns the value of attribute value.
Instance Method Summary collapse
-
#initialize(params) ⇒ FormField
constructor
see usage examples at the end:.
-
#render_html(parent_node, locale = I18n.locale) ⇒ Object
Renders the field as HTML control *
parent_node
- the HTML parent under which to render (needed by Nokogiri) *locale
- localized input.
Constructor Details
#initialize(params) ⇒ FormField
see usage examples at the end:
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/formalizer/form_field.rb', line 46 def initialize params validate_params params # populating properties @field_type = params[:field_type].nil? ? TYPES[:text] : TYPES[params[:field_type].to_sym] localize params @validation = params[:validation] || {} @id = params[:id] || generate_id @default_value = params[:default_value] unless params[:default_value].nil? @value = nil end |
Instance Attribute Details
#default_value(locale = I18n.locale) ⇒ Object (readonly)
Returns the value of attribute default_value.
24 25 26 |
# File 'lib/formalizer/form_field.rb', line 24 def default_value @default_value end |
#enumeration(locale = I18n.locale) ⇒ Object (readonly)
Returns the value of attribute enumeration.
24 25 26 |
# File 'lib/formalizer/form_field.rb', line 24 def enumeration @enumeration end |
#field_type ⇒ Object (readonly)
Returns the value of attribute field_type.
24 25 26 |
# File 'lib/formalizer/form_field.rb', line 24 def field_type @field_type end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
24 25 26 |
# File 'lib/formalizer/form_field.rb', line 24 def id @id end |
#name(locale = I18n.locale) ⇒ Object (readonly)
getters
62 63 64 |
# File 'lib/formalizer/form_field.rb', line 62 def name @name end |
#validation ⇒ Object (readonly)
Returns the value of attribute validation.
24 25 26 |
# File 'lib/formalizer/form_field.rb', line 24 def validation @validation end |
#value(locale = I18n.locale) ⇒ Object
Returns the value of attribute value.
24 25 26 |
# File 'lib/formalizer/form_field.rb', line 24 def value @value end |
Instance Method Details
#render_html(parent_node, locale = I18n.locale) ⇒ Object
Renders the field as HTML control
-
parent_node
- the HTML parent under which to render (needed by Nokogiri) -
locale
- localized input. Relevant if field_type is enum or multiple
94 95 96 97 98 99 |
# File 'lib/formalizer/form_field.rb', line 94 def render_html parent_node, locale = I18n.locale inp = send("render_html_#{TYPES.key(@field_type)}", parent_node, locale) inp['id'] = @id inp['name'] = @id return inp end |