Module: AgileCommonHelper

Included in:
ArGalleryRenderer, ArMenuRenderer, ArPageRenderer, ArPartRenderer, ArPieceRenderer, ArPollRenderer
Defined in:
app/helpers/agile_common_helper.rb

Overview

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

Usage: include AgileCommonHelper

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.agile_choices_for_field(model, field) ⇒ Object

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

Parameters:

model

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

field

String. Field name used.

Example:

agile_choices_for_field('ar_user', 'state' )

Returns: Array. Choices for select input field



107
108
109
110
111
112
# File 'app/helpers/agile_common_helper.rb', line 107

def self.agile_choices_for_field(model, field)
  c = AgileCommonHelper.t("helpers.label.#{model}.choices_for_#{field}")
  return ['error'] if c.match(/translation missing/i)

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

Instance Method Details

#agile_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.



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

def agile_format_date_time(value, format=nil) #:nodoc:
  AgileHelper.format_date_time(value, format)
end

#agile_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.



195
196
197
# File 'app/helpers/agile_common_helper.rb', line 195

def agile_format_number(value=0, decimals=nil, separator=nil, delimiter=nil, currency=nil) #:nodoc:
  AgileHelper.format_number(value, decimals, separator, delimiter, currency)
end

#agile_help_bodyObject

Will return text from help files



241
242
243
# File 'app/helpers/agile_common_helper.rb', line 241

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

#agile_help_button(data_set) ⇒ Object

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



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'app/helpers/agile_common_helper.rb', line 248

def agile_help_button(data_set)
  type = data_set.nil? ? 'form' : 'index'
  form_name = AgileHelper.form_param(params) || AgileHelper.table_param(params)
  url = url_for(controller: :agile_common, action: :help, type: type, f: form_name)
  html = %(<div class="ar-help-icon ar-link-ajax" data-url=#{url}>#{mi_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 = AgileApplicationController.find_help_file(help_file_name)
  if help_file_name
    help = YAML.load_file(help_file_name)
    return html if help['index']
  end
  ''
end

#agile_help_fieldsObject

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



226
227
228
229
230
231
232
233
234
235
236
# File 'app/helpers/agile_common_helper.rb', line 226

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

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

#agile_help_for_tab(tab) ⇒ Object

Create help text for fields on single tab



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'app/helpers/agile_common_helper.rb', line 202

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

  html = ''
  if tab.class == Array
    tab_name = tab.last['caption'] || tab.first
    tab_label, tab_help = agile_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 = agile_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

#agile_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
agile_icon_for_boolean(document, field_name)

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


169
170
171
172
# File 'app/helpers/agile_common_helper.rb', line 169

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

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

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

Parameters:

model

String. Table (table) 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 an id key but can be any other data type.

Example:

# usage in program.
agile_name_for_id('ar_user', 'name', nil, ar_page.created_by)

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

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



142
143
144
145
146
147
148
149
150
# File 'app/helpers/agile_common_helper.rb', line 142

def agile_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
  record = model.find_by(field_name => id)
  record.nil? ? '' : (record.send(field) rescue '?? not defined')
end

#agile_steps_menu_get(parent) ⇒ Object

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



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'app/helpers/agile_common_helper.rb', line 268

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

  html = %(<ul id="ar-steps-menu"><h2>#{t('agile.steps')}</h2>)
  control = @form['control'] ? @form['control'] : @form['table']
  parms = { controller: :agile, action: 'run', control: "#{control}.steps",
            table: AgileHelper.table_param(params),
            form_name: AgileHelper.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? ? 'ar-link-ajax' : ''
    _class += (params[:step].to_i == n ? ' active' : '')
    html += %(<li class="#{_class}" data-url="#{url}">#{step['title']}</li>)
  end
  html += '</ul>'
end

#agile_text_for_value(model, field, value) ⇒ Object



90
91
92
# File 'app/helpers/agile_common_helper.rb', line 90

def agile_text_for_value(model, field, value)
  AgileHelper.name_for_value(model, field, value)
end

#t(key, default = nil) ⇒ Object

:nodoc



34
35
36
# File 'app/helpers/agile_common_helper.rb', line 34

def t(key, default = nil) #:nodoc
  AgileHelper.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 agile translations.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/helpers/agile_common_helper.rb', line 72

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("agile.#{options['name']}") if label.match('helpers.') # standard field names like created_by, updated_at
  label = options['name'].capitalize if label.match('agile.')  # still no translation. Just capitalize
  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 ‘ ’.



58
59
60
61
62
63
64
65
# File 'app/helpers/agile_common_helper.rb', line 58

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_table_name(table_name, default = nil) ⇒ Object

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

Parameters:

tablename

String. Table (table) name to be translated.

default

String. Value returned if translation is not found.

Returns: String. Translated text.



49
50
51
# File 'app/helpers/agile_common_helper.rb', line 49

def t_table_name(table_name, default = nil)
  t('helpers.label.' + table_name + '.table_title', default || table_name)
end

#url_forward_params(parms) ⇒ Object

Adds additional parameters to url parameters hash. If url is nil then additional parameters are the return value of method.

Parameters:

  • url (Array)

    : URL parameters as array. Values will be added to array.



302
303
304
305
306
307
# File 'app/helpers/agile_common_helper.rb', line 302

def url_forward_params(parms)
  if params[:belongs_to]
    parms[:belongs_to]    = params[:belongs_to]
    parms[:belongs_to_id] = params[:belongs_to_id]
  end
end