drg_cms 0.5.52.12 → 0.5.52.16

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. checksums.yaml +5 -5
  2. data/app/assets/javascripts/drg_cms/drg_cms.js +17 -2
  3. data/app/assets/stylesheets/drg_cms/drg_cms.css +16 -3
  4. data/app/assets/stylesheets/drg_cms/select-multiple.css +1 -1
  5. data/app/controllers/cmsedit_controller.rb +56 -16
  6. data/app/controllers/dc_application_controller.rb +83 -1
  7. data/app/controllers/dc_common_controller.rb +2 -52
  8. data/app/forms/all_options.yml +27 -4
  9. data/app/forms/cms_menu.yml +5 -0
  10. data/app/forms/dc_gallery.yml +53 -0
  11. data/app/forms/dc_link.yml +16 -10
  12. data/app/forms/dc_menu_item.yml +5 -0
  13. data/app/forms/dc_page.yml +1 -2
  14. data/app/forms/dc_removed_url.yml +42 -0
  15. data/app/helpers/cmsedit_helper.rb +63 -22
  16. data/app/helpers/dc_application_helper.rb +35 -11
  17. data/app/helpers/dc_gallery_renderer.rb +94 -0
  18. data/app/helpers/dc_page_renderer.rb +20 -3
  19. data/app/helpers/dc_poll_renderer.rb +6 -7
  20. data/app/models/concerns/dc_page_concern.rb +1 -1
  21. data/app/models/dc_filter.rb +15 -7
  22. data/app/models/dc_gallery.rb +64 -0
  23. data/app/models/dc_link.rb +1 -0
  24. data/app/models/dc_memory.rb +19 -4
  25. data/app/models/dc_page.rb +1 -1
  26. data/app/models/dc_removed_url.rb +54 -0
  27. data/app/models/drgcms_form_fields.rb +5 -1649
  28. data/app/models/drgcms_form_fields/check_box.rb +69 -0
  29. data/app/models/drgcms_form_fields/comment.rb +49 -0
  30. data/app/models/drgcms_form_fields/date_picker.rb +102 -0
  31. data/app/models/drgcms_form_fields/date_select.rb +68 -0
  32. data/app/models/drgcms_form_fields/date_time_picker.rb +87 -0
  33. data/app/models/drgcms_form_fields/datetime_select.rb +73 -0
  34. data/app/models/drgcms_form_fields/drgcms_field.rb +241 -0
  35. data/app/models/drgcms_form_fields/drgcms_form_fields.rb +25 -0
  36. data/app/models/drgcms_form_fields/embedded.rb +84 -0
  37. data/app/models/drgcms_form_fields/file_select.rb +70 -0
  38. data/app/models/drgcms_form_fields/hidden_field.rb +52 -0
  39. data/app/models/drgcms_form_fields/html_field.rb +70 -0
  40. data/app/models/drgcms_form_fields/journal_diff.rb +60 -0
  41. data/app/models/drgcms_form_fields/link_to.rb +69 -0
  42. data/app/models/drgcms_form_fields/multitext_autocomplete.rb +195 -0
  43. data/app/models/drgcms_form_fields/number_field.rb +83 -0
  44. data/app/models/drgcms_form_fields/password_field.rb +62 -0
  45. data/app/models/drgcms_form_fields/readonly.rb +79 -0
  46. data/app/models/drgcms_form_fields/select.rb +164 -0
  47. data/app/models/drgcms_form_fields/submit_tag.rb +58 -0
  48. data/app/models/drgcms_form_fields/text_area.rb +68 -0
  49. data/app/models/drgcms_form_fields/text_autocomplete.rb +143 -0
  50. data/app/models/drgcms_form_fields/text_field.rb +56 -0
  51. data/app/models/drgcms_form_fields/text_with_select.rb +92 -0
  52. data/app/models/drgcms_form_fields/tree_select.rb +150 -0
  53. data/config/locales/drgcms_en.yml +1 -0
  54. data/config/locales/drgcms_sl.yml +2 -1
  55. data/config/locales/models_en.yml +42 -6
  56. data/config/locales/models_sl.yml +38 -3
  57. data/lib/drg_cms.rb +1 -1
  58. data/lib/drg_cms/version.rb +1 -1
  59. data/lib/tasks/dc_cleanup.rake +1 -1
  60. metadata +33 -4
@@ -0,0 +1,70 @@
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 html_field DRG CMS form field.
27
+ #
28
+ # HtmlField class only implements code for calling actual html edit field code.
29
+ # This is by default drg_default_html_editor gem which uses CK editor javascript plugin
30
+ # or any other plugin. Which plugin will be used as html editor is defined by
31
+ # dc_site.settings html_editor setting.
32
+ #
33
+ # Example of dc_site.setting used for drg_default_html_editor gem.
34
+ # html_editor: ckeditor
35
+ # ck_editor:
36
+ # config_file: /files/ck_config.js
37
+ # css_file: /files/ck_css.css
38
+ # file_select: elfinder
39
+ #
40
+ # Form example:
41
+ # 10:
42
+ # name: body
43
+ # type: html_field
44
+ # options: "height: 500, width: 550, toolbar: 'basic'"
45
+ ###########################################################################
46
+ class HtmlField < DrgcmsField
47
+
48
+ ###########################################################################
49
+ # Render html_field field html code
50
+ ###########################################################################
51
+ def render
52
+ return ro_standard if @readonly
53
+ # retrieve html editor from page settings
54
+ editor_string = @parent.dc_get_site.params['html_editor'] if @parent.dc_get_site
55
+ editor_string ||= 'ckeditor'
56
+ #
57
+ klas_string = editor_string.camelize
58
+ if DrgcmsFormFields.const_defined?(klas_string)
59
+ klas = DrgcmsFormFields::const_get(klas_string)
60
+ o = klas.new(@parent, @record, @yaml).render
61
+ @js << o.js
62
+ @html << o.html
63
+ else
64
+ @html << "HTML editor not defined. Check site.settings or include drgcms_default_html_editor gem."
65
+ end
66
+ self
67
+ end
68
+
69
+ end
70
+ end
@@ -0,0 +1,60 @@
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 journal_diff DRG CMS form field. journal_diff field is used to
27
+ # show differences between two fields in DcJournal collection.
28
+ #
29
+ # ===Form options:
30
+ # * +name:+ field name (required)
31
+ # * +type:+ journal_diff (required)
32
+ #
33
+ # Form example:
34
+ # 10:
35
+ # name: diff
36
+ # type: journal_diff
37
+ # html:
38
+ # size: 100x25
39
+ ###########################################################################
40
+ class JournalDiff < DrgcmsField
41
+
42
+ ###########################################################################
43
+ # Render journal_diff field html code
44
+ ###########################################################################
45
+ def render
46
+ @yaml['name'] = 'old' if @record[@yaml['name']].nil?
47
+ @html << '<div class="dc-journal">'
48
+ JSON.parse(@record[@yaml['name']]).each do |k,v|
49
+ old_value = v.class == Array ? v[0] : v
50
+ new_value = v.class == Array ? v[1] : v
51
+ @html << "<div style='background-color: #eee;'>#{@parent.check_box('select', k)} #{k}</div>
52
+ <div style='background-color: #fcc;'>-<br>#{old_value}</div>
53
+ <div style='background-color: #cfc;'>+<br>#{new_value}</div><br>"
54
+ end
55
+ @html << '</div>'
56
+ self
57
+ end
58
+ end
59
+
60
+ end
@@ -0,0 +1,69 @@
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 link_to DRG CMS form field. link_to form field is mostly used by polls but can
27
+ # be also incorporated anywhere on the form.
28
+ #
29
+ # ===Form options:
30
+ # * +type:+ link_to (required)
31
+ # * +caption:+ Link caption
32
+ # * +icon:+ Link icon
33
+ # * +url:+ direct url link
34
+ # * +controller:+ controller name
35
+ # * +action:+ action name
36
+ # * +html:+ html options which apply to link_to (optional)
37
+ #
38
+ # Form example:
39
+ # 30:
40
+ # type: link_to
41
+ # caption: Some action
42
+ # icon: cogs
43
+ # controller: my_controller
44
+ # action: my_action
45
+ # id: id # will be replaced by record._id
46
+ ###########################################################################
47
+ class LinkTo < DrgcmsField
48
+
49
+ ###########################################################################
50
+ # Render link_to field html code
51
+ ###########################################################################
52
+ def render
53
+ @yaml['html'] ||= {}
54
+ @yaml['html']['class'] ||= 'dc-link dc-animate'
55
+ @yaml['html'].symbolize_keys!
56
+ #
57
+ @yaml[:id] = record._id if @yaml[:id] == 'id'
58
+ url = @yaml['url'] || "#{@yaml[:controller]}/#{@yaml[:action]}/#{@yaml[:id]}"
59
+ url.gsub!('//','/') # no action and id
60
+ url = '/' + @yaml['url'] unless url[0,1] == '/' # no leading /
61
+ url.chop if url[0,-1] == '/' # remove trailing /
62
+ #
63
+ caption = @yaml['caption'] || @yaml['text']
64
+ @html << @parent.dc_link_to(caption, @yaml['icon'], url, @yaml['html'])
65
+ self
66
+ end
67
+ end
68
+
69
+ end
@@ -0,0 +1,195 @@
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 multitext_autocomplete DRG CMS form field.
27
+ #
28
+ # multitext_autocomplete field is complex data entry field which uses autocomplete
29
+ # function when selecting multiple values for MongoDB Array field. Array typically holds
30
+ # id's of selected documents and control typically displays value of the field name
31
+ # defined by search options.
32
+ #
33
+ # ===Form options:
34
+ # * +name:+ field name (required)
35
+ # * +type:+ multitext_autocomplete (required)
36
+ # * +table+ Collection (table) name. When defined search must contain field name
37
+ # * +search:+ Search may consist of three parameters from which are separated either by dot (.) or comma(,)
38
+ # * search_field_name; when table option is defined search must define field name which will be used for search query
39
+ # * collection_name.search_field_name; Same as above except that table options must be ommited.
40
+ # * collection_name.search_field_name.method_name; When searching is more complex custom search
41
+ # method may be defined in CollectionName model which will provide result set for search.
42
+ #
43
+ # Form example:
44
+ # 90:
45
+ # name: kats
46
+ # type: multitext_autocomplete
47
+ # search: dc_category.name
48
+ # html:
49
+ # size: 30
50
+ ###########################################################################
51
+ class MultitextAutocomplete < DrgcmsField
52
+
53
+ ###########################################################################
54
+ # Returns value for readonly field
55
+ ###########################################################################
56
+ def ro_standard(table, search)
57
+ result = ''
58
+ table = table.classify.constantize
59
+ return self if @record[@yaml['name']].nil?
60
+ # when field name and method are defined together
61
+ search = search.split('.').first if search.match('.')
62
+ @record[@yaml['name']].each do |element|
63
+ result << table.find(element)[search] + '<br>'
64
+ end
65
+ super(result)
66
+ end
67
+
68
+ ###########################################################################
69
+ # Render multitext_autocomplete field html code
70
+ ###########################################################################
71
+ def render
72
+ # search field name
73
+ if @yaml['search'].class == Hash
74
+ table = @yaml['search']['table']
75
+ field_name = @yaml['search']['field']
76
+ method = @yaml['search']['method']
77
+ search = method.nil? ? field_name : "#{field_name}.#{method}"
78
+ elsif @yaml['search'].to_s.match(/\./)
79
+ table, field_name, method = @yaml['search'].split(/\.|\,/)
80
+ search = method.nil? ? field_name : "#{field_name}.#{method}"
81
+ else # search and table name are separated
82
+ search = field_name = @yaml['search']
83
+ end
84
+ # determine table name
85
+ if @yaml['table']
86
+ table = if @yaml['table'].class == String
87
+ @yaml['table']
88
+ # eval(how_to_get_my_table_name)
89
+ elsif @yaml['table']['eval']
90
+ eval @yaml['table']['eval']
91
+ else
92
+ p "Field #{@yaml['name']}: Invalid table parameter!"
93
+ nil
94
+ end
95
+ end
96
+ unless (table and search)
97
+ @html << 'Table or search field not defined!'
98
+ return self
99
+ end
100
+ #
101
+ return ro_standard(table, search) if @readonly
102
+ # TODO check if table exists
103
+ collection = table.classify.constantize
104
+ unless @record.respond_to?(@yaml['name'])
105
+ @html << "Invalid field name: #{@yaml['name']}"
106
+ return self
107
+ end
108
+ # put field to enter search data on form
109
+ @yaml['html'] ||= {}
110
+ @yaml['html']['value'] = '' # must be. Otherwise it will look into record and return error
111
+ @yaml['html']['placeholder'] = t('drgcms.search_placeholder')
112
+ _name = '_' + @yaml['name']
113
+ @html << '<div class="ui-autocomplete-border">'
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
+
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
120
+ unless @record[@yaml['name']].nil?
121
+ @record[@yaml['name']].each do |element|
122
+ # this is quick and dirty trick. We have model dc_big_table which can be used for retrive
123
+ # more complicated options
124
+ # TODO retrieve choices from big_table
125
+ rec = if table == 'dc_big_table'
126
+ collection.find(@yaml['name'], @parent.session)
127
+ else
128
+ collection.find(element)
129
+ end
130
+ # Related data is missing. It happends.
131
+ @html << if rec
132
+ link = @parent.link_to(@parent.fa_icon('remove lg', class: 'dc-animate dc-red'), '#',
133
+ onclick: "$('##{rec.id}').hide(); var v = $('##{record}_#{@yaml['name']}_#{rec.id}'); v.val(\"-\" + v.val());return false;")
134
+ field = @parent.hidden_field(record, "#{@yaml['name']}_#{rec.id}", value: element)
135
+ "<div id=\"#{rec.id}\" style=\"padding:4px;\">#{link} #{rec.send(field_name)}<br>#{field}</div>"
136
+ else
137
+ '** error **'
138
+ end
139
+ end
140
+ end
141
+ @html << "</div></div>"
142
+ # Create text for div to be added when new category is selected
143
+ link = @parent.link_to(@parent.fa_icon('remove lg', class: 'dc-animate dc-red'), '#',
144
+ onclick: "$('#rec_id').hide(); var v = $('##{record}_#{@yaml['name']}_rec_id'); v.val(\"-\" + v.val());return false;")
145
+ field = @parent.hidden_field(record, "#{@yaml['name']}_rec_id", value: 'rec_id')
146
+ one_div = "<div id=\"rec_id\" style=\"padding:4px;\">#{link} rec_search<br>#{field}</div>"
147
+
148
+ # JS stuff
149
+ @js << <<EOJS
150
+ $(document).ready(function() {
151
+ $("##{record}_#{_name}").autocomplete( {
152
+ source: function(request, response) {
153
+ $.ajax({
154
+ url: "#{ @parent.url_for( controller: 'dc_common', action: 'autocomplete' )}",
155
+ type: "POST",
156
+ dataType: "json",
157
+ data: { input: request.term, table: "#{table}", search: "#{search}" #{(',id: "'+@yaml['id'] + '"') if @yaml['id']} },
158
+ success: function(data) {
159
+ response( $.map( data, function(key) {
160
+ return key;
161
+ }));
162
+ }
163
+ });
164
+ },
165
+ change: function (event, ui) {
166
+ var div = '#{one_div}';
167
+ div = div.replace(/rec_id/g, ui.item.id)
168
+ div = div.replace('rec_search', ui.item.value)
169
+ $("##{record}#{@yaml['name']}").append(div);
170
+ $("##{record}_#{_name}").val('');
171
+ $("##{record}_#{_name}").focus();
172
+ },
173
+ minLength: 2
174
+ });
175
+ });
176
+ EOJS
177
+
178
+ self
179
+ end
180
+
181
+ ###########################################################################
182
+ # Class method for retrieving data from multitext_autocomplete form field.
183
+ ###########################################################################
184
+ def self.get_data(params, name)
185
+ r = []
186
+ params['record'].each do |k,v|
187
+ # if it starts with - then it was removed
188
+ r << BSON::ObjectId.from_string(v) if k.match("#{name}_") and v[0,1] != '-'
189
+ end
190
+ r.uniq!
191
+ r
192
+ end
193
+
194
+ end
195
+ end
@@ -0,0 +1,83 @@
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 number_field DRG CMS form field. Number fields can be
27
+ # formated for display with thousands delimiters and decimal separators and
28
+ # can have currency symbol.
29
+ #
30
+ # ===Form options:
31
+ # * +type:+ number_field (required)
32
+ # * +name:+ Field name (required)
33
+ # * +format:+ Format options
34
+ # * +decimals:+ No of decimal places
35
+ # * +separator:+ decimal separator (yes no , .) Default yes if decimals > 0
36
+ # * +delimiter:+ Thousands delimiter (yes no , .) Default defind by locals
37
+ # * +currency:+ Currency sign (yes no sign) Default no. If yes defined by locals
38
+ # * +html:+ html options which apply to text_field field (optional)
39
+ #
40
+ # Form example:
41
+ # 10:
42
+ # name: title
43
+ # type: number_field
44
+ # size: 10
45
+ # format:
46
+ # decimals: 2
47
+ # delimiter: false
48
+ ###########################################################################
49
+ class NumberField < DrgcmsField
50
+
51
+ ###########################################################################
52
+ # Render text_field field html code
53
+ ###########################################################################
54
+ def render
55
+ return ro_standard if @readonly
56
+ set_initial_value
57
+ #
58
+ record = record_text_for(@yaml['name'])
59
+ @yaml['html'] ||= {}
60
+ @yaml['html']['class'] = 'dc-number'
61
+ @yaml['html']['data-decimal'] = @yaml.dig('format','decimal') || 2
62
+ @yaml['html']['data-delimiter'] = @yaml.dig('format','delimiter') || I18n.t('number.currency.format.delimiter')
63
+ @yaml['html']['data-separator'] = @yaml.dig('format','separator') || I18n.t('number.currency.format.separator')
64
+ # @yaml['html']['data-currency'] = @yaml.dig('format','currency') == 'yes' ? I18n.t('number.currency.format.currency') : @yaml.dig('format','currency')
65
+ value = @record[@yaml['name']] || 0
66
+
67
+ @html << @parent.hidden_field( record, @yaml['name'], value: value )
68
+
69
+ @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'])
71
+ self
72
+ end
73
+
74
+ ###########################################################################
75
+ # Return value. Return nil if input field is empty
76
+ ###########################################################################
77
+ def self.get_data(params, name)
78
+ return 0 if params['record'][name].blank?
79
+ params['record'][name].match('.') ? BigDecimal.new(params['record'][name]) : Integer.new(params['record'][name])
80
+ end
81
+
82
+ end
83
+ end