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



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

def self.dc_choices_for_field(model, field)
  c = CmsCommonHelper.t('helpers.label.' + model + '.choices4_' + field )
  return ['error'] if c.match( 'translation missing' )
  c.chomp.split(',').inject([]) {|r,v| r << v.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.



251
252
253
254
255
256
257
258
259
# File 'app/helpers/cms_common_helper.rb', line 251

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.



290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'app/helpers/cms_common_helper.rb', line 290

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.



114
115
116
117
118
119
120
# File 'app/helpers/cms_common_helper.rb', line 114

def self.dc_name_for_value(model, field, value)
  return '' if value.nil?
  c = t('helpers.label.' + model + '.choices4_' + field )
  a = c.chomp.split(',').inject([]) {|r,v| r << v.split(':') }
  a.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 or c.match( 'translation missing' )
    c = I18n.t(key, locale: 'en') 
# Still not found. Return default if set
    if c.class == Hash or c.match( 'translation missing' )
      c = default.nil? ? key : default
    end
  end
  c
end

Instance Method Details

#dc_choices4_field(model, field) ⇒ Object



162
163
164
165
# File 'app/helpers/cms_common_helper.rb', line 162

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



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

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

#dc_date_time(value, format) ⇒ Object



275
276
277
278
# File 'app/helpers/cms_common_helper.rb', line 275

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.



268
269
270
# File 'app/helpers/cms_common_helper.rb', line 268

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.



327
328
329
# File 'app/helpers/cms_common_helper.rb', line 327

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



373
374
375
# File 'app/helpers/cms_common_helper.rb', line 373

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

#dc_help_fieldsObject

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



358
359
360
361
362
363
364
365
366
367
368
# File 'app/helpers/cms_common_helper.rb', line 358

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



334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# File 'app/helpers/cms_common_helper.rb', line 334

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 = false) ⇒ Object



239
240
241
242
# File 'app/helpers/cms_common_helper.rb', line 239

def dc_icon4_boolean(document = false, field_name = false) #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


231
232
233
234
# File 'app/helpers/cms_common_helper.rb', line 231

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



209
210
211
212
# File 'app/helpers/cms_common_helper.rb', line 209

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



130
131
132
133
# File 'app/helpers/cms_common_helper.rb', line 130

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.



195
196
197
198
199
200
201
202
203
204
# File 'app/helpers/cms_common_helper.rb', line 195

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 = Mongoid::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



123
124
125
# File 'app/helpers/cms_common_helper.rb', line 123

def dc_name_for_value(model, field, value)
  CmsCommonHelper.dc_name_for_value(model, field, value)
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_name(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
# File 'app/helpers/cms_common_helper.rb', line 84

def t_name(field_name, default='')
  c = t("helpers.label.#{@form['table']}.#{field_name}", default)
  c = field_name.capitalize.gsub('_',' ') if c.match( 'translation missing' )
  c
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