drg_cms 0.5.52.16 → 0.6.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (133) hide show
  1. checksums.yaml +4 -4
  2. data/app/assets/javascripts/drg_cms/drg_cms.js +446 -144
  3. data/app/assets/javascripts/drg_cms/jstree.min.js +6 -6
  4. data/app/assets/javascripts/drg_cms_application.js +1 -1
  5. data/app/assets/stylesheets/drg_cms/drg_cms.css +625 -243
  6. data/app/assets/stylesheets/drg_cms/jstree.css +6 -12
  7. data/app/assets/stylesheets/drg_cms/select-multiple.css +18 -12
  8. data/app/assets/stylesheets/drg_cms_cms.css +1 -1
  9. data/app/controllers/cmsedit_controller.rb +536 -331
  10. data/app/controllers/dc_application_controller.rb +380 -117
  11. data/app/controllers/dc_common_controller.rb +105 -14
  12. data/app/{controllers → controls}/browse_models_control.rb +0 -0
  13. data/app/controls/dc_help_control.rb +126 -0
  14. data/app/{controllers → controls}/dc_page_control.rb +24 -8
  15. data/app/controls/dc_poll_result_control.rb +88 -0
  16. data/app/controls/dc_report.rb +225 -0
  17. data/app/{controllers → controls}/design_element_settings_control.rb +0 -0
  18. data/app/forms/all_options.yml +50 -9
  19. data/app/forms/cms_menu.yml +28 -18
  20. data/app/forms/dc_ad.yml +11 -22
  21. data/app/forms/dc_big_table.yml +1 -0
  22. data/app/forms/dc_big_table_value.yml +1 -0
  23. data/app/forms/dc_category.yml +2 -1
  24. data/app/forms/dc_design.yml +20 -16
  25. data/app/forms/dc_filter.yml +3 -6
  26. data/app/forms/dc_help_1.yml +109 -0
  27. data/app/forms/dc_journal.yml +3 -1
  28. data/app/forms/dc_json_ld.yml +59 -0
  29. data/app/forms/dc_key_value.yml +32 -0
  30. data/app/forms/dc_link.yml +1 -1
  31. data/app/forms/dc_menu.yml +2 -0
  32. data/app/forms/dc_menu_item.yml +2 -0
  33. data/app/forms/dc_page.yml +3 -5
  34. data/app/forms/dc_part.yml +1 -0
  35. data/app/forms/dc_piece.yml +1 -0
  36. data/app/forms/dc_poll.yml +19 -9
  37. data/app/forms/dc_poll_item.yml +2 -1
  38. data/app/forms/dc_poll_result.yml +83 -0
  39. data/app/forms/dc_poll_result_export.yml +35 -0
  40. data/app/forms/dc_seo.yml +33 -0
  41. data/app/forms/dc_simple_menu.yml +2 -0
  42. data/app/forms/dc_site.yml +4 -12
  43. data/app/forms/dc_user.yml +27 -11
  44. data/app/forms/dc_user_role.yml +3 -0
  45. data/app/forms/json_ld_schema.yml +168 -0
  46. data/app/helpers/cms_common_helper.rb +376 -0
  47. data/app/helpers/cms_edit_helper.rb +506 -0
  48. data/app/helpers/cms_helper.rb +268 -0
  49. data/app/helpers/cms_index_helper.rb +576 -0
  50. data/app/helpers/dc_application_helper.rb +183 -271
  51. data/app/models/concerns/dc_page_concern.rb +40 -6
  52. data/app/models/concerns/dc_policy_rule_concern.rb +20 -8
  53. data/app/models/concerns/dc_seo_concern.rb +66 -0
  54. data/app/models/concerns/dc_site_concern.rb +67 -44
  55. data/app/models/concerns/dc_user_concern.rb +62 -21
  56. data/app/models/dc_design.rb +30 -18
  57. data/app/models/dc_filter.rb +22 -13
  58. data/app/models/dc_json_ld.rb +152 -0
  59. data/app/models/dc_key_value.rb +48 -0
  60. data/app/models/dc_key_value_store.rb +1 -0
  61. data/app/models/dc_memory.rb +8 -1
  62. data/app/models/dc_page.rb +0 -1
  63. data/app/models/dc_permission.rb +49 -9
  64. data/app/models/dc_policy.rb +25 -14
  65. data/app/models/dc_policy_role.rb +22 -11
  66. data/app/models/dc_poll.rb +39 -19
  67. data/app/models/{drgcms_form_fields/drgcms_form_fields.rb → dc_poll_result.rb} +23 -2
  68. data/app/models/dc_temp.rb +140 -0
  69. data/app/models/dc_user_role.rb +2 -2
  70. data/app/models/drgcms_form_fields.rb +14 -6
  71. data/app/models/drgcms_form_fields/action.rb +61 -0
  72. data/app/models/drgcms_form_fields/check_box.rb +3 -0
  73. data/app/models/drgcms_form_fields/comment.rb +8 -4
  74. data/app/models/drgcms_form_fields/date_picker.rb +11 -9
  75. data/app/models/drgcms_form_fields/date_select.rb +1 -1
  76. data/app/models/drgcms_form_fields/{date_time_picker.rb → datetime_picker.rb} +12 -10
  77. data/app/models/drgcms_form_fields/datetime_select.rb +1 -1
  78. data/app/models/drgcms_form_fields/drgcms_field.rb +55 -9
  79. data/app/models/drgcms_form_fields/embedded.rb +26 -16
  80. data/app/models/drgcms_form_fields/file_field.rb +52 -0
  81. data/app/models/drgcms_form_fields/file_select.rb +2 -2
  82. data/app/models/drgcms_form_fields/hash_field.rb +90 -0
  83. data/app/models/drgcms_form_fields/hidden_field.rb +1 -1
  84. data/app/models/drgcms_form_fields/html_field.rb +1 -1
  85. data/app/models/drgcms_form_fields/link_to.rb +2 -2
  86. data/app/models/drgcms_form_fields/method.rb +66 -0
  87. data/app/models/drgcms_form_fields/multitext_autocomplete.rb +23 -14
  88. data/app/models/drgcms_form_fields/number_field.rb +15 -6
  89. data/app/models/drgcms_form_fields/radio.rb +96 -0
  90. data/app/models/drgcms_form_fields/readonly.rb +2 -2
  91. data/app/models/drgcms_form_fields/select.rb +92 -29
  92. data/app/models/drgcms_form_fields/text_area.rb +1 -1
  93. data/app/models/drgcms_form_fields/text_autocomplete.rb +29 -17
  94. data/app/models/drgcms_form_fields/text_field.rb +1 -1
  95. data/app/models/drgcms_form_fields/text_with_select.rb +6 -3
  96. data/app/models/drgcms_form_fields/tree_select.rb +29 -10
  97. data/app/{helpers → renderers}/dc_ad_renderer.rb +0 -0
  98. data/app/{helpers → renderers}/dc_big_menu_renderer.rb +1 -0
  99. data/app/{helpers → renderers}/dc_captcha_renderer.rb +0 -0
  100. data/app/{helpers → renderers}/dc_common_renderer.rb +0 -0
  101. data/app/{helpers → renderers}/dc_gallery_renderer.rb +1 -0
  102. data/app/{helpers → renderers}/dc_menu_renderer.rb +11 -4
  103. data/app/{helpers → renderers}/dc_page_renderer.rb +1 -0
  104. data/app/{helpers → renderers}/dc_part_renderer.rb +5 -4
  105. data/app/{helpers → renderers}/dc_piece_renderer.rb +1 -1
  106. data/app/{helpers → renderers}/dc_poll_renderer.rb +84 -48
  107. data/app/{helpers → renderers}/dc_renderer.rb +1 -0
  108. data/app/{helpers → renderers}/dc_simple_menu_renderer.rb +1 -0
  109. data/app/views/cmsedit/{remove_edit_stuff.js.erb → __remove_edit_stuff.js.erb} +0 -0
  110. data/app/views/cmsedit/{show.html.erb → __show.html.erb} +0 -0
  111. data/app/views/cmsedit/_edit_stuff.html.erb +4 -25
  112. data/app/views/cmsedit/_form.html.erb +4 -3
  113. data/app/views/cmsedit/_result.html.erb +2 -3
  114. data/app/views/cmsedit/edit.html.erb +4 -1
  115. data/app/views/cmsedit/index.html.erb +7 -1
  116. data/app/views/cmsedit/new.html.erb +3 -1
  117. data/app/views/dc_common/_help.html.erb +8 -0
  118. data/app/views/layouts/models.html.erb +2 -1
  119. data/config/locales/drgcms_en.yml +26 -3
  120. data/config/locales/drgcms_sl.yml +31 -5
  121. data/config/locales/models_en.yml +66 -18
  122. data/config/locales/models_sl.yml +71 -20
  123. data/drg_cms.gemspec +3 -3
  124. data/lib/drg_cms.rb +63 -1
  125. data/lib/drg_cms/version.rb +2 -2
  126. data/lib/generators/new_drg_form/new_drg_form_generator.rb +9 -2
  127. data/lib/tasks/database.rake +6 -56
  128. metadata +81 -63
  129. data/app/assets/stylesheets/drg_cms/__jquery-ui.css +0 -339
  130. data/app/helpers/application_helper.rb +0 -2
  131. data/app/helpers/cmsedit_helper.rb +0 -885
  132. data/app/models/dc_dummy.rb +0 -102
  133. data/test/fixtures/drg_cms_test_data.rb +0 -87
@@ -42,7 +42,7 @@ class HiddenField < DrgcmsField
42
42
  ###########################################################################
43
43
  def render
44
44
  set_initial_value
45
- value = @yaml['html']['value'] ? @yaml['html']['value'] : @record[@yaml['name']]
45
+ value = @yaml['html']['value'] ? @yaml['html']['value'] : @record.send(@yaml['name'])
46
46
  record = record_text_for(@yaml['name'])
47
47
  @parent.hidden_field(record, @yaml['name'], value: value)
48
48
  end
@@ -49,7 +49,7 @@ class HtmlField < DrgcmsField
49
49
  # Render html_field field html code
50
50
  ###########################################################################
51
51
  def render
52
- return ro_standard if @readonly
52
+ #return ro_standard if @readonly
53
53
  # retrieve html editor from page settings
54
54
  editor_string = @parent.dc_get_site.params['html_editor'] if @parent.dc_get_site
55
55
  editor_string ||= 'ckeditor'
@@ -53,13 +53,13 @@ def render
53
53
  @yaml['html'] ||= {}
54
54
  @yaml['html']['class'] ||= 'dc-link dc-animate'
55
55
  @yaml['html'].symbolize_keys!
56
- #
56
+
57
57
  @yaml[:id] = record._id if @yaml[:id] == 'id'
58
58
  url = @yaml['url'] || "#{@yaml[:controller]}/#{@yaml[:action]}/#{@yaml[:id]}"
59
59
  url.gsub!('//','/') # no action and id
60
60
  url = '/' + @yaml['url'] unless url[0,1] == '/' # no leading /
61
61
  url.chop if url[0,-1] == '/' # remove trailing /
62
- #
62
+
63
63
  caption = @yaml['caption'] || @yaml['text']
64
64
  @html << @parent.dc_link_to(caption, @yaml['icon'], url, @yaml['html'])
65
65
  self
@@ -0,0 +1,66 @@
1
+ #--
2
+ # Copyright (c) 2012+ Damjan Rems
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ #++
23
+ module DrgcmsFormFields
24
+
25
+ ###########################################################################
26
+ # Implementation of custom DRG CMS form field. Method field will call method or class method
27
+ # defined in eval option and add returned code to HTML output code.
28
+ #
29
+ # It can be used in case when form contains complex data structure. For example set of pictures which can not
30
+ # be simply displayed by any other DRG Forms field.
31
+ #
32
+ # Form example:
33
+ # 50:
34
+ # name: galery
35
+ # type: method
36
+ # eval: show_gallery
37
+ # or
38
+ # eval: MyClass.show_gallery
39
+ #
40
+ ###########################################################################
41
+ class Method < DrgcmsField
42
+
43
+ ###########################################################################
44
+ # Render file_select field html code
45
+ ###########################################################################
46
+ def render
47
+ # might be defined as my_method or MyClass.my_method
48
+ clas, method = @yaml['eval'].split(/\.|\,/).map(&:strip)
49
+ if method.nil?
50
+ if @parent.respond_to?(clas)
51
+ @html << @parent.send(clas, @record, @yaml, @readonly)
52
+ return self
53
+ end
54
+ else
55
+ klass = clas.camelize.constantize
56
+ if klass.respond_to?(method)
57
+ @html << klass.send(method, @record, @yaml, @readonly)
58
+ return self
59
+ end
60
+ end
61
+ @html << "Error: #{@yaml['name']} : #{@yaml['eval']} not defined!"
62
+ self
63
+ end
64
+
65
+ end
66
+ end
@@ -23,7 +23,7 @@
23
23
  module DrgcmsFormFields
24
24
 
25
25
  ###########################################################################
26
- # Implementation of multitext_autocomplete DRG CMS form field.
26
+ # Implementation of multitext_autocomplete DRG Form field.
27
27
  #
28
28
  # multitext_autocomplete field is complex data entry field which uses autocomplete
29
29
  # function when selecting multiple values for MongoDB Array field. Array typically holds
@@ -33,20 +33,21 @@ module DrgcmsFormFields
33
33
  # ===Form options:
34
34
  # * +name:+ field name (required)
35
35
  # * +type:+ multitext_autocomplete (required)
36
- # * +table+ Collection (table) name. When defined search must contain field name
36
+ # * +table+ Model (table) name which must contain searched field name.
37
37
  # * +search:+ Search may consist of three parameters from which are separated either by dot (.) or comma(,)
38
38
  # * search_field_name; when table option is defined search must define field name which will be used for search query
39
39
  # * collection_name.search_field_name; Same as above except that table options must be ommited.
40
40
  # * collection_name.search_field_name.method_name; When searching is more complex custom search
41
41
  # method may be defined in CollectionName model which will provide result set for search.
42
+ # * +with_new+ Will add an icon for shortcut to add new document to collection
42
43
  #
43
44
  # Form example:
44
45
  # 90:
45
46
  # name: kats
46
47
  # type: multitext_autocomplete
47
48
  # search: dc_category.name
48
- # html:
49
- # size: 30
49
+ # with_new: model_name
50
+ # size: 30
50
51
  ###########################################################################
51
52
  class MultitextAutocomplete < DrgcmsField
52
53
 
@@ -76,7 +77,7 @@ def render
76
77
  method = @yaml['search']['method']
77
78
  search = method.nil? ? field_name : "#{field_name}.#{method}"
78
79
  elsif @yaml['search'].to_s.match(/\./)
79
- table, field_name, method = @yaml['search'].split(/\.|\,/)
80
+ table, field_name, method = @yaml['search'].split(/\.|\,/).map(&:strip)
80
81
  search = method.nil? ? field_name : "#{field_name}.#{method}"
81
82
  else # search and table name are separated
82
83
  search = field_name = @yaml['search']
@@ -89,7 +90,7 @@ def render
89
90
  elsif @yaml['table']['eval']
90
91
  eval @yaml['table']['eval']
91
92
  else
92
- p "Field #{@yaml['name']}: Invalid table parameter!"
93
+ @parent.logger.error "Field #{@yaml['name']}: Invalid table parameter!"
93
94
  nil
94
95
  end
95
96
  end
@@ -97,15 +98,14 @@ def render
97
98
  @html << 'Table or search field not defined!'
98
99
  return self
99
100
  end
100
- #
101
- return ro_standard(table, search) if @readonly
102
- # TODO check if table exists
101
+ #
102
+ # TODO check if table exists
103
103
  collection = table.classify.constantize
104
104
  unless @record.respond_to?(@yaml['name'])
105
105
  @html << "Invalid field name: #{@yaml['name']}"
106
106
  return self
107
107
  end
108
- # put field to enter search data on form
108
+ # put field to enter search data on form
109
109
  @yaml['html'] ||= {}
110
110
  @yaml['html']['value'] = '' # must be. Otherwise it will look into record and return error
111
111
  @yaml['html']['placeholder'] = t('drgcms.search_placeholder')
@@ -113,10 +113,18 @@ def render
113
113
  @html << '<div class="ui-autocomplete-border">'
114
114
  @html << @parent.link_to(@parent.fa_icon('plus-square lg', class: 'dc-animate dc-green'), '#',onclick: 'return false;') # dummy add. But it is usefull.
115
115
 
116
- record = record_text_for(@yaml['name'])
117
- @html << ' ' << @parent.text_field(record, _name, @yaml['html']) # text field for autocomplete
118
- @html << "<div id =\"#{record}#{@yaml['name']}\">" # div to list active records
119
- # find value for each field inside categories
116
+ record = record_text_for(@yaml['name'])
117
+ # text field for autocomplete
118
+ @html << '<span class="dc-text-autocomplete">' << @parent.text_field(record, _name, @yaml['html']) << '<span></span></span>'
119
+ # direct link for adding new documents to collection
120
+ if @yaml['with_new'] and !@readonly
121
+ @html << ' ' +
122
+ @parent.fa_icon('plus-square lg', class: 'in-edit-add', title: t('drgcms.new'),
123
+ style: "vertical-align: top;", 'data-table' => @yaml['with_new'] )
124
+ end
125
+ # div to list active selections
126
+ @html << "<div id =\"#{record}#{@yaml['name']}\">"
127
+ # find value for each field inside categories
120
128
  unless @record[@yaml['name']].nil?
121
129
  @record[@yaml['name']].each do |element|
122
130
  # this is quick and dirty trick. We have model dc_big_table which can be used for retrive
@@ -131,6 +139,7 @@ def render
131
139
  @html << if rec
132
140
  link = @parent.link_to(@parent.fa_icon('remove lg', class: 'dc-animate dc-red'), '#',
133
141
  onclick: "$('##{rec.id}').hide(); var v = $('##{record}_#{@yaml['name']}_#{rec.id}'); v.val(\"-\" + v.val());return false;")
142
+ link = @parent.fa_icon('check lg', class: 'dc-green') if @readonly
134
143
  field = @parent.hidden_field(record, "#{@yaml['name']}_#{rec.id}", value: element)
135
144
  "<div id=\"#{rec.id}\" style=\"padding:4px;\">#{link} #{rec.send(field_name)}<br>#{field}</div>"
136
145
  else
@@ -52,22 +52,31 @@ class NumberField < DrgcmsField
52
52
  # Render text_field field html code
53
53
  ###########################################################################
54
54
  def render
55
- return ro_standard if @readonly
55
+ #return ro_standard if @readonly
56
56
  set_initial_value
57
57
  #
58
58
  record = record_text_for(@yaml['name'])
59
59
  @yaml['html'] ||= {}
60
60
  @yaml['html']['class'] = 'dc-number'
61
+ if @yaml['format'].class == String
62
+ format = @yaml['format']
63
+ @yaml['format'] = {}
64
+ @yaml['format']['decimal'] = format[1].blank? ? 2 : format[1].to_i
65
+ @yaml['format']['separator'] = format[2].blank? ? I18n.t('number.currency.format.separator') : format[2]
66
+ @yaml['format']['delimiter'] = format[3].blank? ? I18n.t('number.currency.format.delimiter') : format[3]
67
+ end
61
68
  @yaml['html']['data-decimal'] = @yaml.dig('format','decimal') || 2
62
69
  @yaml['html']['data-delimiter'] = @yaml.dig('format','delimiter') || I18n.t('number.currency.format.delimiter')
63
70
  @yaml['html']['data-separator'] = @yaml.dig('format','separator') || I18n.t('number.currency.format.separator')
64
71
  # @yaml['html']['data-currency'] = @yaml.dig('format','currency') == 'yes' ? I18n.t('number.currency.format.currency') : @yaml.dig('format','currency')
65
72
  value = @record[@yaml['name']] || 0
66
-
67
- @html << @parent.hidden_field( record, @yaml['name'], value: value )
68
-
69
73
  @yaml['html']['value'] = @parent.dc_format_number(value, @yaml['html']['data-decimal'], @yaml['html']['data-separator'], @yaml['html']['data-delimiter'] )
70
- @html << @parent.text_field( nil,"record_#{@yaml['name']}1", @yaml['html'])
74
+
75
+ return ro_standard(@yaml['html']['value']) if @readonly
76
+
77
+ @yaml['html']['autocomplete'] ||= 'off'
78
+ @html << @parent.hidden_field( record, @yaml['name'], value: value )
79
+ @html << @parent.text_field( nil,"record_#{@yaml['name']}_", @yaml['html'])
71
80
  self
72
81
  end
73
82
 
@@ -76,7 +85,7 @@ end
76
85
  ###########################################################################
77
86
  def self.get_data(params, name)
78
87
  return 0 if params['record'][name].blank?
79
- params['record'][name].match('.') ? BigDecimal.new(params['record'][name]) : Integer.new(params['record'][name])
88
+ params['record'][name].match('.') ? params['record'][name].to_f : Integer.new(params['record'][name])
80
89
  end
81
90
 
82
91
  end
@@ -0,0 +1,96 @@
1
+ #--
2
+ # Copyright (c) 2012+ Damjan Rems
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ #++
23
+ module DrgcmsFormFields
24
+
25
+ ###########################################################################
26
+ # Implementation of radio DRG CMS form field. Field provides radio button input
27
+ # structure.
28
+ # Form options are mostly same as in select field.
29
+ #
30
+ # ===Form options:
31
+ # * +name:+ field name (required)
32
+ # * +type:+ radio (required)
33
+ # * +choices:+ Values for choices separated by comma. Values can also be specified like description:value.
34
+ # In this case description will be shown to user, but value will be saved to document.
35
+ # choices: 'OK:0,Ready:1,Error:2'
36
+ # choices: Ruby,Pyton,PHP
37
+ # * +eval:+ Choices will be provided by evaluating expression
38
+ # eval: dc_choices4('model_name','description_field_name','_id'); dc_choices4 helper will provide data for select field.
39
+ # eval: ModelName.choices4_field; ModelName class will define method choices4_field which
40
+ # will provide data for select field. Since expression is evaluated in the context of Form Field object
41
+ # Even session session variables can be accessed.
42
+ # eval: 'MyClass.method(@parent.session[:user_id])'
43
+ # When searching is more complex custom search method may be defined in CollectionName
44
+ # model which will provide result set for search.
45
+ # eval: collection_name.search_field_name.method_name;
46
+ # If choices or eval is not defined choices will be provided from translation helpers. For example:
47
+ # Collection has field status. Choices for field will be provided by en.helpers.model_name.choices4_status
48
+ # entry of english translation. English is of course default translation. If you provide translations in
49
+ # your local language then select choices will be localized.
50
+ # en.helpers.model_name.choices4_status: 'OK:0,Ready:1,Error:2'
51
+ # sl.helpers.model_name.choices4_status: 'V redu:0,Pripravljen:1,Napaka:2'
52
+ # * +inline:+ Radio buttons will be presented inline instead of stacked on each other.
53
+ #
54
+ # Form example:
55
+ # 10:
56
+ # name: hifi
57
+ # type: radio
58
+ # choices: 'Marantz:1,Sony:2,Bose:3,Pioneer:4'
59
+ # inline: true
60
+ ###########################################################################
61
+ class Radio < Select
62
+
63
+ ###########################################################################
64
+ # Render radio DRG Form field
65
+ ###########################################################################
66
+ def render
67
+ set_initial_value('html','value')
68
+
69
+ record = record_text_for(@yaml['name'])
70
+ @yaml['html'].symbolize_keys!
71
+ clas = 'dc-radio' + ( @yaml['inline'] ? ' dc-inline' : '')
72
+ @html << "<div class=\"#{clas}\">"
73
+ choices = get_choices
74
+ # When error and boolean field
75
+ #if choices.size == 1 and (@record[@yaml['name']].class == TrueClass or @record[@yaml['name']].class == FalseClass)
76
+ # choices = [[I18n.t('drgcms.true'), true], [I18n.t('drgcms.false'), false]]
77
+ #end
78
+ # Should select first button if no value provided
79
+ value = if @record.nil? || @record[@yaml['name']].blank?
80
+ choices.first.class == String ? choices.first : choices.first.last
81
+ else
82
+ @record[@yaml['name']]
83
+ end
84
+ choices.each do |choice|
85
+ choice = [choice, choice] if choice.class == String
86
+ @html << "<div>"
87
+ @html << @parent.radio_button_tag("#{record}[#{@yaml['name']}]",choice.last, choice.last.to_s == value.to_s)
88
+ @html << choice.first
89
+ @html << "</div>"
90
+ end
91
+ @html << "</div>\n"
92
+ self
93
+ end
94
+
95
+ end
96
+ end
@@ -66,10 +66,10 @@ def render
66
66
 
67
67
  # @parent.dc_name4_id(a[1], a[2], @record[ @yaml['name'] ])
68
68
  else
69
- eval( "#{@yaml['eval']} '#{@record[ @yaml['name'] ]}'")
69
+ eval( "#{@yaml['eval']} '#{@record.send(@yaml['name'])}'")
70
70
  end
71
71
  else
72
- @parent.dc_format_value(@record[@yaml['name']],@yaml['format'])
72
+ @parent.dc_format_value(@record.send(@yaml['name']),@yaml['format'])
73
73
  end
74
74
  @html << '</div>'
75
75
  self
@@ -44,6 +44,8 @@ module DrgcmsFormFields
44
44
  # your local language then select choices will be localized.
45
45
  # en.helpers.model_name.choices4_status: 'OK:0,Ready:1,Error:2'
46
46
  # sl.helpers.model_name.choices4_status: 'V redu:0,Pripravljen:1,Napaka:2'
47
+ # * +depend:+ Select options may depend on a value in some other field. If depend option is specified
48
+ # then chices must be provided by class method and defined in eval option.
47
49
  # * +html:+ html options which apply to select field (optional)
48
50
  #
49
51
  # Form example:
@@ -56,53 +58,107 @@ module DrgcmsFormFields
56
58
  # eval: DcCategory.values_for_parent
57
59
  # html:
58
60
  # include_blank: true
61
+ # 50:
62
+ # name: company
63
+ # type: select
64
+ # choices: Audi,BMW,Mercedes
65
+ # or
66
+ # choices: helpers.label.model.choices4_field
67
+ # 60:
68
+ # name: type
69
+ # type: select
70
+ # eval: Cars.choices4_type
71
+ # depend: company
59
72
  ###########################################################################
60
73
  class Select < DrgcmsField
61
74
 
62
75
  ###########################################################################
63
- # Return values, when choices options will be returned by evaluating expression
76
+ # Choices are defined in helper as:
77
+ # helper.label.table_name.choices_for_fieldname or
78
+ # choices4_tablename_fieldname
79
+ ###########################################################################
80
+ def choices_in_helper(helper = nil)
81
+ helper ||= "helpers.label.#{@form['table']}.choices4_#{@yaml['name']}"
82
+ c = t(helper)
83
+ if c.match( 'translation missing' )
84
+ helper = "choices_for_#{@form['table']}_#{@yaml['name']}"
85
+ return "Error. #{helper} not defined" if c.match( 'translation missing' )
86
+ end
87
+ c
88
+ end
89
+
64
90
  ###########################################################################
65
- def do_eval(e)
91
+ # Choices are defined by evaluating an expression. This is most common class
92
+ # method defined in a class. eg. SomeClass.get_choices4
93
+ ###########################################################################
94
+ def choices_in_eval(e)
66
95
  e.strip!
67
- method = e.split(/\ |\(/).first
68
- return eval(e) if respond_to?(method) # id method defined here
69
- return eval('@parent.'+e) if @parent.respond_to?(method) # is method defined in helpers
70
- # eval whatever it is
71
- eval e
96
+ if @yaml['depend'].nil?
97
+ method = e.split(/\ |\(/).first
98
+ return eval(e) if respond_to?(method) # id method defined here
99
+ return eval('@parent.'+e) if @parent.respond_to?(method) # is method defined in helpers
100
+ # eval whatever it is there
101
+ eval e
102
+ else
103
+ # add event listener to depend field
104
+ @js << %(
105
+ $(document).ready(function() {
106
+ $('#record_#{@yaml['depend']}').change( function(e) { update_select_depend('record_#{@yaml['name']}', 'record_#{@yaml['depend']}','#{e}');});
107
+ $('#_record_#{@yaml['depend']}').change( function(e) { update_select_depend('record_#{@yaml['name']}', '_record_#{@yaml['depend']}','#{e}');});
108
+ });
109
+ )
110
+ # depend field might be virtual field. It's value should be set in params
111
+ depend_value = @yaml['depend'][0] == '_' ? @parent.params["p_#{@yaml['depend']}"] : @record[@yaml['depend']]
112
+
113
+ e << " '#{depend_value}'"
114
+ eval e
115
+ end
72
116
  end
73
-
117
+
74
118
  ###########################################################################
75
119
  # Create choices array for select field.
76
120
  ###########################################################################
77
121
  def get_choices
78
122
  begin
79
123
  choices = case
80
- when @yaml['choices'] then
81
- (@yaml['choices'].match('helpers.') ) ? t(@yaml['choices']) : @yaml['choices']
82
- when @yaml['eval'] then
83
- do_eval(@yaml['eval'])
84
- else
85
- c = t('helpers.label.' + @form['table'] + '.choices4_' + @yaml['name'] )
86
- c = 'Error' if c.match( 'translation missing' )
87
- c
88
- end
124
+ when @yaml['choices'] then
125
+ @yaml['choices'].match('helpers.') ? choices_in_helper(@yaml['choices']) : @yaml['choices']
126
+ when @yaml['eval'] then
127
+ choices_in_eval(@yaml['eval'])
128
+ else
129
+ choices_in_helper()
130
+ end
89
131
  # Convert string to Array
90
132
  choices.class == String ?
91
- choices.chomp.split(',').inject([]) {|r,v| r << (v.match(':') ? v.split(':') : v )} :
133
+ choices.chomp.split(',').inject([]) { |r,v| r << (v.match(':') ? v.split(':') : v ) } :
92
134
  choices
93
135
  rescue Exception => e
94
- Rails.logger.debug "Error in select eval. #{e.message}\n"
136
+ Rails.logger.debug "\nError in select eval. #{e.message}\n"
137
+ Rails.logger.debug(e.backtrace.join($/)) if Rails.env.development?
95
138
  ['error'] # return empty array when error occures
96
139
  end
97
140
  end
98
141
 
142
+ ###########################################################################
143
+ # Will add code to view more data about selected option in a window
144
+ ###########################################################################
145
+ def add_view_code
146
+ return '' if (data = @record.send(@yaml['name'])).blank?
147
+
148
+ ar = @yaml['view'].split(/\ |\,/).delete_if {|e| e.blank?}
149
+ table, form_name = *ar
150
+ url = @parent.url_for(controller: :cmsedit, id: data, action: :edit, table: table, form_name: form_name, readonly: true, window_close: 1 )
151
+ icon = @parent.fa_icon('eye')
152
+ %(<span class="dc-window-open" data-url="#{url}">#{icon}</span>)
153
+ end
154
+
99
155
  ###########################################################################
100
156
  # Return value when readonly is required
101
157
  ###########################################################################
102
158
  def ro_standard
103
- value = @record.respond_to?(@yaml['name']) ? @record[@yaml['name']] : nil
104
- return self if value.nil?
105
- #
159
+ value = @record.respond_to?(@yaml['name']) ? @record.send(@yaml['name']) : nil
160
+ return self if value.blank?
161
+
106
162
  choices = get_choices()
107
163
  if value.class == Array # multiple choices
108
164
  html = ''
@@ -132,15 +188,22 @@ end
132
188
  ###########################################################################
133
189
  def render
134
190
  return ro_standard if @readonly
191
+
135
192
  set_initial_value('html','selected')
136
- #
193
+ # separate options and html part
194
+ html_part = {}
137
195
  @yaml['html'].symbolize_keys!
196
+ %i(class id style required).each { |sym| html_part[sym] = @yaml['html'].delete(sym) if html_part[sym]}
197
+ html_part[:multiple] = true if @yaml['multiple']
198
+
138
199
  record = record_text_for(@yaml['name'])
139
- if @yaml['multiple']
140
- @html << @parent.select(record, @yaml['name'], get_choices, @yaml['html'], {multiple: true})
200
+ if html_part[:multiple]
201
+ @html << @parent.select(record, @yaml['name'], get_choices, @yaml['html'], html_part)
141
202
  @js << "$('##{record}_#{@yaml['name']}').selectMultiple();"
142
203
  else
143
- @html << @parent.select(record, @yaml['name'], get_choices, @yaml['html'])
204
+ @html << @parent.select(record, @yaml['name'], get_choices, @yaml['html'], html_part)
205
+ # add code for view more data
206
+ @html << add_view_code() if @yaml['view']
144
207
  end
145
208
  self
146
209
  end
@@ -151,11 +214,11 @@ end
151
214
  def self.get_data(params, name)
152
215
  if params['record'][name].class == Array
153
216
  params['record'][name].delete_if {|e| e.blank? }
154
- return nil if params['record'][name].size == 0
155
- # convert to BSON objects
217
+ return if params['record'][name].size == 0
218
+
219
+ # convert to BSON objects
156
220
  is_id = BSON::ObjectId.legal?(params['record'][name].first)
157
221
  return params['record'][name].map{ |e| BSON::ObjectId.from_string(e) } if is_id
158
- return params['record'][name]
159
222
  end
160
223
  params['record'][name]
161
224
  end