Class: DrgcmsFormFields::TextAutocomplete

Inherits:
DrgcmsField
  • Object
show all
Defined in:
app/models/drgcms_form_fields.rb

Overview

Implementation of text_autocomplete DRG CMS form field.

Form options:

  • name: field name (required)

  • type: text_autocomplete (required)

  • table Collection (table) name. When defined search must contain field name

  • search: Search may consist of three parameters from which are separated either by dot (.)

    • search_field_name; when table option is defined search must define field name which will be used for search query

    • collection_name.search_field_name; Same as above except that table options must be ommited.

    • 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.

Form example:

10:
  name: user_id
  type: text_autocomplete
  search: dc_user.name
  html:
    size: 30

Instance Attribute Summary

Attributes inherited from DrgcmsField

#html, #js

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from DrgcmsField

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

Constructor Details

This class inherits a constructor from DrgcmsFormFields::DrgcmsField

Class Method Details

.get_data(params, name) ⇒ Object

Return value. Return nil if input field is empty



1245
1246
1247
# File 'app/models/drgcms_form_fields.rb', line 1245

def self.get_data(params, name)
  params['record']["_#{name}"].blank? ? nil : params['record'][name]
end

Instance Method Details

#renderObject

Render text_autocomplete field html code



1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
# File 'app/models/drgcms_form_fields.rb', line 1158

def render
# Return descriptive text and put it into input field
# search field name
  if @yaml['search'].class == Hash
    table    = @yaml['search']['table']
    ret_name = @yaml['search']['field']
    method   = @yaml['search']['method']
  elsif @yaml['search'].match(/\./)
    table, ret_name, method = @yaml['search'].split('.') 
  else
    ret_name = @yaml['search']
  end
# determine table name 
  if @yaml['table']
    table = if @yaml['table'].class == String
      @yaml['table']
# eval(how_to_get_my_table_name)    
    elsif @yaml['table']['eval']
      eval @yaml['table']['eval']
    else
      p "Field #{@yaml['name']}: Invalid table parameter!"
      nil
    end
  end
  return 'Table or field keyword not defined!' unless (table and ret_name)
# TODO check if table exists 
  t = table.classify.constantize
# find record and return value of field
  value_send_as = 'p_' + @yaml['name']
  value = if @parent.params[value_send_as]
    @parent.params[value_send_as]
  elsif @record and @record[@yaml['name']]
    @record[@yaml['name']]
  end
# Found value to be written in field. If field is not found write out value.
  if value
    record = t.find(value)
    value_displayed = record ? record.send(ret_name) : value
  end
# return if readonly
  return ro_standard(value_displayed) if @readonly
# Add method back, so autocomplete will know that it must search for method inside class
  ret_name = "#{ret_name}.#{method}" if method
  @yaml['html'] ||= {}
  @yaml['html']['value'] = value_displayed
  @yaml['html']['placeholder'] ||= t('drgcms.search_placeholder') || nil
#    
  _name = '_' + @yaml['name']
  record = record_text_for(@yaml['name'])  
  @html << @parent.text_field(record, _name, @yaml['html'])
  if @yaml['with_new']
    @html << ' ' + 
             @parent.fa_icon('plus-square lg', class: 'in-edit-add', title: t('drgcms.new'), 
             style: "vertical-align: top;", 'data-table' => @yaml['with_new'] )    
  end
  @html << @parent.hidden_field(record, @yaml['name'], value: value)        # actual value will be in hidden field
# JS stuff    
  @js << <<EOJS
$(document).ready(function() {
  $("##{record}_#{_name}").autocomplete( {
    source: function(request, response) {
      $.ajax({
        url: '/dc_common/autocomplete',
        type: "POST",
        dataType: "json",
        data: { input: request.term, table: "#{table}", search: "#{ret_name}" #{(',id: "'+@yaml['id'] + '"') if @yaml['id']} },
        success: function(data) {
          response( $.map( data, function(key) {
            return key;
          }));
        }
      });
    },
    change: function (event, ui) { 
      $("##{record}_#{@yaml['name']}").val(ui.item.id);
    },
    minLength: 2
  });
});
EOJS
    
  self 
end