Module: Wice::GridViewHelper

Defined in:
lib/wice/helpers/wice_grid_misc_view_helpers.rb,
lib/wice/helpers/wice_grid_view_helpers.rb,
lib/wice/helpers/wice_grid_serialized_queries_view_helpers.rb

Overview

Various grid related helpers

Instance Method Summary collapse

Instance Method Details

:nodoc:



648
649
650
651
652
653
654
655
656
657
658
# File 'lib/wice/helpers/wice_grid_view_helpers.rb', line 648

def back_to_pagination_link(parameters, grid_name) #:nodoc:
  pagination_override_parameter_name = "#{grid_name}[pp]"
  parameters = parameters.reject { |k, _v| k == pagination_override_parameter_name }

  (:a, NlMessage['switch_back_to_paginated_mode_label'],
              href: '#',
              title: NlMessage['switch_back_to_paginated_mode_tooltip'],
              class: 'wg-back-to-pagination-link',
              'data-grid-state' => parameters.to_json
  )
end

#call_block(block, ar, extra_argument = nil) ⇒ Object

:nodoc:



164
165
166
# File 'lib/wice/helpers/wice_grid_view_helpers.rb', line 164

def call_block(block, ar, extra_argument = nil)  #:nodoc:
  extra_argument ? block.call(ar, extra_argument) : block.call(ar)
end

#define_grid(grid, opts = {}, &block) ⇒ Object

Has the same parameters as grid but does not output the grid. After define_grid render_grid can be used to output the grid HTML code. Usually used with detached filters: first define_grid, then grid_filters, and then render_grid



82
83
84
85
86
87
88
89
90
91
92
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
# File 'lib/wice/helpers/wice_grid_view_helpers.rb', line 82

def define_grid(grid, opts = {}, &block)
  # strip the method from HTML stuff
  unless grid.class == WiceGrid
    raise WiceGridArgumentError.new('The first argument for the grid helper must be an instance of the WiceGrid class')
  end

  options = {
    allow_showing_all_records:     Defaults::ALLOW_SHOWING_ALL_RECORDS,
    class:                         nil,
    extra_request_parameters:      {},
    header_tr_html:                {},
    hide_reset_button:             false,
    hide_submit_button:            false,
    hide_csv_button:               false,
    show_filters:                  Defaults::SHOW_FILTER,
    sorting_dependant_row_cycling: false,
    html:                          {},
    upper_pagination_panel:        Defaults::SHOW_UPPER_PAGINATION_PANEL,
    pagination_theme: ConfigurationProvider.value_for(:PAGINATION_THEME)
  }

  opts.assert_valid_keys(options.keys)

  options.merge!(opts)

  options[:show_filters] = :no     if options[:show_filters] == false
  options[:show_filters] = :always if options[:show_filters] == true

  rendering = GridRenderer.new(grid, self)

  block.call(rendering) # calling block containing column() calls

  reuse_last_column_for_filter_buttons =
    Defaults::REUSE_LAST_COLUMN_FOR_FILTER_ICONS && rendering.last_column_for_html.capable_of_hosting_filter_related_icons?

  if grid.output_csv?
    grid_csv(grid, rendering)
  else
    # If blank_slate is defined we don't show any grid at all
    if rendering.blank_slate_handler && grid.resultset.size == 0 && !grid.filtering_on?
      generate_blank_slate(grid, rendering)
    else
      grid_html(grid, options, rendering, reuse_last_column_for_filter_buttons)
    end
  end

  grid.view_helper_finished = true

  grid.csv_tempfile ? grid.csv_tempfile.path : nil
end

#dump_filter_parameters_as_hidden_fields(grid) ⇒ Object

This method dumps all HTTP parameters related to filtering and ordering of a certain grid as hidden form fields. This might be required if you want to keep the state of a grid while reloading the page using other forms.

The only parameter is a grid object returned by initialize_grid in the controller.



9
10
11
12
13
14
15
16
17
# File 'lib/wice/helpers/wice_grid_misc_view_helpers.rb', line 9

def dump_filter_parameters_as_hidden_fields(grid)
  unless grid.is_a? WiceGrid
    raise WiceGridArgumentError.new('dump_filter_parameters_as_hidden_fields: the parameter must be a WiceGrid instance.')
  end

  grid.get_state_as_parameter_value_pairs(true).collect do|param_name, value|
    hidden_field_tag(param_name, value, id: "hidden-#{param_name.gsub(/[\[\]]/, '-')}")
  end.join("\n").html_safe
end

#dump_state(grid) ⇒ Object

display the state of the grid



44
45
46
# File 'lib/wice/helpers/wice_grid_misc_view_helpers.rb', line 44

def dump_state(grid)  #:nodoc:
  debug(grid.get_state_as_parameter_value_pairs)
end

#filter_and_order_state_as_hash(grid) ⇒ Object

This method dumps all HTTP parameters related to filtering and ordering of a certain grid in the form of a hash. This might be required if you want to keep the state of a grid while reloading the page using Rails routing helpers.

The only parameter is a grid object returned by initialize_grid in the controller.



33
34
35
36
37
38
39
40
41
# File 'lib/wice/helpers/wice_grid_misc_view_helpers.rb', line 33

def filter_and_order_state_as_hash(grid)
  {
    grid.name => {
      'f'               => grid.status[:f],
      'order'           => grid.status[:order],
      'order_direction' => grid.status[:order_direction]
    }
  }
end

#filter_state_as_hash(grid) ⇒ Object

This method dumps all HTTP parameters related to filtering of a certain grid in the form of a hash. This might be required if you want to keep the state of a grid while reloading the page using Rails routing helpers. Attention: this does not return parameters for ordering the grid, use filter_and_order_state_as_hash if you need it.

The only parameter is a grid object returned by initialize_grid in the controller.



25
26
27
# File 'lib/wice/helpers/wice_grid_misc_view_helpers.rb', line 25

def filter_state_as_hash(grid)
  { grid.name => { 'f' => grid.status[:f] } }
end

#generate_blank_slate(grid, rendering) ⇒ Object

:nodoc:



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/wice/helpers/wice_grid_view_helpers.rb', line 146

def generate_blank_slate(grid, rendering) #:nodoc:
  grid.output_buffer = GridOutputBuffer.new

  grid.output_buffer << if rendering.blank_slate_handler.is_a?(Proc)
    call_block(rendering.blank_slate_handler, nil)
  elsif rendering.blank_slate_handler.is_a?(Hash)
    render(rendering.blank_slate_handler)
  else
    rendering.blank_slate_handler
  end

  # rubocop:disable Style/SymbolProc
  if rendering.find_one_for(:in_html) { |column| column.detach_with_id }
    grid.output_buffer.return_empty_strings_for_nonexistent_filters = true
  end
  # rubocop:enable Style/SymbolProc
end

#get_row_content(rendering, ar, sorting_dependant_row_cycling) ⇒ Object

:nodoc:



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
# File 'lib/wice/helpers/wice_grid_view_helpers.rb', line 168

def get_row_content(rendering, ar, sorting_dependant_row_cycling) #:nodoc:
  cell_value_of_the_ordered_column = nil
  row_content = ''
  rendering.each_column(:in_html) do |column|
    cell_block = column.cell_rendering_block

    opts = column.html

    opts = opts ? opts.clone : {}

    column_block_output = if column.class == Columns.get_view_column_processor(:action)
      cell_block.call(ar, params)
    else
      call_block(cell_block, ar)
    end

    if column_block_output.is_a?(Array)

      unless column_block_output.size == 2
        raise WiceGridArgumentError.new('When WiceGrid column block returns an array it is expected to contain 2 elements only - ' \
          'the first is the contents of the table cell and the second is a hash containing HTML attributes for the <td> tag.')
      end

      column_block_output, additional_opts = column_block_output

      unless additional_opts.is_a?(Hash)
        raise WiceGridArgumentError.new('When WiceGrid column block returns an array its second element is expected to be a ' \
                                        "hash containing HTML attributes for the <td> tag. The returned value is #{additional_opts.inspect}. Read documentation.")
      end

      additional_css_class = nil
      if additional_opts.key?(:class)
        additional_css_class = additional_opts[:class]
        additional_opts.delete(:class)
      elsif additional_opts.key?('class')
        additional_css_class = additional_opts['class']
        additional_opts.delete('class')
      end
      opts.merge!(additional_opts)
      Wice::WgHash.add_or_append_class_value!(opts, additional_css_class) unless additional_css_class.blank?
    end

    if sorting_dependant_row_cycling && column.attribute && grid.ordered_by?(column)
      cell_value_of_the_ordered_column = column_block_output
    end
    row_content += (:td, column_block_output, opts)
  end
  [row_content, cell_value_of_the_ordered_column]
end

#grid(grid, opts = {}, &block) ⇒ Object

View helper for rendering the grid.

The first parameter is a grid object returned by initialize_grid in the controller.

The second parameter is a hash of options:

  • :html - a hash of HTML attributes to be included into the table tag.

  • :class - a shortcut for html: {class: 'css_class'}

  • :header_tr_html - a hash of HTML attributes to be included into the first tr tag (or two first tr‘s if the filter row is present).

  • :show_filters - defines when the filter is shown. Possible values are:

    • :when_filtered - the filter is shown when the current table is the result of filtering

    • :always or true - show the filter always

    • :no or false - never show the filter

  • :upper_pagination_panel - a boolean value which defines whether there is an additional pagination panel on top of the table. By default it is false.

  • :extra_request_parameters - a hash which will be added as additional HTTP request parameters to all links generated by the grid, be it sorting links, filters, or the ‘Reset Filter’ icon. Please note that WiceGrid respects and retains all request parameters already present in the URL which formed the page, so there is no need to enumerate them in :extra_request_parameters. A typical usage of :extra_request_parameters is a page with javascript tabs - changing the active tab does not reload the page, but if one such tab contains a WiceGrid, it could be required that if the user orders or filters the grid, the result page should have the tab with the grid activated. For this we need to send an additional parameter specifying from which tab the request was generated.

  • :sorting_dependant_row_cycling - When set to true (by default it is false) the row styles odd and even will be changed only when the content of the cell belonging to the sorted column changes. In other words, rows with identical values in the ordered column will have the same style (color).

  • :allow_showing_all_records - allow or prohibit the “All Records” mode.

  • :hide_reset_button - Do not show the default Filter Reset button. Useful when using a custom reset button. By default it is false.

  • :hide_submit_button - Do not show the default Filter Submit button. Useful when using a custom submit button By default it is false.

  • :hide_csv_button - a boolean value which defines whether the default Export To CSV button should be rendered. Useful when using a custom Export To CSV button. By default it is false. Please read README for more insights.

The block contains definitions of grid columns using the column method sent to the object yielded into the block. In other words, the value returned by each of the blocks defines the content of a cell, the first block is called for cells of the first column for each row (each ActiveRecord instance), the second block is called for cells of the second column, and so on. See the example:

<%= grid(@accounts_grid, html: {class: 'grid_style', id: 'accounts_grid'}, header_tr_html: {class: 'grid_headers'}) do |g|

  g.column name: 'Username', attribute: 'username' do |account|
    account.username
  end

  g.column name: 'application_account.field.identity_id'._, attribute: 'firstname', model: Person do |account|
    link_to(account.identity.name, identity_path(account.identity))
  end

  g.column do |account|
    link_to('Edit', edit_account_path(account))
  end

end -%>

Defaults for parameters :show_filters and :upper_pagination_panel can be changed in lib/wice_grid_config.rb using constants Wice::Defaults::SHOW_FILTER and WiceGrid::Defaults::SHOW_UPPER_PAGINATION_PANEL, this is convenient if you want to set a project wide setting without having to repeat it for every grid instance.

Pease read documentation about the column method to achieve the enlightenment.



70
71
72
73
74
75
76
# File 'lib/wice/helpers/wice_grid_view_helpers.rb', line 70

def grid(grid, opts = {}, &block)
  raise WiceGridArgumentError.new('Missing block for the grid helper.' \
    ' For detached filters use first define_grid with the same API as grid, ' \
    'then grid_filter to add filters, and then render_grid to actually show the grid') if block.nil?
  define_grid(grid, opts, &block)
  render_grid(grid)
end

#grid_csv(grid, rendering) ⇒ Object

:nodoc:



589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
# File 'lib/wice/helpers/wice_grid_view_helpers.rb', line 589

def grid_csv(grid, rendering) #:nodoc:
  spreadsheet = ::Wice::Spreadsheet.new(grid.name, grid.csv_field_separator, grid.csv_encoding)

  # columns
  spreadsheet << rendering.column_labels(:in_csv)

  # rendering  rows
  grid.each do |ar| # rows
    row = []

    rendering.each_column(:in_csv) do |column|
      cell_block = column.cell_rendering_block

      column_block_output = call_block(cell_block, ar)

      if column_block_output.is_a?(Array)
        column_block_output, _additional_opts = column_block_output
      end

      row << column_block_output
    end
    spreadsheet << row
  end
  grid.csv_tempfile = spreadsheet.tempfile
end

#grid_filter(grid, filter_key) ⇒ Object

Renders a detached filter. The parameters are:

  • grid the WiceGrid object

  • filter_key an identifier of the filter specified in the column declaration by parameter :detach_with_id



571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
# File 'lib/wice/helpers/wice_grid_view_helpers.rb', line 571

def grid_filter(grid, filter_key)
  unless grid.is_a? WiceGrid
    raise WiceGridArgumentError.new('grid_filter: the parameter must be a WiceGrid instance.')
  end
  if grid.output_buffer.nil?
    raise WiceGridArgumentError.new("grid_filter: You have attempted to run 'grid_filter' before 'grid'. Read about detached filters in the documentation.")
  end
  if grid.output_buffer == true
    raise WiceGridArgumentError.new('grid_filter: You have defined no detached filters, or you try use detached filters with' \
      ':show_filters => :no (set :show_filters to :always in this case). Read about detached filters in the documentation.')
  end

   :span,
              grid.output_buffer.filter_for(filter_key),
              class: "wg-detached-filter #{grid.name}_detached_filter",
              'data-grid-name' => grid.name
end

#grid_html(grid, options, rendering, reuse_last_column_for_filter_buttons) ⇒ Object

the longest method? :(



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
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
# File 'lib/wice/helpers/wice_grid_view_helpers.rb', line 219

def grid_html(grid, options, rendering, reuse_last_column_for_filter_buttons) #:nodoc:
  table_html_attrs, header_tr_html = options[:html], options[:header_tr_html]

  Wice::WgHash.add_or_append_class_value!(table_html_attrs, 'wice-grid', true)

  if Array === Defaults::DEFAULT_TABLE_CLASSES
    Defaults::DEFAULT_TABLE_CLASSES.each do |default_class|
      Wice::WgHash.add_or_append_class_value!(table_html_attrs, default_class, true)
    end
  end

  if options[:class]
    Wice::WgHash.add_or_append_class_value!(table_html_attrs, options[:class])
    options.delete(:class)
  end

  cycle_class = nil
  sorting_dependant_row_cycling = options[:sorting_dependant_row_cycling]

  grid.output_buffer = GridOutputBuffer.new

  # Ruby 1.9.x
  grid.output_buffer.force_encoding('UTF-8') if grid.output_buffer.respond_to?(:force_encoding)

  grid.output_buffer << %(<div class="wice-grid-container table-responsive" data-grid-name="#{grid.name}" id="#{grid.name}"><div id="#{grid.name}_title">)
  grid.output_buffer << (:h3, grid.saved_query.name) if grid.saved_query
  grid.output_buffer << "</div><table #{public_tag_options(table_html_attrs, true)}>"
  grid.output_buffer << "<caption>#{rendering.kaption}</caption>" if rendering.kaption
  grid.output_buffer << '<thead>'

  no_filters_at_all = (options[:show_filters] == :no || rendering.no_filter_needed?)

  if no_filters_at_all
    no_rightmost_column = no_filter_row = no_filters_at_all
  else
    no_rightmost_column = no_filter_row = (options[:show_filters] == :no || rendering.no_filter_needed_in_main_table?) ? true : false
  end

  no_rightmost_column = true if reuse_last_column_for_filter_buttons

  number_of_columns = rendering.number_of_columns(:in_html)
  number_of_columns -= 1 if no_rightmost_column

  number_of_columns_for_extra_rows = number_of_columns + 1

  pagination_panel_content_html = nil
  if options[:upper_pagination_panel]
    grid.output_buffer << rendering.pagination_panel(number_of_columns, options[:hide_csv_button]) do
      pagination_panel_content_html =
        pagination_panel_content(grid, options[:extra_request_parameters], options[:allow_showing_all_records], options[:pagination_theme])
      pagination_panel_content_html
    end
  end

  title_row_attrs = header_tr_html.clone
  Wice::WgHash.add_or_append_class_value!(title_row_attrs, 'wice-grid-title-row', true)

  grid.output_buffer << %(<tr #{public_tag_options(title_row_attrs, true)}>)

  filter_row_id = grid.name + '_filter_row'

  # first row of column labels with sorting links

  filter_shown = if options[:show_filters] == :when_filtered
    grid.filtering_on?
  elsif options[:show_filters] == :always
    true
  end

  rendering.each_column_aware_of_one_last_one(:in_html) do |column, last|
    column_name = column.name

    opts = column.html

    opts = opts ? opts.clone : {}

    Wice::WgHash.add_or_append_class_value!(opts, column.css_class)

    if column.attribute && (column.ordering || column.sort_by)

      column.add_css_class('active-filter') if grid.filtered_by?(column)

      direction = 'asc'
      link_style = nil
      arrow_class = nil

      if grid.ordered_by?(column)
        column.add_css_class('sorted')
        Wice::WgHash.add_or_append_class_value!(opts, 'sorted')
        link_style = grid.order_direction

        case grid.order_direction
        when 'asc'
          direction = 'desc'
          arrow_class = 'down'
        when 'desc'
          direction = 'asc'
          arrow_class = 'up'
        end
      end

      col_link = link_to(
        (column_name +
          if arrow_class
            ' ' + (:i, '', class: "fa fa-arrow-#{arrow_class}")
          else
            ''
          end).html_safe,

        rendering.column_link(
          column,
          direction,
          params,
          options[:extra_request_parameters]
        ),
        class: link_style)

      grid.output_buffer << (:th, col_link, opts)

    else
      if reuse_last_column_for_filter_buttons && last
        grid.output_buffer << (:th,
                                          hide_show_icon(filter_row_id, grid, filter_shown, no_filter_row, options[:show_filters], rendering), opts
        )
      else
        grid.output_buffer << (:th, column_name, opts)
      end
    end
  end

  grid.output_buffer << (:th,
                                    hide_show_icon(filter_row_id, grid, filter_shown, no_filter_row, options[:show_filters], rendering)
  ) unless no_rightmost_column

  grid.output_buffer << '</tr>'
  # rendering first row end

  unless no_filters_at_all # there are filters, we don't know where, in the table or detached
    if no_filter_row # they are all detached
      rendering.each_column(:in_html) do |column|
        if column.filter_shown?
          filter_html_code = column.render_filter.html_safe
          grid.output_buffer.add_filter(column.detach_with_id, filter_html_code)
        end
      end

    else # some filters are present in the table

      filter_row_attrs = header_tr_html.clone
      Wice::WgHash.add_or_append_class_value!(filter_row_attrs, 'wg-filter-row', true)
      filter_row_attrs['id'] = filter_row_id

      grid.output_buffer << %(<tr #{public_tag_options(filter_row_attrs, true)} )
      grid.output_buffer << 'style="display:none"' unless filter_shown
      grid.output_buffer << '>'

      rendering.each_column_aware_of_one_last_one(:in_html) do |column, last|
        opts = column.html ? column.html.clone : {}
        Wice::WgHash.add_or_append_class_value!(opts, column.css_class)

        if column.filter_shown?

          filter_html_code = column.render_filter.html_safe
          if column.detach_with_id
            grid.output_buffer << (:th, '', opts)
            grid.output_buffer.add_filter(column.detach_with_id, filter_html_code)
          else
            grid.output_buffer << (:th, filter_html_code, opts)
          end
        else
          if reuse_last_column_for_filter_buttons && last
            grid.output_buffer << (:th,
                                              reset_submit_buttons(options, grid, rendering),
                                              Wice::WgHash.add_or_append_class_value!(opts, 'filter_icons')
            )
          else
            grid.output_buffer << (:th, '', opts)
          end
        end
      end
      unless no_rightmost_column
        grid.output_buffer << (:th, reset_submit_buttons(options, grid, rendering), class: 'filter_icons')
      end
      grid.output_buffer << '</tr>'
    end
  end

  grid.output_buffer << '</thead><tfoot>'
  grid.output_buffer << rendering.pagination_panel(number_of_columns, options[:hide_csv_button]) do
    if pagination_panel_content_html
      pagination_panel_content_html
    else
      pagination_panel_content_html =
        pagination_panel_content(grid, options[:extra_request_parameters], options[:allow_showing_all_records], options[:pagination_theme])
      pagination_panel_content_html
    end
  end

  grid.output_buffer << '</tfoot><tbody>'

  # rendering  rows
  cell_value_of_the_ordered_column = nil
  previous_cell_value_of_the_ordered_column = nil

  grid.each do |ar| # rows
    before_row_output = if rendering.before_row_handler
      call_block(rendering.before_row_handler, ar, number_of_columns_for_extra_rows)
    end

    after_row_output = if rendering.after_row_handler
      call_block(rendering.after_row_handler, ar, number_of_columns_for_extra_rows)
    end

    replace_row_output = if rendering.replace_row_handler
      call_block(rendering.replace_row_handler, ar, number_of_columns_for_extra_rows)
    end

    row_content = if replace_row_output
      no_rightmost_column = true
      replace_row_output
    else
      row_content, tmp_cell_value_of_the_ordered_column = get_row_content(rendering, ar, sorting_dependant_row_cycling)
      cell_value_of_the_ordered_column = tmp_cell_value_of_the_ordered_column if tmp_cell_value_of_the_ordered_column
      row_content
    end

    row_attributes = rendering.get_row_attributes(ar)

    if sorting_dependant_row_cycling
      cycle_class = cycle('odd', 'even', name: grid.name) if cell_value_of_the_ordered_column != previous_cell_value_of_the_ordered_column
      previous_cell_value_of_the_ordered_column = cell_value_of_the_ordered_column
    else
      cycle_class = cycle('odd', 'even', name: grid.name)
    end

    Wice::WgHash.add_or_append_class_value!(row_attributes, cycle_class)

    grid.output_buffer << before_row_output if before_row_output
    grid.output_buffer << "<tr #{public_tag_options(row_attributes)}>#{row_content}"
    grid.output_buffer << (:td, '') unless no_rightmost_column
    grid.output_buffer << '</tr>'
    grid.output_buffer << after_row_output if after_row_output
  end

  last_row_output = if rendering.last_row_handler
    call_block(rendering.last_row_handler, number_of_columns_for_extra_rows)
  end

  grid.output_buffer << last_row_output if last_row_output

  grid.output_buffer << '</tbody></table>'

  base_link_for_filter, base_link_for_show_all_records = rendering.base_link_for_filter(controller, options[:extra_request_parameters])

  link_for_export = rendering.link_for_export(controller, 'csv', options[:extra_request_parameters])

  parameter_name_for_query_loading = { grid.name => { q: '' } }.to_query
  parameter_name_for_focus = { grid.name => { foc: '' } }.to_query

  processor_initializer_arguments = [
    base_link_for_filter,
    base_link_for_show_all_records,
    link_for_export,
    parameter_name_for_query_loading,
    parameter_name_for_focus,
    Rails.env
  ]

  filter_declarations = if no_filters_at_all
    []
  else
    rendering.select_for(:in_html) do |vc|
      vc.attribute && vc.filter
    end.collect(&:yield_declaration)
  end

  wg_data = {
    'data-processor-initializer-arguments' => processor_initializer_arguments.to_json,
    'data-filter-declarations'             => filter_declarations.to_json,
    :class                                 => 'wg-data'
  }

  wg_data['data-foc'] = grid.status['foc'] if grid.status['foc']

  grid.output_buffer << (:div, '', wg_data)

  grid.output_buffer << '</div>'

  if Rails.env.development?
    grid.output_buffer << javascript_tag(%/ document.ready = function(){ \n/ +
      %$ if (typeof(WiceGridProcessor) == "undefined"){\n$ +
      %$   alert("wice_grid.js not loaded, WiceGrid cannot proceed!\\n" +\n$ +
      %(     "Make sure that you have loaded wice_grid.js.\\n" + ) +
      %(     "Add line //= require wice_grid.js " + ) +
      %$     "to app/javascript/application.js")\n$ +
      %( } ) +
      %$ } $)
  end

  grid.output_buffer
end

#hide_show_icon(_filter_row_id, _grid, filter_shown, no_filter_row, show_filters, _rendering) ⇒ Object

:nodoc:



521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
# File 'lib/wice/helpers/wice_grid_view_helpers.rb', line 521

def hide_show_icon(_filter_row_id, _grid, filter_shown, no_filter_row, show_filters, _rendering)  #:nodoc:
  no_filter_opening_closing_icon = (show_filters == :always) || no_filter_row

  styles = ['display: block;', 'display: none;']
  styles.reverse! unless filter_shown

  if no_filter_opening_closing_icon
    ''
  else

    (:div, (:i, '', class: 'fa-regular fa-eye-slash'),
                title: NlMessage['hide_filter_tooltip'],
                style: styles[0],
                class: 'clickable  wg-hide-filter'
    ) +

      (:div, (:i, '', class: 'fa-regular fa-eye'),
                  title: NlMessage['show_filter_tooltip'],
                  style: styles[1],
                  class: 'clickable  wg-show-filter'
      )

  end
end

#pagination_info(grid, allow_showing_all_records) ⇒ Object

:nodoc:



660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
# File 'lib/wice/helpers/wice_grid_view_helpers.rb', line 660

def pagination_info(grid, allow_showing_all_records)  #:nodoc:
  collection = grid.resultset

  if grid.all_record_mode?

    collection_total_entries = collection.length

    first = 1
    last = collection.size

    total_pages = 1

    class << collection
      def current_page
        1
      end

      def total_pages
        1
      end
    end

  else
    collection_total_entries = collection.total_count

    first = collection.offset_value + 1
    last  = collection.last_page? ? collection.total_count : collection.offset_value + collection.limit_value

    total_pages = collection.total_pages
  end

  parameters = grid.get_state_as_parameter_value_pairs

  if total_pages < 2 && collection.length == 0
    '0'
  else
    parameters << ["#{grid.name}[pp]", collection_total_entries]

    show_all_records_link = allow_showing_all_records && collection_total_entries > collection.length

    if show_all_records_link && limit = Wice::ConfigurationProvider.value_for(:SHOW_ALL_ALLOWED_UP_TO, strict: false)
      show_all_records_link = limit > collection_total_entries
    end

    "#{first}-#{last} / #{collection_total_entries} " +
      if show_all_records_link
        res, _js = show_all_link(collection_total_entries, parameters, grid.name)
        res
      else
        ''
      end
  end +
    if grid.all_record_mode?
      back_to_pagination_link(parameters, grid.name)
    else
      ''
    end
end

#pagination_panel_content(grid, extra_request_parameters, allow_showing_all_records, pagination_theme) ⇒ Object

:nodoc:



615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
# File 'lib/wice/helpers/wice_grid_view_helpers.rb', line 615

def pagination_panel_content(grid, extra_request_parameters, allow_showing_all_records, pagination_theme) #:nodoc:
  extra_request_parameters = extra_request_parameters.clone
  if grid.saved_query
    extra_request_parameters["#{grid.name}[q]"] = grid.saved_query.id
  end

  html = pagination_info(grid, allow_showing_all_records)

  paginate(grid.resultset,
           theme:        pagination_theme,
           param_name:   "#{grid.name}[page]",
           params:       extra_request_parameters,
           inner_window: 4,
           outer_window: 2
  ) +
    (' <div class="pagination_status">' + html + '</div>').html_safe
end

#render_grid(grid) ⇒ Object

Used after define_grid to actually output the grid HTML code. Usually used with detached filters: first define_grid, then grid_filters, and then render_grid



136
137
138
139
140
141
142
143
144
# File 'lib/wice/helpers/wice_grid_view_helpers.rb', line 136

def render_grid(grid)
  if grid.output_buffer
    grid.output_buffer
  elsif grid.csv_tempfile
    grid.csv_tempfile.path
  else
    raise WiceGridException.new("Attempt to use 'render_grid' without 'define_grid' before.")
  end
end

#reset_submit_buttons(options, grid, _rendering) ⇒ Object

:nodoc:



546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
# File 'lib/wice/helpers/wice_grid_view_helpers.rb', line 546

def reset_submit_buttons(options, grid, _rendering)  #:nodoc:
  if options[:hide_submit_button]
    ''
  else
    (:div, (:i, '', class: 'fa fa-filter'),
                title: NlMessage['filter_tooltip'],
                id:    grid.name + '_submit_grid_icon',
                class: 'submit clickable'
    )
  end.html_safe + ' ' +
    if options[:hide_reset_button]
      ''
    else

      (:div, (:i, '', class: 'fa fa-table'),
                  title: NlMessage['reset_filter_tooltip'],
                  id:    grid.name + '_reset_grid_icon',
                  class: 'reset clickable'
      )
    end.html_safe
end

#saved_queries_list(grid_name, saved_query = nil, extra_parameters = {}, confirm = 1) ⇒ Object

:nodoc:



51
52
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
85
86
87
88
89
90
91
92
# File 'lib/wice/helpers/wice_grid_serialized_queries_view_helpers.rb', line 51

def saved_queries_list(grid_name, saved_query = nil, extra_parameters = {}, confirm = 1)  #:nodoc:
  link_title            = NlMessage['saved_query_link_title']
  deletion_confirmation = confirm ? NlMessage['saved_query_deletion_confirmation'] : nil
  deletion_link_title   = NlMessage['saved_query_deletion_link_title']

  query_store_model = ::Wice.get_query_store_model
  currently_loaded_query_id = saved_query ? saved_query.id : nil
  # with = extra_parameters.nil? ? nil : "'"  + {extra: extra_parameters}.to_query + "'"

  %(<ul id="#{grid_name}_query_list" class="query-list"> ) +
    query_store_model.list(grid_name, controller).collect do |sq|
      link_opts = {
        class: 'wice-grid-query-load-link',
        title: "#{link_title} #{sq.name}",
        'data-query-id' => sq.id,
        'data-grid-name' => grid_name
      }
      link_opts[:class] += ' current' if saved_query == sq
      '<li>' + link_to(
        (:i, '', class: 'fa-regular fa-trash-can delete-icon'),
        delete_serialized_query_path(
          grid_name: grid_name,
          id:      sq.id,
          current: currently_loaded_query_id,
          extra:   extra_parameters,
          confirm: confirm
        ),
        class: 'wice-grid-delete-query',
        'data-wg-confirm' => deletion_confirmation,
        'data-grid-name'  => grid_name,
        title: "#{deletion_link_title} #{sq.name}"
      ) + ' &nbsp; ' +
        link_to(h(sq.name), '#', link_opts) +
        if sq.respond_to? :description
          desc = sq.description
          desc.blank? ? '' : " <i>#{desc}</i>"
        else
          ''
        end +
        '</li>'
    end.join('') + '</ul>'
end

#saved_queries_panel(grid, opts = {}) ⇒ Object

View helper to render the list of saved queries and the form to create a new query. Parameters:

  • :extra_parameters - a hash of additional parameters to use when creating a new query object.

  • :confirm - A boolean value which turns on or off the JS confirm dialogs when deleting saved queries.

Read section “Adding Application Specific Logic to Saving/Restoring Queries” in README for more details.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/wice/helpers/wice_grid_serialized_queries_view_helpers.rb', line 8

def saved_queries_panel(grid, opts = {})
  unless grid.is_a? WiceGrid
    raise WiceGridArgumentError.new('saved_queries_panel: the parameter must be a WiceGrid instance.')
  end

  options = { extra_parameters: {}, confirm: 1 }
  opts.assert_valid_keys(options.keys)
  options.merge!(opts)

  grid_name = grid.name
  input_field_name = "#{grid_name}_saved_query_name"
  base_path_to_query_controller = create_serialized_query_url(grid_name: grid_name, confirm: options[:confirm])

  parameters = grid.get_state_as_parameter_value_pairs

  options[:extra_parameters].each do |k, v|
    parameters << [CGI.unescape({ extra: { k => '' } }.to_query.sub(/=$/, '')), v.to_s]
  end
  parameters << ['authenticity_token', form_authenticity_token]
  notification_messages_id = "#{grid_name}_notification_messages"
  (%( <div class="wice-grid-query-panel well"><h3>#{NlMessage['saved_query_panel_title']}</h3>) +
    saved_queries_list(grid_name, grid.saved_query, options[:extra_parameters], options[:confirm]) +
    %(<div id="#{notification_messages_id}" ></div>) +
    if block_given?
      view, ids = yield
      view
    else
      ''
    end +

    '<div class="form-horizontal"><div class="wg-saved-query-input-controls input-append">' + text_field_tag(input_field_name,  '',  onkeydown: '', class: 'wice-grid-save-query-field form-control ') +
    button_tag(
      NlMessage['save_query_button_label'],
      class: 'wice-grid-save-query-button btn btn-primary',
      'data-grid-name'                     => grid_name,
      'data-base-path-to-query-controller' => base_path_to_query_controller,
      'data-parameters'                    => parameters.to_json,
      'data-ids'                           => ids.to_json
    ) +
    '</div></div></div>'
    ).html_safe
end

#scaffolded_grid(grid_obj, opts = {}) ⇒ Object

secret but stupid weapon - takes an ActiveRecord and using reflection tries to build all the column clauses by itself. WiceGrid is not a scaffolding solution, I hate scaffolding and how certain idiots associate scaffolding with Rails, so I do not document this method to avoid contributing to this misunderstanding.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/wice/helpers/wice_grid_misc_view_helpers.rb', line 51

def scaffolded_grid(grid_obj, opts = {}) #:nodoc:
  unless grid_obj.is_a? WiceGrid
    raise WiceGridArgumentError.new('scaffolded_grid: the parameter must be a WiceGrid instance.')
  end

  # debug grid.klass.column_names
  columns = grid_obj.klass.column_names
  if opts[:reject_attributes].is_a? Proc
    columns = columns.reject { |c| opts[:reject_attributes].call(c) }
    opts.delete(:reject_attributes)
  else
    columns = columns.reject { |c| c =~ opts[:reject_attributes] }
    opts.delete(:reject_attributes)
  end
  grid(grid_obj, opts) do |g|
    columns.each do |column_name|
      g.column name: column_name.humanize, attribute: column_name  do |ar|
        ar.send(column_name)
      end
    end
  end
end

:nodoc:



633
634
635
636
637
638
639
640
641
642
643
644
645
646
# File 'lib/wice/helpers/wice_grid_view_helpers.rb', line 633

def show_all_link(collection_total_entries, parameters, _grid_name) #:nodoc:
  message = NlMessage['all_queries_warning']
  confirmation = collection_total_entries > Defaults::START_SHOWING_WARNING_FROM ? message : nil

  html = (:a, NlMessage['show_all_records_label'],
                     href: '#',
                     title: NlMessage['show_all_records_tooltip'],
                     class: 'wg-show-all-link',
                     'data-grid-state'     => parameters.to_json,
                     'data-confim-message' => confirmation
  )

  [html, '']
end