Module: CmsCommonHelper
- Included in:
- DcBigMenuRenderer, DcGalleryRenderer, DcMenuRenderer, DcPageRenderer, DcPartRenderer, DcPieceRenderer, DcPollRenderer, DcSimpleMenuRenderer
- Defined in:
- app/helpers/cms_common_helper.rb
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
-
.dc_choices_for_field(model, field) ⇒ Object
Return choices for field in model if choices are defined in localization text.
-
.dc_format_date_time(value, format = nil) ⇒ Object
Returns html code for displaying date/time formatted by strftime.
-
.dc_format_number(value = 0, decimals = nil, separator = nil, delimiter = nil, currency = nil) ⇒ Object
Returns html code for displaying formatted number.
-
.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.
-
.t(key, default = nil) ⇒ Object
Wrapper for i18 t method, with some spice added.
Instance Method Summary collapse
- #dc_choices4_field(model, field) ⇒ Object
- #dc_choices_for_field(model, field) ⇒ Object
- #dc_date_time(value, format) ⇒ Object
-
#dc_format_date_time(value, format = nil) ⇒ Object
Returns html code for displaying date/time formatted by strftime.
-
#dc_format_number(value = 0, decimals = nil, separator = nil, delimiter = nil, currency = nil) ⇒ Object
Returns html code for displaying formatted number.
-
#dc_help_body ⇒ Object
Will return text from help files.
-
#dc_help_button(result_set) ⇒ Object
Will return code for help button if there is any help text available for the form.
-
#dc_help_fields ⇒ Object
Will scoop fields and help text associated with them to create basic help text.
-
#dc_help_for_tab(tab) ⇒ Object
Create help text for fields on single tab.
- #dc_icon4_boolean(document = false, field_name = false) ⇒ Object
-
#dc_icon_for_boolean(document = false, field_name = nil) ⇒ Object
Return html code for icon presenting boolean value.
- #dc_name4_id(model, field, field_name, id = nil) ⇒ Object
- #dc_name4_value(model, field, value) ⇒ Object
-
#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.
- #dc_name_for_value(model, field, value) ⇒ Object
-
#dc_steps_menu_get(parent) ⇒ Object
Will return html code for steps menu when form with steps is processed.
-
#t(key, default = nil) ⇒ Object
:nodoc.
-
#t_label_for_column(options) ⇒ Object
Returns label for field translated to current locale for usage browser header.
-
#t_label_for_field(field_name, default = '') ⇒ Object
Returns label for field translated to current locale for usage on data entry form.
-
#t_tablename(tablename, default = nil) ⇒ Object
Returns table (collection) name translation for usage in dialog title.
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
162 163 164 165 166 |
# File 'app/helpers/cms_common_helper.rb', line 162 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.
265 266 267 268 269 270 271 272 273 |
# File 'app/helpers/cms_common_helper.rb', line 265 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.
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 |
# File 'app/helpers/cms_common_helper.rb', line 304 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.
128 129 130 131 132 133 134 |
# File 'app/helpers/cms_common_helper.rb', line 128 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
176 177 178 179 |
# File 'app/helpers/cms_common_helper.rb', line 176 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
169 170 171 |
# File 'app/helpers/cms_common_helper.rb', line 169 def dc_choices_for_field(model, field) CmsCommonHelper.dc_choices_for_field(model, field) end |
#dc_date_time(value, format) ⇒ Object
289 290 291 292 |
# File 'app/helpers/cms_common_helper.rb', line 289 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.
282 283 284 |
# File 'app/helpers/cms_common_helper.rb', line 282 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.
341 342 343 |
# File 'app/helpers/cms_common_helper.rb', line 341 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_body ⇒ Object
Will return text from help files
387 388 389 |
# File 'app/helpers/cms_common_helper.rb', line 387 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.
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 |
# File 'app/helpers/cms_common_helper.rb', line 394 def (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_fields ⇒ Object
Will scoop fields and help text associated with them to create basic help text.
372 373 374 375 376 377 378 379 380 381 382 |
# File 'app/helpers/cms_common_helper.rb', line 372 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
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 |
# File 'app/helpers/cms_common_helper.rb', line 348 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
253 254 255 256 |
# File 'app/helpers/cms_common_helper.rb', line 253 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
245 246 247 248 |
# File 'app/helpers/cms_common_helper.rb', line 245 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
223 224 225 226 |
# File 'app/helpers/cms_common_helper.rb', line 223 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
144 145 146 147 |
# File 'app/helpers/cms_common_helper.rb', line 144 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.
209 210 211 212 213 214 215 216 217 218 |
# File 'app/helpers/cms_common_helper.rb', line 209 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
137 138 139 |
# File 'app/helpers/cms_common_helper.rb', line 137 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.
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 |
# File 'app/helpers/cms_common_helper.rb', line 414 def (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 browser header. Translation is provided by lang.helpers.label.table_name.field_name locale. If not found method will look in standard drgcms translations.
96 97 98 99 100 101 102 |
# File 'app/helpers/cms_common_helper.rb', line 96 def t_label_for_column() label = ['caption'] || ['label'] label = (['name'] ? "helpers.label.#{@form['table']}.#{['name']}" : '') if label.nil? label = t(label) if label.match(/\./) label = t("drgcms.#{['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 |
# File 'app/helpers/cms_common_helper.rb', line 84 def t_label_for_field(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 |