Class: DrgcmsFormFields::Select

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

Overview

Implementation of select DRG CMS form field.

Form options:

  • name: field name (required)

  • type: select (required)

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

In the example 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.

    • 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'
    
  • depend: Select options may depend on a value in some other field. If depend option is specified then chices must be provided by class method and defined in eval option.

  • html: html options which apply to select field (optional)

Form example:

30:
  name: type
  type: select
40:
  name: parent
  type: select
  eval: DcCategory.values_for_parent
  html:
    include_blank: true      
50:
  name: company
  type: select
  choices: Audi,BMW,Mercedes
60:
  name: type
  type: select
  eval: Cars.choices4_type
  depend: company

Direct Known Subclasses

TextWithSelect, TreeSelect

Instance Attribute Summary

Attributes inherited from DrgcmsField

#js

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from DrgcmsField

#hash_to_options, #html, #initialize, #record_text_for, #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.



184
185
186
187
188
189
190
191
192
193
194
# File 'app/models/drgcms_form_fields/select.rb', line 184

def self.get_data(params, name)
  if params['record'][name].class == Array
    params['record'][name].delete_if {|e| e.blank? }
    return nil if params['record'][name].size == 0
# convert to BSON objects   
    is_id = BSON::ObjectId.legal?(params['record'][name].first)
    return params['record'][name].map{ |e| BSON::ObjectId.from_string(e) } if is_id
    return params['record'][name]
  end
  params['record'][name]
end

Instance Method Details

#choices_in_eval(e, all = false) ⇒ Object

Choices are defined by evaluating an exspression. This is most common class method defined in a class. SomeClass.get_choices4.



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'app/models/drgcms_form_fields/select.rb', line 92

def choices_in_eval(e, all=false)
  e.strip!
  if @yaml['depend'].nil?
    method = e.split(/\ |\(/).first
    return eval(e) if respond_to?(method) # id method defined here
    return eval('@parent.'+e) if @parent.respond_to?(method) # is method defined in helpers
    # eval whatever it is there
    eval e
  else
    # add event listener to depend field
    @js << "$('#record_#{@yaml['depend']}').change( function(e) { update_select_depend('#{@yaml['name']}', '#{@yaml['depend']}','#{e}');})\n"
    depend_value = @record[@yaml['depend']]
    e << " '#{depend_value}'"
    eval e
  end
end

#choices_in_helper(all) ⇒ Object

Choices are defined in helper as: helper.label.table_name.choices_for_fieldname or choices4_tablename_fieldname



78
79
80
81
82
83
84
85
86
# File 'app/models/drgcms_form_fields/select.rb', line 78

def choices_in_helper(all)
  helper = "helpers.label.#{@form['table']}.choices4_#{@yaml['name']}"
  c = t(helper)
  if c.match( 'translation missing' )
    helper = "choices_for_#{@form['table']}_#{@yaml['name']}"
    return "Error. #{helper} not defined" if c.match( 'translation missing' )
  end
  c
end

#get_choices(all = false) ⇒ Object

Create choices array for select field.



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'app/models/drgcms_form_fields/select.rb', line 112

def get_choices(all=false)
  begin
    choices = case 
    when @yaml['choices'] then 
      @yaml['choices']
    when @yaml['eval']    then
      choices_in_eval(@yaml['eval'], all)
    else
      choices_in_helper(all)
    end
  # Convert string to Array
    choices.class == String ?
      choices.chomp.split(',').inject([]) {|r,v| r << (v.match(':') ? v.split(':') : v )} :
      choices
  rescue Exception => e 
    Rails.logger.debug "Error in select eval. #{e.message}\n"
    ['error'] # return empty array when error occures
  end
end

#renderObject

Render select field html code



166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'app/models/drgcms_form_fields/select.rb', line 166

def render
  return ro_standard if @readonly
  set_initial_value('html','selected')
#
  @yaml['html'].symbolize_keys!
  record = record_text_for(@yaml['name'])
  if @yaml['multiple']  
    @html << @parent.select(record, @yaml['name'], get_choices, @yaml['html'], {multiple: true})
    @js   << "$('##{record}_#{@yaml['name']}').selectMultiple();"
  else
    @html << @parent.select(record, @yaml['name'], get_choices, @yaml['html'])
  end
  self
end

#ro_standardObject

Return value when readonly is required



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'app/models/drgcms_form_fields/select.rb', line 135

def ro_standard
  value = @record.respond_to?(@yaml['name']) ? @record[@yaml['name']] : nil
  return self if value.nil?
# 
  choices = get_choices()
  if value.class == Array   # multiple choices
    html = ''
    value.each do |element|
      choices.each do |choice|
        if choice.to_s == element.to_s
          html << '<br>' if html.size > 0
          html << "#{element.to_s}"
        end
      end       
    end
    return super(html)
  else
    choices.each do |choice|
      if choice.class == Array
        return super(choice.first) if choice.last.to_s == value.to_s
      else
        return super(choice) if choice.to_s == value.to_s
      end 
    end
  end
  super('')
end