Module: AgileHelper

Defined in:
app/helpers/agile_helper.rb

Overview

AgileHelper module defines common methods used for AgileRails 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/agile_helper.rb', line 31

def js
  @js
end

Class Method Details

.dont?(what, default = false) ⇒ Boolean

Returns true if parameter has value of 0, false, no, none or -. Returns value of default if parameter has nil value.

Parameters:

what

String/boolean/Integer.

default

Default value when what has value of nil. False by default.

Example:

agile_dont?('none')    # => true
agile_dont?('-')       # => true
agile_dont?(1)         # => false
agile_dont?(nil, true) # => true

Returns:

  • (Boolean)


503
504
505
506
507
# File 'app/helpers/agile_helper.rb', line 503

def self.dont?(what, default = false)
  return default if what.nil?

  %w(0 n - no none false).include?(what.to_s.downcase.strip)
end

.form_file_find(form_file) ⇒ String

Searches forms path for file_name and returns full file name or nil if not found.

be useful when you are extending form but want to retain same name as original form For example. You are extending agile_user form from AgileRails gem and want to retain same agile_user form name, because it is used in AgileRails application menu. This can be done by setting agile_rails.agile_user as extend option.

Parameters:

  • Form (String)

    file name. File name can be passed as gem_name.filename. This can

Returns:

  • (String)

    Form file name including path or nil if not found.



346
347
348
349
350
351
352
353
354
355
# File 'app/helpers/agile_helper.rb', line 346

def self.form_file_find(form_file)
  form_path = nil
  form_path, form_file = form_file.split(/\.|\//) if form_file.match(/\.|\//)

 Agile.paths(:forms).reverse.each do |path|
    f = "#{path}/#{form_file}.yml"
    return f if File.exist?(f) && (form_path.nil? || path.to_s.match(/\/#{form_path}(-|\/)/i))
  end
  raise "Exception: Form file #{form_file}.yml not found!"
end

.form_param(params) ⇒ Object

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



324
325
326
# File 'app/helpers/agile_helper.rb', line 324

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

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



442
443
444
445
446
447
448
449
450
451
# File 'app/helpers/agile_helper.rb', line 442

def self.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.localtime.strftime(format)
  value.strftime(format)
end

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



463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
# File 'app/helpers/agile_helper.rb', line 463

def self.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.ljust(decimals, '0')[0, decimals]
  # slice whole on chunks of 3
  whole_a = []
  while whole.size > 0 do
    n = whole.size >= 3 ? 3 : whole.size
    whole_a << whole.slice!(n*-1, n)
  end
  # put it all back
  "#{sign}#{whole_a.reverse.join(delimiter)}#{separator}#{dec}"
end

.forms_merge(hash1, hash2) ⇒ Object

Merges two forms when current form extends other form. Subroutine of agile_form_read. With a little help of www.ruby-forum.com/topic/142809



361
362
363
364
365
366
367
368
369
370
371
372
# File 'app/helpers/agile_helper.rb', line 361

def self.forms_merge(hash1, hash2)
  target = hash1.dup
  hash2.keys.each do |key|
    if hash2[key].is_a?(Hash) && hash1[key].is_a?(Hash)
      target[key] = AgileHelper.forms_merge(hash1[key], hash2[key])
      next
    end
    target[key] = hash2[key] == '/' ? nil :  hash2[key]
  end
  # delete keys with nil value
  target.delete_if { |k, v| v.nil? }
end

.get_field_form_definition(name, form) ⇒ Object

Will return field form definition if field is defined on form. Field definition will then be used for creating filter input field.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/helpers/agile_helper.rb', line 51

def self.get_field_form_definition(name, form) #:nodoc:
  return if form['form'].nil?

  found = nil
  if form['form']['tabs']
    form['form']['tabs'].each do |tab, data|
      found = data.find{ |k, v| k.is_a?(Integer) && v['name'] == name }
      break if found
    end
  elsif form['form']['fields']
    found = form['form']['fields'].find{ |k, v| k.is_a?(Integer) && v['name'] == name }
  end
  found ? found.last : nil
end

.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.choices_for_name locale. This is how select field options are translated. Method returns selected choice translated to current locale.

Parameters:

model

String. Table (table) 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'
agile_text_for_value('agile_user', 'state', @record.active )

# usage in form
columns:
  2:
    name: state
    eval: agile_text_for_value agile_user, state

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



426
427
428
429
430
431
432
433
# File 'app/helpers/agile_helper.rb', line 426

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

  choices = t("helpers.label.#{model}.choices_for_#{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.



388
389
390
391
392
393
394
395
396
397
398
399
400
# File 'app/helpers/agile_helper.rb', line 388

def self.t(key, default = nil)
  return default if key.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 set
    if c.class == Hash || c.match(/translation missing/i)
      c = default.nil? ? key : default
    end
  end
  c
end

.table_param(params) ⇒ Object

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



331
332
333
# File 'app/helpers/agile_helper.rb', line 331

def self.table_param(params)
  params[:table] || params[:t]
end

Instance Method Details

#agile_field_action(yaml) ⇒ Object

Creates code for including data entry field in index actions.



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'app/helpers/agile_helper.rb', line 158

def agile_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 = AgileHelper.get_field_form_definition(yaml['name'], @form) )
    # some options may be redefined
    field_definition['size'] = yaml['size'] if yaml['size']
    field, label, help = agile_field_label_help(field_definition)
  else
    yaml['type'] = yaml['field_type']
    field, label, help = agile_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

#agile_field_label_help(options) ⇒ Object

Return field code, label and help text for a field defined on a AgileRails Form.

Parameters: options : Hash : Field definition

Returns: Array

field_html : String : HTML code for field definition
label : String : Label text
help : String : Help text


77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/helpers/agile_helper.rb', line 77

def agile_field_label_help(options)
  label, help = agile_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 AgileFormFields.const_defined?(klass_string) # when field type defined
      klass = AgileFormFields.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

#agile_form_idObject

Will return form id, to be used on each form for simpler css selecting.



317
318
319
# File 'app/helpers/agile_helper.rb', line 317

def agile_form_id
  %( id=#{AgileHelper.form_param(params) || AgileHelper.table_param(params)} )
end

#agile_get_caption(yaml) ⇒ Object

There are several options for defining caption (caption,label, text). This method will ensure that caption is returned anyhow provided.



191
192
193
# File 'app/helpers/agile_helper.rb', line 191

def agile_get_caption(yaml)
  yaml['caption'] || yaml['text'] || yaml['label']
end

#agile_get_html_data(yaml) ⇒ Object

Create ex. class=“my-class” html code from html options for action



181
182
183
184
185
# File 'app/helpers/agile_helper.rb', line 181

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

  yaml.reject{ |k, v| v.nil? }.map{ |k, v| "#{k}=\"#{v}\"" }.join(' ')
end

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


107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'app/helpers/agile_helper.rb', line 107

def agile_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 options['type'] == 'check_box'
  if options['name']
    label = if label.to_s.empty?
              t_label_for_field(options['name'], options['name'].capitalize.gsub('_',' ') )
            elsif options['name']
              t(label, label)
            end
  end
  # help text can be defined in form or in translations starting with helpers. or as helpers.help.collection.field
  help = options['help']
  if help.blank?
    help = if options['name']
             # if defined as i18n_prefix replace "label" with "help"
             prefix = @form['i18n_prefix'] ? @form['i18n_prefix'].sub('label', 'help') : "helpers.help.#{@form['table']}"
             "#{prefix}.#{options['name']}"
           end
    help = help.to_s
  end
  help = t(help, ' ') if help.to_s.match(/help\./)

  [label, help]
end

Creates code for link, ajax or windows action for index or form actions.

Parameters:

yaml: Hash : Action definition
record : Object : Currently selected record if available
action_active : Boolean : Is action active or disabled

Returns:

String : HTML code for action


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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'app/helpers/agile_helper.rb', line 206

def agile_link_ajax_window_submit_action(yaml, record = nil, action_active = true)
  parms = {}
  caption = agile_get_caption(yaml)
  caption = caption ? t(caption.downcase.to_s, caption) : nil
  icon    = yaml['icon'] ? mi_icon(yaml['icon']).to_s : ''
  # action is not active
  unless agile_is_action_active?(yaml)
    return %(<li><div class="ar-link-no">#{icon} #{caption}</div></li>)
  end
  # set data-confirm when confirm shortcut present
  yaml['html'] ||= {}
  text = yaml['html']['data-confirm'] || yaml['confirm']
  yaml['html']['data-confirm'] = t(text) if text.present?

  text = yaml['html']['title'] || yaml['title']
  yaml['html']['title'] = t(text) if text.present?

  yaml['html']['target'] ||= yaml['target']
  # direct url
  if yaml['url']
    parms['url'] = yaml['url']
    parms['idr'] = record.id if record
  # make url from action controller
  else
    parms['controller'] = yaml['controller'] || :agile
    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'] = record.id if record
  # overwrite with or add additional parameters from environment or record
  yaml['params'].each { |k, v| parms[k] = agile_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 = agile_get_html_data(yaml['html'])
  url_forward_params(parms)
  url = url_for(parms) rescue 'URL error'
  url = nil if parms['url'] == '#'
  request = yaml['request'] || yaml['method'] || 'get'

  code = case yaml['type']
  # ajax button
  when 'ajax'
    clas = 'ar-link-ajax'
    %(<div class="#{clas}" data-url="#{action_active ? url : ''}" #{html_data}
       data-request="#{request}" title="#{yaml['title']}">#{icon}#{caption}</div>)

  # submit button
  when 'submit'
    # 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 = 'ar-action-submit'
    %(<div class="#{clas}" data-url="#{action_active ? url : ''}" #{html_data}
       data-request="#{request}" title="#{yaml['title']}">#{icon}#{caption}</div>)

  # link button
  when 'link'
    yaml['html'] = agile_yaml_add_option(yaml['html'], class: 'ar-link')
    link = agile_link_to(caption, yaml['icon'], parms, yaml['html'] )
    (action_active ? link : caption).to_s

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

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

  else
    'Type error!'
  end
  "<li>#{code}</li>"
end

#agile_log_exception(exception, msg = '') ⇒ Object

Log exception to rails log. Usefull for debugging eval errors.



306
307
308
309
310
311
312
# File 'app/helpers/agile_helper.rb', line 306

def agile_log_exception(exception, msg = '')
  log  = "\n*** ERROR ***\n"
  log += exception ? "#{msg}: #{exception.message}\n#{exception.backtrace.first.inspect}\n" : msg.to_s
  log += "\nAgileRails Form: #{AgileHelper.form_param(params)}, line: #{session[:form_processing]}\n"

  logger.error log
end

#agile_script_action(yaml) ⇒ Object

Creates code for script action type.



36
37
38
39
40
41
42
43
44
45
# File 'app/helpers/agile_helper.rb', line 36

def agile_script_action(yaml)
  icon = agile_icon_for_link yaml['icon']
  yaml['html'] ||= {}
  yaml['html']['data-url'] = 'script'
  yaml['html']['data-request'] = 'script'
  yaml['html']['data-script'] = (yaml['js'] || yaml['script']).to_s
  yaml['html']['class'] ||= 'ar-link-ajax'
  attributes = yaml['html'].map { |k, v| "#{k}=\"#{v}\"" }.join(' ')
  %(<li><div #{attributes}>#{icon} #{ t(yaml['caption'], yaml['caption']) }</div></li>)
end

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


145
146
147
148
149
150
151
152
153
# File 'app/helpers/agile_helper.rb', line 145

def agile_tab_label_help(tab_name)
  label = @form.dig('form', 'tabs', tab_name, 'caption') || tab_name
  label = t(label, t_label_for_field(label, label))
  help = @form.dig('form', 'tabs', tab_name, 'help') || "helpers.help.#{@form['table']}.#{tab_name}"
  help = t(help, t_label_for_field(help, help))
  help = nil if help.match('helpers.') # help not found in translation

  [label, help]
end

#agile_yaml_add_option(source, options) ⇒ Object

Add new option to yaml. Subroutine of agile_link_ajax_window_submit_action.



293
294
295
296
297
298
299
300
301
# File 'app/helpers/agile_helper.rb', line 293

def agile_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