Module: AgileEditHelper

Defined in:
app/helpers/agile_edit_helper.rb

Overview

AgileEditHelper module defines helper methods used by AgileRails form edit action. Output is controlled by data found in 3 major sections of AgileRails form: index, data_set and form sections.

Instance Method Summary collapse

Instance Method Details

#add_one_step(key, tab_name, key_number) ⇒ Object



529
530
531
532
# File 'app/helpers/agile_edit_helper.rb', line 529

def add_one_step(key, tab_name, key_number)
  fields = tab_name ? @form['form']['tabs'][tab_name] :  @form['form']['fields']
  { key_number => fields[key] }
end

#add_step_to_form(index, step, next_step) ⇒ Object



554
555
556
557
# File 'app/helpers/agile_edit_helper.rb', line 554

def add_step_to_form(index, step, next_step)
  @form['form']['actions'][index]['params']['step'] = step
  @form['form']['actions'][index]['params']['next_step'] = next_step
end

#agile_actions_for_form(position) ⇒ Object

Creates actions div for edit form.

Displaying readonly form turned out to be challenge. For now when readonly parameter has value 2, back link will force readonly form. Value 1 or not set will result in normal link.



93
94
95
96
97
98
99
100
101
102
103
104
105
106
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'app/helpers/agile_edit_helper.rb', line 93

def agile_actions_for_form(position)
  actions_pos = agile_check_and_default(@form['form']['actions_position'], Agile.config(:default_actions_position), %w[top bottom both])
  return '' if actions_pos != 'both' && (position != actions_pos)# == 'bottom') || (position == 'bottom' && actions_pos == 'top')

  # create standard actions
  actions = @form['form']['actions'] || {}
  std_actions = Agile.config(:form_standard_actions)
  std_actions = { 1 => 'back' } if @form['readonly']   # readonly
  if actions.class == String
    actions = process_standard_actions(actions, std_actions)
  elsif actions['standard']
    actions.merge!(std_actions)
    actions.delete('standard')
  end

  # some actions have meaning only when editing
  if @record.try(:id).nil?
    actions_remove_action(actions, 'new', 'enable', 'refresh')
    # enable disable if active field is present
  elsif @record.has_attribute?(:active)
    index = actions.find { |k, v| v.is_a?(String) && v == 'enable' }
    actions[index.first] = (@record.active ? 'disable' : 'enable') if index
  else
    actions_remove_action(actions, 'enable')
  end
  # Actions are strictly forbidden
  if @form['form']['actions'] && agile_dont?(@form['form']['actions'])
    actions = []
  end

  # request for close window button present
  if actions.kind_of?(Hash) && params[:window_close].present?
    case params[:window_close].to_i
    when 0
      actions[1] = 'close'
      actions_remove_action(actions, 'new')

    when 1, 2
      actions = { 1 => 'close' }
    end
    # if save & back present with close action change it to save
    index = actions.find { |k, v| v.is_a?(String) && v == 'save&back' }
    actions[index.first] = 'save' if index
  end

  # select only integer keys and sort
  actions = actions.to_a.select{ _1.first.is_a?(Integer) }.sort{ |x, y| x[0] <=> y[0] }
  # Add spinner to the beginning
  html = %(<span class="ar-spinner">#{mi_icon('settings-o spin')}</span><ul class="ar-edit-menu #{position}">)

  actions.each do |key, options|
    session[:form_processing] = "form:actions: #{key} #{options}"
    next if options.nil?  # yes it happens

    parms = @form_params.clone
    if options.class == String
      next if @form['readonly'] && !options.match(/back|close/)

      partial =
        case options

        when 'save&back', 'save'
          html += form_submit_action(options) # already comes with li tags
          next

        when 'back', 'cancel'
          # If return_to is present link directly to URL
          if parms['xreturn_to'] # disabled for now
            agile_link_to( 'agile.back', 'arrow_back', parms['return_to'], class: 'ar-link' )
          else
            parms['action']   = 'index'
            parms['readonly'] = parms['readonly'].to_s.to_i < 2 ? nil : 1

            agile_link_to( 'agile.back', 'arrow_back', parms, class: 'ar-link' )
          end

        when 'new'
          parms['action'] = options
          agile_link_to( 'agile.new', 'add', parms, class: 'ar-link')

        when 'enable', 'disable'
          parms['operation'] = options
          parms['id']        = @record.id
          icon = (options == 'enable' ? 'check_box-o' : 'check_box_outline_blank')
          agile_link_to( "agile.#{options}",icon, parms, method: :delete, class: 'ar-link' )

        when 'refresh'
          %(<div class="ar-link refresh">#{mi_icon('refresh')} #{t('agile.refresh')}</div>)
          
        when 'close'
          close = params[:window_close].to_i
          if close < 2
            %(<div class="ar-link close">#{mi_icon('close')} #{t('agile.close')}</div>)
          else
            %(<div class="ar-link back">#{mi_icon('close')} #{t('agile.close')}</div>)
          end

        else
          "err1 #{key}=>#{options}"
        end
      html += "<li>#{partial}</li>"

    # non standard actions      
    else
      # action will be displayed when show: always or readonly option is declared and form is readonly
      next if @form['readonly'] && !%w[readonly always].include?(options['show'].to_s)

      options['title'] = t("#{options['title'].downcase}", options['title']) if options['title']
      html +=
        case
        # submit button
        when options['type'] == 'submit'
          form_submit_action(options)

        # ajax or link button
        when %w(ajax link window popup).include?(options['type'])
          agile_link_ajax_window_submit_action(options, @record)

        # Javascript action
        when options['type'] == 'script'
          agile_script_action(options)
        else
          '<li>err2</li>'
        end
    end

  end
  (html += '</ul>').html_safe
end

#agile_background_for_dataset(start) ⇒ Object

Create background div and table definitions for dataset.



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

def agile_background_for_dataset(start)
  if start == :start
    style = @form.dig('index', 'data_set', 'table_style')
    html  = '<div class="ar-result-div" '
    html += (style ? 'style="overflow-x: scroll;" >' : '>')

    html += "\n<div class=\"ar-result #{@form['index']['data_set']['table_class']}\" "
    html += ( style ? "style=\"#{style}\" >" : '>')
  else
    html = '</div></div>'
  end
  html.html_safe
end

#agile_check_and_default(value, default, values = nil) ⇒ Object

Checks if value is defined and sets default. If values are sent it also checks if value is found in values. If not it will report error and set value to default. Subroutine of agile_fields_for_tab.



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'app/helpers/agile_edit_helper.rb', line 245

def agile_check_and_default(value, default, values = nil) # :nodoc:
  return default if value.nil?

  # check if value is within allowed values
  if values && !values.index(value)
    # parameters should be in downcase
    if n = values.index(value.downcase)
      return values[n]
    else
      logger.error("AgileRails Forms: Value #{value} not within values [#{values.join(',')}]. Default #{default} used!")
      return default
    end
  end
  value
end

#agile_document_statisticsObject

Creates current document statistics div (created_by, created_at, ....) at the bottom of edit form.

  • lots of more. At the moment also adds icon for dumping current record as json text.


353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# File 'app/helpers/agile_edit_helper.rb', line 353

def agile_document_statistics
  return '' if @record.id.nil? || agile_dont?(@form['form']['info'])

  html =  %(<div id="ar-document-info">#{mi_icon('info md-18')}</div> <div id="ar-document-info-popup" class="div-hidden">)
  u = agile_document_user_for('created_by')
  html += %(<div><span>#{t('agile.created_by', 'Created by')}: </span><span>#{u}</span></div>) if u
  u = agile_document_user_for('updated_by')
  html += %(<div><span>#{t('agile.updated_by', 'Updated by')}: </span><span>#{u}</span></div>) if u

  html += %(<div><span>#{t('agile.created_at', 'Created at')}: </span><span>#{agile_format_value(@record.created_at)}</span></div>) if @record['created_at']
  html += %(<div><span>#{t('agile.updated_at', 'Updated at')}: </span><span>#{agile_format_value(@record.updated_at)}</span></div>) if @record['updated_at']
  # copy to clipboard icon
  parms = params.clone
  parms[:controller] = :agile_common
  parms[:action]     = :copy_clipboard
  url = url_for(parms.permit!)
  html += '<div>'
  html += mi_icon('content_copy-o md-18', class: 'ar-link-img ar-link-ajax',
                  'data-url' => url, 'data-request' => 'get', title: t('agile.copy_clipboard') )

  url = url_for(controller: :agile, action: :run, table: 'ar_journal', control: 'agile.filter_on',
                filter_oper: 'eq', filter_field: 'id', filter_value: "#{@form['table']};#{@record.id}")

  html += mi_icon('history md-18', class: 'ar-link-img ar-window-open',
                  'data-url' => url, title: t('helpers.label.ar_journal.table_title') )
  html += %(<span>ID: </span>
            <span id="record-id" class="hover" onclick="agile_copy_to_clipboard('record-id');" title="Copy document ID to clipboard">#{@record.id}
            </span>)

  (html += '</div></div>').html_safe
end

#agile_document_user_for(field_name) ⇒ Object

Returns username for id. Subroutine of agile_document_statistics



342
343
344
345
346
347
# File 'app/helpers/agile_edit_helper.rb', line 342

def agile_document_user_for(field_name) #:nodoc:
  return if @record[field_name].nil?

  user = ArUser.find(@record[field_name])
  user ? user.name : @record[field_name]
end

#agile_fields_for_formObject

Creates input fields defined in form options



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'app/helpers/agile_edit_helper.rb', line 264

def agile_fields_for_form
  html = '<div id="data_fields" ' + (@form['form']['height'] ? "style=\"height: #{@form['form']['height']}px;\">" : '>')
  @js  ||= ''
  @css ||= ''
  # fields
  if (form_fields = @form['form']['fields'])
    html += "#{agile_input_form(form_fields)}</div>"
  elsif @form['form']['tabs'] #tabs
    html = agile_tabs_form()
  end
  # add last_updated_at hidden field so controller can check if record was updated in db during editing
  html += hidden_field(nil, :last_updated_at, value: @record.updated_at.to_i) if @record.respond_to?(:updated_at)
  # add form time stamp to prevent double form submit
  html += hidden_field(nil, :form_time_stamp, value: Time.now.to_i)
  # add javascript code if defined by form
  @js  += "\n#{@form['script']} #{@form['js']}"
  @css += "\n#{@form['css']}\n#{@form['form']['css']}"
  html.html_safe
end

#agile_form_leftObject

If form is divided into two parts, this method gathers html to be painted on right side of the form pane.



399
400
401
402
403
404
405
406
407
# File 'app/helpers/agile_edit_helper.rb', line 399

def agile_form_left
  yaml = @form.dig('form', 'form_left')
  return '' unless yaml

  html = ''
  html += agile_process_eval(yaml['eval'], self) if yaml['eval']

  html.html_safe
end

#agile_head_for_formObject

Creates head form div. Head form div is used to display header data useful to be seen even when tabs are switched.



288
289
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'app/helpers/agile_edit_helper.rb', line 288

def agile_head_for_form
  @css ||= ''
  head = @form['form']['head']
  return '' if head.nil?

  html    = %(<div class="ar-head #{head['class']}">\n<div class="ar-row">)
  split   = head['split'] || 4
  percent = 100/split
  current = 0
  head_fields = head.select {|field| field.class == Integer }
  head_fields.to_a.sort.each do |number, options|
    session[:form_processing] = "form: head: #{number}=#{options}"
    # Label
    caption = options['caption']
    span    = options['span'] || 1
    @css += "\n#{options['css']}" unless options['css'].blank?
    label = if caption.blank?
      ''
    elsif options['name'] == caption
      t_label_for_field(options['name'], options['name'].capitalize.gsub('_',' ') )
    else
      t(caption, caption)
    end
    # Field value
    begin
      field = if options['eval']
        agile_process_column_eval(options, @record)
      else
        @record.send(options['name'])
      end
    rescue Exception => e
      agile_log_exception(e, 'agile_head_for_form')
      field = '!!!Error'
    end
    #
    klass = agile_style_or_class(nil, options['class'], field, @record)
    style = agile_style_or_class(nil, options['style'], field, @record)
    html += %(<div class="ar-column #{klass}" style="width:#{percent*span}%;#{style}">
  #{label.blank? ? '' : "<span class=\"label\">#{label}</span>"}
  <span id="head-#{options['name']}" class="field">#{field}</span>
</div>)
    current += span
    if current == split
      html += %(</div>\n<div class="ar-row">)
      current = 0
    end
  end
  html += '</div></div>'
  html.html_safe
end

#agile_is_action_active?(options) ⇒ Boolean

Creates actions div for edit form.

Displaying readonly form turned out to be challenge. For now when readonly parameter has value 2, back link will force readonly form. Value 1 or not set will result in normal link.

Returns:

  • (Boolean)


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/helpers/agile_edit_helper.rb', line 53

def agile_is_action_active?(options)
  return true unless options['active']

  # alias record and document so both can be used in eval
  record = document = @record
  option = options['active']
  case 
  # usually only for test
  when option.class == TrueClass || option['eval'].class == TrueClass then true

  when option.class == String then
    if option.match(/new_record/i)
      (@record.new_record? && option == 'new_record') || (!@record.new_record? && option == 'not_new_record')
    elsif option.match(/\./)
      # shortcut for method and eval option
      agile_process_eval(option, self)
    else
      eval(option['eval'])
    end

  # direct evaluate expression
  when option['eval'] then
    eval(option['eval'])

  # if record present send record otherwise send self as parameter
  when option['method'] then
    agile_process_eval(option['method'], self)

  else
    false
  end  
end

#agile_update_formObject

Updates form prior to processing form



388
389
390
391
392
393
# File 'app/helpers/agile_edit_helper.rb', line 388

def agile_update_form
  # update form for steps options
  if @form.dig('form', 'steps')
    agile_update_form_steps
  end
end

#agile_value_for_parameter(param, current_document = nil) ⇒ Object

Will return value when internal or additional parameters are defined in action Subroutine of agile_actions_for_form.



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

def agile_value_for_parameter(param, current_document = nil)#:nodoc:
  if param.class == Hash
    agile_internal_var(param['object'] ||= 'record', param['method'], current_document)
  elsif param.to_s.match(/record|document/)
    current_document ? current_document : @record
  else
    param
  end
end