Module: CmsCommonHelper

Overview

Common methods which may also come handy in controllers or models or any other module of program.

Usage: include CmsCommonHelper

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.dc_choices_for_field(model, field) ⇒ Object

Return choices for field in model if choices are defined in localization text.

Parameters:

model

String. Table (collection) model name (lowercase).

field

String. Field name used.

Example:

dc_choices4_field('dc_user', 'state' )

Returns: Array. Choices for select input field



173
174
175
176
177
178
# File 'app/helpers/cms_common_helper.rb', line 173

def self.dc_choices_for_field(model, field)
  choices = CmsCommonHelper.t("helpers.label.#{model}.choices4_#{field}" )
  return ['error'] if choices.match( /translation missing/i )

  choices.chomp.split(',').map{ _1.split(':') }
end

.dc_format_date_time(value, format = nil) ⇒ Object

Returns html code for displaying date/time formatted by strftime. Will return ” if value is nil.

Parameters:

value

Date/DateTime/Time.

format

String. strftime format mask. Defaults to locale’s default format.



277
278
279
280
281
282
283
284
285
# File 'app/helpers/cms_common_helper.rb', line 277

def self.dc_format_date_time(value, format=nil)
  return '' if value.blank?

  format ||= value.class == Date ? t('date.formats.default') : t('time.formats.default')
  if format.size == 1
    format = format.match(/d/i) ? t('date.formats.default') : t('time.formats.default')
  end
  value.strftime(format)
end

.dc_format_number(value = 0, decimals = nil, separator = nil, delimiter = nil, currency = nil) ⇒ Object

Returns html code for displaying formatted number.

Parameters:

value

Numeric number.

decimals

Integer. Number of decimals

separator

String. Decimals separator

delimiter

String. Thousands delimiter.

currency

String. Currency symbol if applied to result string.



316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'app/helpers/cms_common_helper.rb', line 316

def self.dc_format_number(value=0, decimals=nil, separator=nil, delimiter=nil, currency=nil)
  decimals  ||=  I18n.t('number.currency.format.precision')
  separator ||= I18n.t('number.currency.format.separator')
  separator   = '' if decimals == 0
  delimiter ||= I18n.t('number.currency.format.delimiter')
  whole, dec = value.to_s.split('.')
  whole = '0' if whole.blank?
# remove and remember sign  
  sign = ''
  if whole[0] == '-'
    whole.delete_prefix!('-')
    sign  << '-'
  end
# format decimals
  dec ||= '0'
  dec = dec[0,decimals]
  while dec.size < decimals do dec += '0' end
# slice whole on chunks of 3
  ar = []
  while whole.size > 0 do 
    n = whole.size >=3 ? 3 : whole.size 
    ar << whole.slice!(n*-1,n)
  end
# put it all back and format
  "#{sign}#{ar.reverse.join(delimiter)}#{separator}#{dec}" 
end

.dc_name_for_value(model, field, value) ⇒ Object

When select field is used on form options for select can be provided by helpers.label.table_name.choices4_name locale. This is how select field options are translated. Method returns selected choice translated to current locale.

Parameters:

model

String. Table (collection) model name (lowercase).

field

String. Field name used.

value

String. Value of field which translation will be returned.

Example:

# usage in program. Choice values for state are 'Deactivated:0,Active:1,Waiting:2'
dc_name4_value('dc_user', 'state', @record.active )

# usage in form
columns:
  2: 
    name: state
    eval: dc_name4_value dc_user, state

Returns: String. Descriptive text (translated) for selected choice value.



138
139
140
141
142
143
144
145
# File 'app/helpers/cms_common_helper.rb', line 138

def self.dc_name_for_value(model, field, value)
  return '' if value.nil?

  choices = t("helpers.label.#{model}.choices4_#{field}")
  values  = choices.chomp.split(',').map{ _1.split(':') }
  values.each{ |e| return e.first if e.last.to_s == value.to_s }
  '???'
end

.t(key, default = nil) ⇒ Object

Wrapper for i18 t method, with some spice added. If translation is not found English translation value will be returned. And if still not found default value will be returned if passed.

Parameters:

key

String. String to be translated into locale.

default

String. Value returned if translation is not found.

Example:

t('translate.this','Enter text for ....')

Returns: String. Translated text.



47
48
49
50
51
52
53
54
55
56
57
# File 'app/helpers/cms_common_helper.rb', line 47

def self.t(key, default = nil)
  c = I18n.t(key)
  if c.class == Hash || c.match( /translation missing/i )
    c = I18n.t(key, locale: 'en') 
    # Still not found, return default
    if c.class == Hash || c.match( /translation missing/i )
      c = default || key
    end
  end
  c
end

Instance Method Details

#dc_choices4_field(model, field) ⇒ Object



188
189
190
191
# File 'app/helpers/cms_common_helper.rb', line 188

def dc_choices4_field(model, field) #nodoc
  #dc_deprecate('dc_choices4_field will be deprecated. Use dc_choices_for_field instead.')
  CmsCommonHelper.dc_choices_for_field(model, field)
end

#dc_choices_for_field(model, field) ⇒ Object



181
182
183
# File 'app/helpers/cms_common_helper.rb', line 181

def dc_choices_for_field(model, field)
  CmsCommonHelper.dc_choices_for_field(model, field)
end

#dc_date_time(value, format) ⇒ Object



301
302
303
304
# File 'app/helpers/cms_common_helper.rb', line 301

def dc_date_time(value, format) #:nodoc:
  dc_deprecate 'dc_date_time will be deprecated! Use dc_format_date_time instead.'
  dc_format_date_time(value, format)
end

#dc_format_date_time(value, format = nil) ⇒ Object

Returns html code for displaying date/time formatted by strftime. Will return ” if value is nil.

Parameters:

value

Date/DateTime/Time.

format

String. strftime format mask. Defaults to locale’s default format.



294
295
296
# File 'app/helpers/cms_common_helper.rb', line 294

def dc_format_date_time(value, format=nil) #:nodoc:
  CmsCommonHelper.dc_format_date_time(value, format)
end

#dc_format_number(value = 0, decimals = nil, separator = nil, delimiter = nil, currency = nil) ⇒ Object

Returns html code for displaying formatted number.

Parameters:

value

Numeric number.

decimals

Integer. Number of decimals

separator

String. Decimals separator

delimiter

String. Thousands delimiter.

currency

String. Currency symbol if applied to result string.



353
354
355
# File 'app/helpers/cms_common_helper.rb', line 353

def dc_format_number(value=0, decimals=nil, separator=nil, delimiter=nil, currency=nil) #:nodoc:
  CmsCommonHelper.dc_format_number(value, decimals, separator, delimiter, currency)
end

#dc_help_bodyObject

Will return text from help files



399
400
401
# File 'app/helpers/cms_common_helper.rb', line 399

def dc_help_body
  (params[:type] == 'index' ? @help['index'] : @help['form']).html_safe
end

#dc_help_button(result_set) ⇒ Object

Will return code for help button if there is any help text available for the form.



406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
# File 'app/helpers/cms_common_helper.rb', line 406

def dc_help_button(result_set)
  type = result_set.nil? ? 'form' : 'index'
  form_name = CmsHelper.form_param(params) || CmsHelper.table_param(params)
  url = url_for(controller: :dc_common, action: :help, type: type, f: form_name)
  html = %(<div class="dc-help-icon dc-link-ajax" data-url=#{url}>#{fa_icon('question-circle')}</div>)
  return html if type == 'form'

  # check if index has any help available
  help_file_name = @form['help'] || @form['extend'] || form_name
  help_file_name = DcApplicationController.find_help_file(help_file_name)
  if help_file_name
    help = YAML.load_file(help_file_name)
    return html if help['index']
  end
  ''
end

#dc_help_fieldsObject

Will scoop fields and help text associated with them to create basic help text.



384
385
386
387
388
389
390
391
392
393
394
# File 'app/helpers/cms_common_helper.rb', line 384

def dc_help_fields
  return '' if @form['form'].nil?

  html = '<a id="fields"></a>'
  if @form['form']['tabs']
    @form['form']['tabs'].each { |tab| html << dc_help_for_tab(tab) }
  else
    html << dc_help_for_tab(@form['form']['fields'])
  end
  html.html_safe
end

#dc_help_for_tab(tab) ⇒ Object

Create help text for fields on single tab



360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# File 'app/helpers/cms_common_helper.rb', line 360

def dc_help_for_tab(tab)
  return '' if tab.nil?

  html = ''
  if tab.class == Array
    tab_name = tab.last['caption'] || tab.first
    tab_label, tab_help = dc_tab_label_help(tab_name)
    html << %(<div class="help-tab">#{tab_label}</div><div class="help-tab-help">#{tab_help}</div>)

    tab = tab.last
  end

  tab.each do |field|
    label, help = dc_label_help(field.last)
    next if help.blank?

    html << %(<div class="help-field"><div class="help-label">#{label}</div><div class="help-text">#{help.gsub("\n",'<br>')}</div></div>)
  end
  html
end

#dc_icon4_boolean(document = false, field_name = nil) ⇒ Object



265
266
267
268
# File 'app/helpers/cms_common_helper.rb', line 265

def dc_icon4_boolean(document = false, field_name = nil) #nodoc
  #dc_deprecate('dc_icon4_boolean will be deprecated. Use dc_icon_for_boolean instead.')
  dc_icon_for_boolean(document, field_name)
end

#dc_icon_for_boolean(document = false, field_name = nil) ⇒ Object

Return html code for icon presenting boolean value. Icon is a picture of checked or unchecked box. If second parameter (fiel_name) is ommited value is supplied as first parameter.

Parameters:

value

Boolean.

Example:

# usage from program
dc_icon4_boolean(document, field_name)

# usage from form description
columns:
  10: 
    name: active
    eval: dc_icon4_boolean


257
258
259
260
# File 'app/helpers/cms_common_helper.rb', line 257

def dc_icon_for_boolean(document = false, field_name = nil)
  value = field_name.nil? ? document : document[field_name]
  dc_dont?(value, true) ? fa_icon('check_box_outline_blank md-18') : fa_icon('check_box-o md-18')
end

#dc_name4_id(model, field, field_name, id = nil) ⇒ Object



235
236
237
238
# File 'app/helpers/cms_common_helper.rb', line 235

def dc_name4_id(model, field, field_name, id = nil) #nodoc
  #dc_deprecate('dc_name4_id will be deprecated. Use dc_name_for_id instead.')
  dc_name_for_id(model, field, field_name, id) 
end

#dc_name4_value(model, field, value) ⇒ Object



155
156
157
158
# File 'app/helpers/cms_common_helper.rb', line 155

def dc_name4_value(model, field, value) #nodoc
  #dc_deprecate('dc_name4_value will be deprecated. Use dc_name_for_value instead.')
  CmsCommonHelper.dc_name_for_value(model, field, value)
end

#dc_name_for_id(model, field, field_name, id = nil) ⇒ Object

Will return descriptive text for id key when field in one table (collection) has belongs_to relation to other table.

Parameters:

model

String. Table (collection) model name (lowercase).

field

String. Field name holding the value of descriptive text.

field_name

String. ID field name. This is by default id, but can be any other

(preferred unique) field.

value

Value of id_field. Usually a BSON Key but can be any other data type.

Example:

# usage in program.
dc_name4_id('dc_user', 'name', nil, dc_page.created_by)

# usage in form
columns:
  2: 
    name: site_id
    eval: dc_name4_id,site,name
# username is saved to document instead of user.id field
  5: 
    name: user
    eval: dc_name4_id,dc_user,name,username

Returns: String. Name (descriptive value) for specified key in table.



221
222
223
224
225
226
227
228
229
230
# File 'app/helpers/cms_common_helper.rb', line 221

def dc_name_for_id(model, field, field_name, id = nil)
  return '' if id.nil?

  field_name = (field_name || 'id').strip.to_sym
  field = field.strip.to_sym
  model = model.strip.classify.constantize if model.class == String
  doc = Mongo::QueryCache.cache { model.find_by(field_name => id) }

  doc.nil? ? '' : (doc.send(field) rescue 'not defined')
end

#dc_name_for_value(model, field, value) ⇒ Object



148
149
150
# File 'app/helpers/cms_common_helper.rb', line 148

def dc_name_for_value(model, field, value)
  CmsCommonHelper.dc_name_for_value(model, field, value)
end

#dc_steps_menu_get(parent) ⇒ Object

Will return html code for steps menu when form with steps is processed.



426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
# File 'app/helpers/cms_common_helper.rb', line 426

def dc_steps_menu_get(parent)
  yaml = @form['form']['steps']
  return '' unless yaml

  html = %(<ul id="dc-steps-menu"><h2>#{t('drgcms.steps')}</h2>)
  control = @form['control'] ? @form['control'] : @form['table']
  parms = { controller: 'cmsedit', action: 'run', control: "#{control}.steps",
            table: CmsHelper.table_param(params),
            form_name: CmsHelper.form_param(params),
            id: @record.id }

  yaml.sort.each_with_index do |data, i|
    n = i + 1
    step = data.last # it's an array
    url = case params[:step].to_i
          when n + 1 then url_for(parms.merge({ step: n + 1, next_step: n}))
          when n then url_for(parms.merge({ step: n, next_step: n}))
          when n - 1 then url_for(parms.merge({ step: n - 1, next_step: n}))
          else
            ''
          end
    _class = url.present? ? 'dc-link-ajax' : ''
    _class << (params[:step].to_i == n ? ' active' : '')
    html << %(<li class="#{_class}" data-url="#{url}">#{step['title']}</li>)
  end
  html << '</ul>'
end

#t(key, default = nil) ⇒ Object

:nodoc



60
61
62
# File 'app/helpers/cms_common_helper.rb', line 60

def t(key, default = nil) #:nodoc
  CmsCommonHelper.t(key, default)
end

#t_label_for_column(options) ⇒ Object

Returns label for field translated to current locale for usage in browser header. Translation is provided by lang.helpers.label.table_name.field_name locale. If not found method will look in standard drgcms translations.



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'app/helpers/cms_common_helper.rb', line 98

def t_label_for_column(options)
  label = options['caption'] || options['label']
  return ' ' if label == false

  if label.blank?
    label = if options['name']
              prefix = @form['i18n_prefix'] || "helpers.label.#{@form['table']}"
              "#{prefix}.#{options['name']}"
            end
    label = label.to_s
  end
  label = t(label) if label.match(/\./)
  label = t("drgcms.#{options['name']}") if label.match('helpers.') # standard field names like created_by, updated_at
  label
end

#t_label_for_field(field_name, default = '') ⇒ Object

Returns label for field translated to current locale for usage on data entry form. Translation is provided by lang.helpers.label.table_name.field_name locale. If translation is not found method will capitalize field_name and replace ‘_’ with ‘ ’.



84
85
86
87
88
89
90
91
# File 'app/helpers/cms_common_helper.rb', line 84

def t_label_for_field(field_name, default = '')
  c = (@form['i18n_prefix'] || "helpers.label.#{@form['table']}") + ".#{field_name}"
  c = field_name if field_name.match(/helpers\./)

  label = t(c, default)
  label = field_name.capitalize.gsub('_', ' ') if c.match( /translation missing/i )
  label
end

#t_tablename(tablename, default = nil) ⇒ Object

Returns table (collection) name translation for usage in dialog title. Tablename title is provided by helpers.label.table_name.tabletitle locale.

Parameters:

tablename

String. Table (collection) name to be translated.

default

String. Value returned if translation is not found.

Returns: String. Translated text.



75
76
77
# File 'app/helpers/cms_common_helper.rb', line 75

def t_tablename(tablename, default = nil)
  t('helpers.label.' + tablename + '.tabletitle', default || tablename)
end