Module: CmsHelper

Defined in:
app/helpers/cms_helper.rb

Overview

CmseditHelper module defines common methods used for DRGCMS forms.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#jsObject (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_param(params) ⇒ Object

Will return form_name from parameter regardless if set as form_name or just f.



293
294
295
# File 'app/helpers/cms_helper.rb', line 293

def self.form_param(params)
  params[:form_name] || params[:f]
end

.table_param(params) ⇒ Object

Will return table name from parameter regardless if set as table or just t.



300
301
302
# File 'app/helpers/cms_helper.rb', line 300

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.



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'app/helpers/cms_helper.rb', line 144

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


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'app/helpers/cms_helper.rb', line 73

def dc_field_label_help(options)
  label, help = dc_label_help(options)
  # create field object from type and call its render method
  if options['type'].present?
    klass_string = options['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, options).render
      @js  << field.js
      @css << field.css
      field.html
    else
      "Error: Field type #{options['type']} not defined!"
    end
  else
    "Error: Field type missing!"
  end
  [field_html, label, help]
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.



177
178
179
# File 'app/helpers/cms_helper.rb', line 177

def dc_get_caption(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.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/helpers/cms_helper.rb', line 46

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



167
168
169
170
171
# File 'app/helpers/cms_helper.rb', line 167

def dc_html_data(yaml)
  return '' if yaml.blank?

  yaml.inject(' ') { |result, e| result = e.last.nil? ? result : result << "#{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


103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'app/helpers/cms_helper.rb', line 103

def dc_label_help(options)
  # no label or help in comments
  return [nil, nil] if %w(comment action).include?(options['type'])

  label = options['caption'] || options['text'] || options['label']
  label = if label.blank?
            t_name(options['name'], options['name'].capitalize.gsub('_',' ') )
          elsif options['name']
            t(label, label)
          end
  # help text can be defined in form or in translations starting with helpers. or as helpers.help.collection.field
  help = options['help']
  help ||= "helpers.help.#{@form['table']}.#{options['name']}" if options['name']
  help = t(help, ' ') if help.to_s.match(/helpers\./)
  [label, help]
end

String : HTML code for action



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
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
# File 'app/helpers/cms_helper.rb', line 191

def dc_link_ajax_window_submit_action(yaml, record = nil, action_active = true)
  parms = {}
  caption = dc_get_caption(yaml)
  caption = caption ? t("#{caption.downcase}", caption) : 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} #{caption}</div></li>)
  end
  # set data-confirm when confirm 
  yaml['html'] ||= {}
  confirm = yaml['html']['data-confirm'] || yaml['confirm']
  yaml['html']['data-confirm'] = t(confirm) if confirm.present?
  yaml['html']['title'] ||= yaml['title']
  yaml['html']['title'] = t(yaml['title']) if yaml['title'].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}#{caption}</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}#{caption}</div>)

  when 'link'  # link button
    yaml['html'] = dc_yaml_add_option(yaml['html'], class: 'dc-link')
    link = dc_link_to(caption, yaml['icon'], parms, yaml['html'] )
    %(#{action_active ? link : caption})

  when 'window' # open window
    clas = 'dc-link dc-window-open'
    %(<div class="#{clas}" data-url="#{action_active ? url : ''}" #{html_data}>#{icon}#{caption}</div>)

  when 'popup' # popup dialog
    clas = 'dc-link dc-popup-open'
    %(<div class="#{clas}" data-url="#{action_active ? url : ''}" #{html_data}>#{icon}#{caption}</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.



283
284
285
286
287
288
# File 'app/helpers/cms_helper.rb', line 283

def dc_log_exception(exception, where = '')
  log = exception ? "\n*** Error:#{where + ':'} #{exception.message}\n#{exception.backtrace.first.inspect}\n" : ''
  log << "DRG Form processing 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
# File 'app/helpers/cms_helper.rb', line 36

def dc_script_action(yaml)
  icon = dc_icon_for_link yaml['icon']
  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>)
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


130
131
132
133
134
135
136
137
138
139
# File 'app/helpers/cms_helper.rb', line 130

def dc_tab_label_help(tab_name)
  label = @form['form']['tabs'][tab_name]['caption'] || tab_name
  label = t(label, t_name(label, label))

  help = @form['form']['tabs'][tab_name]['help'] || "helpers.help.#{@form['table']}.#{tab_name}"
  help = t(help, t_name(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.



270
271
272
273
274
275
276
277
278
# File 'app/helpers/cms_helper.rb', line 270

def dc_yaml_add_option(source, options) #nodoc
  options.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