Class: DrgcmsFormFields::Select
- Inherits:
-
DrgcmsField
- Object
- DrgcmsField
- DrgcmsFormFields::Select
- Defined in:
- app/models/drgcms_form_fields.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'
-
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
Direct Known Subclasses
Instance Attribute Summary
Attributes inherited from DrgcmsField
Class Method Summary collapse
-
.get_data(params, name) ⇒ Object
Return value.
Instance Method Summary collapse
-
#do_eval(e) ⇒ Object
Return values, when choices options will be returned by evaluating expression.
-
#get_choices ⇒ Object
Create choices array for select field.
-
#render ⇒ Object
Render select field html code.
-
#ro_standard ⇒ Object
Return value when readonly is required.
Methods inherited from DrgcmsField
#hash_to_options, #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.
702 703 704 705 706 707 708 709 710 711 712 |
# File 'app/models/drgcms_form_fields.rb', line 702 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
#do_eval(e) ⇒ Object
Return values, when choices options will be returned by evaluating expression
616 617 618 619 620 621 622 623 |
# File 'app/models/drgcms_form_fields.rb', line 616 def do_eval(e) e.strip! 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 eval e end |
#get_choices ⇒ Object
Create choices array for select field.
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 |
# File 'app/models/drgcms_form_fields.rb', line 628 def get_choices begin choices = case when @yaml['choices'] then (@yaml['choices'].match('helpers.') ) ? t(@yaml['choices']) : @yaml['choices'] when @yaml['eval'] then do_eval(@yaml['eval']) else c = t('helpers.label.' + @form['table'] + '.choices4_' + @yaml['name'] ) c = 'Error' if c.match( 'translation missing' ) c 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.}\n" ['error'] # return empty array when error occures end end |
#render ⇒ Object
Render select field html code
684 685 686 687 688 689 690 691 692 693 694 695 696 697 |
# File 'app/models/drgcms_form_fields.rb', line 684 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_standard ⇒ Object
Return value when readonly is required
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 |
# File 'app/models/drgcms_form_fields.rb', line 653 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 |