Module: CmsHelper
- Defined in:
- app/helpers/cms_helper.rb
Overview
CmseditHelper module defines common methods used for DRGCMS forms.
Instance Attribute Summary collapse
-
#js ⇒ Object
readonly
javascript part created by form helpers.
Class Method Summary collapse
-
.form_file_find(form_file) ⇒ String
Searches forms path for file_name and returns full file name or nil if not found.
-
.form_param(params) ⇒ Object
Will return form_name from parameter regardless if set as form_name or just f.
-
.forms_merge(hash1, hash2) ⇒ Object
Merges two forms when current form extends other form.
-
.table_param(params) ⇒ Object
Will return table name from parameter regardless if set as table or just t.
Instance Method Summary collapse
-
#dc_field_action(yaml) ⇒ Object
Creates code for including data entry field in index actions.
-
#dc_field_label_help(options) ⇒ Object
Return field code, label and help text for a field defined on a DRG Form.
-
#dc_form_class(additional_class = nil) ⇒ Object
Will return class for form.
-
#dc_form_id ⇒ Object
Will return form id.
-
#dc_get_caption(yaml) ⇒ Object
There are several options for defining caption (caption,label, text).
-
#dc_get_field_form_definition(name) ⇒ Object
Will return field form definition if field is defined on form.
-
#dc_html_data(yaml) ⇒ Object
Create ex.
-
#dc_label_help(options) ⇒ Object
Return label and help text for a field defined on Form.
-
#dc_link_ajax_window_submit_action(yaml, record = nil, action_active = true) ⇒ Object
String : HTML code for action.
-
#dc_log_exception(exception, where = '') ⇒ Object
Log exception to rails log.
-
#dc_script_action(yaml) ⇒ Object
Creates code for script action type.
-
#dc_tab_label_help(tab_name) ⇒ Object
Return label and help for tab on Form.
-
#dc_yaml_add_option(source, options) ⇒ Object
Add new option to yaml.
Instance Attribute Details
#js ⇒ Object (readonly)
javascript part created by form helpers
31 32 33 |
# File 'app/helpers/cms_helper.rb', line 31 def js @js end |
Class Method Details
.form_file_find(form_file) ⇒ String
Searches forms path for file_name and returns full file name or nil if not found.
be useful when you are extending form but want to retain same name as original form For example. You are extending dc_user form from drg_cms gem and want to retain same dc_user name. This can be done by setting drg_cms.dc_user as extend option.
350 351 352 353 354 355 356 357 358 359 |
# File 'app/helpers/cms_helper.rb', line 350 def self.form_file_find(form_file) form_path = nil form_path, form_file = form_file.split(/\.|\//) if form_file.match(/\.|\//) DrgCms.paths(:forms).reverse.each do |path| f = "#{path}/#{form_file}.yml" return f if File.exist?(f) && (form_path.nil? || path.to_s.match(/\/#{form_path}(-|\/)/i)) end raise "Exception: Form file '#{form_file}' not found!" end |
.form_param(params) ⇒ Object
Will return form_name from parameter regardless if set as form_name or just f.
329 330 331 |
# File 'app/helpers/cms_helper.rb', line 329 def self.form_param(params) params[:form_name] || params[:f] end |
.forms_merge(hash1, hash2) ⇒ Object
Merges two forms when current form extends other form. Subroutine of dc_form_read. With a little help of www.ruby-forum.com/topic/142809
365 366 367 368 369 370 371 372 373 374 375 376 |
# File 'app/helpers/cms_helper.rb', line 365 def self.forms_merge(hash1, hash2) target = hash1.dup hash2.keys.each do |key| if hash2[key].is_a?(Hash) && hash1[key].is_a?(Hash) target[key] = CmsHelper.forms_merge(hash1[key], hash2[key]) next end target[key] = hash2[key] == '/' ? nil : hash2[key] end # delete keys with nil value target.delete_if { |k, v| v.nil? } end |
.table_param(params) ⇒ Object
Will return table name from parameter regardless if set as table or just t.
336 337 338 |
# File 'app/helpers/cms_helper.rb', line 336 def self.table_param(params) params[:table] || params[:t] end |
Instance Method Details
#dc_field_action(yaml) ⇒ Object
Creates code for including data entry field in index actions.
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'app/helpers/cms_helper.rb', line 163 def dc_field_action(yaml) # assign value if value found in parameters if params['record'] value = params['record'][yaml['name']] params["p_#{yaml['name']}"] = value end # find field definition on form if ( field_definition = dc_get_field_form_definition(yaml['name']) ) # some options may be redefined field_definition['size'] = yaml['size'] if yaml['size'] field, label, help = dc_field_label_help(field_definition) else yaml['type'] = yaml['field_type'] field, label, help = dc_field_label_help(yaml) end # input field will have label as placeholder field = field.sub('input', "input placeholder=\"#{label}\"") %(<li class="no-background">#{field}</li>) end |
#dc_field_label_help(options) ⇒ Object
Return field code, label and help text for a field defined on a DRG Form.
Parameters: options : Hash : Field definition
Returns: Array
field_html : String : HTML code for field definition
label : String : Label text
help : String : Help text
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'app/helpers/cms_helper.rb', line 81 def dc_field_label_help() label, help = dc_label_help() # create field object from type and call its render method if ['type'].present? klass_string = ['type'].camelize field_html = if DrgcmsFormFields.const_defined?(klass_string) # when field type defined klass = DrgcmsFormFields.const_get(klass_string) field = klass.new(self, @record, ).render @js << field.js @css << field.css field.html else "Error: Field type #{['type']} not defined!" end else "Error: Field type missing!" end [field_html, label, help] end |
#dc_form_class(additional_class = nil) ⇒ Object
Will return class for form. Class can be used for different styling of forms.
322 323 324 |
# File 'app/helpers/cms_helper.rb', line 322 def dc_form_class(additional_class = nil) %(class="#{additional_class} #{@form['class']}" ).html_safe end |
#dc_form_id ⇒ Object
Will return form id. id can be used for css selecting of fields on form. Form id is by default form_name || table parameter.
315 316 317 |
# File 'app/helpers/cms_helper.rb', line 315 def dc_form_id %(id="#{CmsHelper.form_param(params) || CmsHelper.table_param(params)}" ).html_safe end |
#dc_get_caption(yaml) ⇒ Object
There are several options for defining caption (caption,label, text). This method will ensure that caption is returned anyhow provided.
196 197 198 |
# File 'app/helpers/cms_helper.rb', line 196 def (yaml) yaml['caption'] || yaml['text'] || yaml['label'] end |
#dc_get_field_form_definition(name) ⇒ Object
Will return field form definition if field is defined on form. Field definition will be used for input field on the form.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'app/helpers/cms_helper.rb', line 54 def dc_get_field_form_definition(name) #:nodoc: return if @form['form'].nil? @form['form']['tabs'].each do |tab| # Array with 2 elements. First is tab name, second is data my_fields = tab.last my_fields.each { |k, v| return v if (k.class == Integer && v['name'] == name) } end if @form['form']['tabs'] # I know. But nice. @form['form']['fields'].each do |field| next unless field.first.class == Integer # options return field.last if field.last['name'] == name end if @form['form']['fields'] nil end |
#dc_html_data(yaml) ⇒ Object
Create ex. class=“my-class” html code from html options for action
186 187 188 189 190 |
# File 'app/helpers/cms_helper.rb', line 186 def dc_html_data(yaml) return '' if yaml.blank? yaml.inject(' ') { |result, e| result << (e.last ? %(#{e.first}="#{e.last}" ) : '') } end |
#dc_label_help(options) ⇒ Object
Return label and help text for a field defined on Form.
Parameters:
@options : Hash : Field definition
Returns:
@label : String : Label text
@help : String : Help text
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'app/helpers/cms_helper.rb', line 111 def dc_label_help() # no label or help in comments return [nil, nil] if %w[comment action].include?(['type']) label = ['caption'] || ['text'] || ['label'] label = '' if ['type'] == 'check_box' if ['name'] label = if label.blank? t_label_for_field(['name'], ['name'].capitalize.gsub('_',' ') ) elsif ['name'] t(label, label) end end # help text can be defined in form or in translations starting with helpers. or as helpers.help.collection.field help = ['help'] if help.blank? help = if ['name'] # if defined as i18n_prefix replace "label" with "help" prefix = @form['i18n_prefix'] ? @form['i18n_prefix'].sub('label', 'help') : "helpers.help.#{@form['table']}" "#{prefix}.#{['name']}" end help = help.to_s end help = t(help, ' ') if help.to_s.match(/help\./) [label, help] end |
#dc_link_ajax_window_submit_action(yaml, record = nil, action_active = true) ⇒ Object
String : HTML code for action
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
# File 'app/helpers/cms_helper.rb', line 210 def dc_link_ajax_window_submit_action(yaml, record = nil, action_active = true) parms = {} = (yaml) = ? t("#{.downcase}", ) : nil icon = yaml['icon'] ? "#{fa_icon(yaml['icon'])}" : '' # action is not active unless dc_is_action_active?(yaml) return %(<li><div class="dc-link-no">#{icon} #{}</div></li>) end # set data-confirm when confirm shortcut present yaml['html'] ||= {} text = yaml['html']['data-confirm'] || yaml['confirm'] yaml['html']['data-confirm'] = t(text) if text.present? text = yaml['html']['title'] || yaml['title'] yaml['html']['title'] = t(text) if text.present? yaml['html']['target'] ||= yaml['target'] # direct url if yaml['url'] parms['url'] = yaml['url'] parms['idr'] = dc_document_path(record) if record # make url from action controller else parms['controller'] = yaml['controller'] || 'cmsedit' parms['action'] = yaml['action'] parms['table'] = yaml['table'] || @form['table'] parms['form_name'] = yaml['form_name'] parms['control'] = yaml['control'] if yaml['control'] parms['id'] = record.id if record end # add current id to parameters parms['id'] = dc_document_path(record) if record # overwrite with or add additional parameters from environment or record yaml['params'].each { |k, v| parms[k] = dc_value_for_parameter(v, record) } if yaml['params'] parms['table'] = parms['table'].underscore if parms['table'] # might be CamelCase # error if controller parameter is missing return "<li>#{'Controller not defined'}</li>" if parms['controller'].nil? && parms['url'].nil? html_data = dc_html_data(yaml['html']) url = url_for(parms) rescue 'URL error' url = nil if parms['url'] == '#' request = yaml['request'] || yaml['method'] || 'get' code = case yaml['type'] when 'ajax' # ajax button clas = 'dc-link-ajax' %(<div class="#{clas}" data-url="#{action_active ? url : ''}" #{html_data} data-request="#{request}" title="#{yaml['title']}">#{icon}#{}</div>) when 'submit' # submit button # It's dirty hack, but will prevent not authorized message and render index action correctly parms[:filter] = 'on' url = url_for(parms) rescue 'URL error' clas = 'dc-action-submit' %(<div class="#{clas}" data-url="#{action_active ? url : ''}" #{html_data} data-request="#{request}" title="#{yaml['title']}">#{icon}#{}</div>) when 'link' # link button yaml['html'] = dc_yaml_add_option(yaml['html'], class: 'dc-link') link = dc_link_to(, yaml['icon'], parms, yaml['html'] ) %(#{action_active ? link : }) when 'window' # open window clas = 'dc-link dc-window-open' %(<div class="#{clas}" data-url="#{action_active ? url : ''}" #{html_data}>#{icon}#{}</div>) when 'popup' # popup dialog clas = 'dc-link dc-popup-open' %(<div class="#{clas}" data-url="#{action_active ? url : ''}" #{html_data}>#{icon}#{}</div>) else 'Type error!' end "<li>#{code}</li>" end |
#dc_log_exception(exception, where = '') ⇒ Object
Log exception to rails log. Usefull for debugging eval errors.
304 305 306 307 308 309 |
# File 'app/helpers/cms_helper.rb', line 304 def dc_log_exception(exception, where = '') log = exception ? "\n*** Error:#{where + ':'} #{exception.}\n#{exception.backtrace.first.inspect}\n" : '' log << "DRG Form: #{CmsHelper.form_param(params)}, line: #{session[:form_processing]}\n" logger.error log end |
#dc_script_action(yaml) ⇒ Object
Creates code for script action type.
36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'app/helpers/cms_helper.rb', line 36 def dc_script_action(yaml) icon = dc_icon_for_link yaml['icon'] yaml['html'] ||= {} yaml['html']['data-url'] = 'script' yaml['html']['data-request'] = 'script' yaml['html']['data-script'] = "#{yaml['js'] || yaml['script']}" yaml['html']['class'] ||= 'dc-link-ajax' attributes = yaml['html'].inject('') { |r, e| r << "#{e.first}=\"#{e.last}\"" } # data = %(data-request="script" data-script="#{yaml['js'] || yaml['script']}" data-url="script") #%(<li><div class="dc-link-ajax" #{data}>#{icon} #{ t(yaml['caption'], yaml['caption']) }</div></li>) %(<li><div #{attributes}>#{icon} #{ t(yaml['caption'], yaml['caption']) }</div></li>) end |
#dc_tab_label_help(tab_name) ⇒ Object
Return label and help for tab on Form.
Parameters: options : String : Tab name on form
Returns:
label : String : Label text
help : String : Help text
149 150 151 152 153 154 155 156 157 158 |
# File 'app/helpers/cms_helper.rb', line 149 def dc_tab_label_help(tab_name) label = @form.dig('form', 'tabs', tab_name, 'caption') || tab_name label = t(label, t_label_for_field(label, label)) help = @form.dig('form', 'tabs', tab_name, 'help') || "helpers.help.#{@form['table']}.#{tab_name}" help = t(help, t_label_for_field(help, help)) help = nil if help.match('helpers.') # help not found in translation [label, help] end |
#dc_yaml_add_option(source, options) ⇒ Object
Add new option to yaml. Subroutine of dc_link_ajax_window_submit_action.
291 292 293 294 295 296 297 298 299 |
# File 'app/helpers/cms_helper.rb', line 291 def dc_yaml_add_option(source, ) #nodoc .each do |k, v| key = k.to_s source[key] ||= '' # only if not already present source[key] << " #{v}" unless source[key].match(v.to_s) end source end |