Class: DrgcmsFormFields::TextWithSelect

Inherits:
Select show all
Defined in:
app/models/drgcms_form_fields.rb

Overview

Implementation of text_with_select DRG CMS form field. Field will provide text_field entry field with select dropdown box with optional values for the field. Form options are mostly same as in select field.

Form options:

  • name: field name (required)

  • type: text_with_select (required)

  • choices: Values for choices separated by comma. Values can also be specified like description:value.

In this case description will be shown to user, but value will be saved to document.

* choices: 'OK:0,Ready:1,Error:2'
* choices: Ruby,Pyton,PHP
  • eval: Choices will be provided by evaluating expression

    • eval: dc_choices4(‘model_name’,‘description_field_name’,‘_id’); dc_choices4 helper will provide data for select field.

    • eval: ModelName.choices4_field; ModelName class will define method choices4_field which

    will provide data for select field. Since expression is evaluated in the context of Form Field object even session session variables can be accessed. Ex. eval: ‘MyClass.method(@parent.session)’

    • collection_name.search_field_name.method_name; When searching is more complex custom search

    method may be defined in CollectionName model which will provide result set for search.

  • If choices or eval is not defined choices will be provided from translation helpers. For example: Collection has field status choices for field may be provided by en.helpers.model_name.choices4_status entry of english translation. English is of course default translation. If you provide translations in your local language then select choices will be localized.

    • en.helpers.model_name.choices4_status: ‘OK:0,Ready:1,Error:2’

    • sl.helpers.model_name.choices4_status: ‘V redu:0,Pripravljen:1,Napaka:2’

  • html: html options which apply to select and text_field fields (optional)

Form example:

10:
  name: link
  type: text_with_select
  eval: '@parent.dc_page_class.all_pages_for_site(@parent.dc_get_site)'
  html:
    size: 50

Instance Attribute Summary

Attributes inherited from DrgcmsField

#html, #js

Instance Method Summary collapse

Methods inherited from Select

#do_eval, #get_choices, #ro_standard

Methods inherited from DrgcmsField

get_data, #hash_to_options, #initialize, #record_text_for, #ro_standard, #set_initial_value, #t

Constructor Details

This class inherits a constructor from DrgcmsFormFields::DrgcmsField

Instance Method Details

#renderObject

Render text_with_select field html code



1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
# File 'app/models/drgcms_form_fields.rb', line 1248

def render
  return ro_standard if @readonly  
  set_initial_value('html','value')
  
  record = record_text_for(@yaml['name'])
  @html << @parent.text_field( record, @yaml['name'], @yaml['html']) 
  @yaml['html']['class'] = 'text-with-select'
  @yaml['html'].symbolize_keys!
  @html << @parent.select( @yaml['name'] + '_', nil, get_choices, { include_blank: true }, { class: 'text-with-select' })

  # javascript to update text field if new value is selected in select field
  @js =<<EOJS
$(document).ready(function() {
 $('##{@yaml['name']}_').change( function() {
  if ($(this).val().toString().length > 0) {
    $('##{record}_#{@yaml['name']}').val( $(this).val() );
  }
  $('##{record}_#{@yaml['name']}').focus();
 });
});
EOJS
  self
end