wice_grid_ms 3.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (92) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +19 -0
  3. data/.inch.yml +3 -0
  4. data/.rspec +3 -0
  5. data/.rubocop.yml +181 -0
  6. data/.travis.yml +22 -0
  7. data/CHANGELOG.md +714 -0
  8. data/Gemfile +4 -0
  9. data/MIT-LICENSE +20 -0
  10. data/README.md +1518 -0
  11. data/Rakefile +59 -0
  12. data/SAVED_QUERIES_HOWTO.md +113 -0
  13. data/TODO.md +16 -0
  14. data/app/views/kaminari/wice_grid/_gap.html.erb +1 -0
  15. data/app/views/kaminari/wice_grid/_next_page.html.erb +1 -0
  16. data/app/views/kaminari/wice_grid/_page.html.erb +1 -0
  17. data/app/views/kaminari/wice_grid/_paginator.html.erb +19 -0
  18. data/app/views/kaminari/wice_grid/_prev_page.html.erb +1 -0
  19. data/config/locales/cz.yml +45 -0
  20. data/config/locales/de.yml +47 -0
  21. data/config/locales/en.yml +47 -0
  22. data/config/locales/es.yml +47 -0
  23. data/config/locales/fr.yml +45 -0
  24. data/config/locales/is.yml +46 -0
  25. data/config/locales/it.yml +38 -0
  26. data/config/locales/ja.yml +47 -0
  27. data/config/locales/nl.yml +45 -0
  28. data/config/locales/pt-BR.yml +36 -0
  29. data/config/locales/pt.yml +45 -0
  30. data/config/locales/ru.yml +45 -0
  31. data/config/locales/sk.yml +45 -0
  32. data/config/locales/uk.yml +45 -0
  33. data/config/locales/zh.yml +45 -0
  34. data/lib/generators/wice_grid/add_migration_for_serialized_queries_generator.rb +20 -0
  35. data/lib/generators/wice_grid/install_generator.rb +14 -0
  36. data/lib/generators/wice_grid/templates/create_wice_grid_serialized_queries.rb +14 -0
  37. data/lib/generators/wice_grid/templates/wice_grid_config.rb +192 -0
  38. data/lib/wice/active_record_column_wrapper.rb +123 -0
  39. data/lib/wice/columns.rb +276 -0
  40. data/lib/wice/columns/column_action.rb +52 -0
  41. data/lib/wice/columns/column_boolean.rb +40 -0
  42. data/lib/wice/columns/column_bootstrap_datepicker.rb +48 -0
  43. data/lib/wice/columns/column_custom_dropdown.rb +115 -0
  44. data/lib/wice/columns/column_float.rb +9 -0
  45. data/lib/wice/columns/column_html5_datepicker.rb +31 -0
  46. data/lib/wice/columns/column_integer.rb +78 -0
  47. data/lib/wice/columns/column_jquery_datepicker.rb +49 -0
  48. data/lib/wice/columns/column_processor_index.rb +23 -0
  49. data/lib/wice/columns/column_rails_date_helper.rb +41 -0
  50. data/lib/wice/columns/column_rails_datetime_helper.rb +40 -0
  51. data/lib/wice/columns/column_range.rb +72 -0
  52. data/lib/wice/columns/column_string.rb +92 -0
  53. data/lib/wice/columns/common_date_datetime_mixin.rb +20 -0
  54. data/lib/wice/columns/common_js_date_datetime_conditions_generator_mixin.rb +42 -0
  55. data/lib/wice/columns/common_js_date_datetime_mixin.rb +15 -0
  56. data/lib/wice/columns/common_rails_date_datetime_conditions_generator_mixin.rb +26 -0
  57. data/lib/wice/columns/common_standard_helper_date_datetime_mixin.rb +22 -0
  58. data/lib/wice/grid_output_buffer.rb +49 -0
  59. data/lib/wice/grid_renderer.rb +609 -0
  60. data/lib/wice/helpers/bs_calendar_helpers.rb +66 -0
  61. data/lib/wice/helpers/js_calendar_helpers.rb +83 -0
  62. data/lib/wice/helpers/wice_grid_misc_view_helpers.rb +75 -0
  63. data/lib/wice/helpers/wice_grid_serialized_queries_view_helpers.rb +95 -0
  64. data/lib/wice/helpers/wice_grid_view_helpers.rb +718 -0
  65. data/lib/wice/kaminari_monkey_patching.rb +14 -0
  66. data/lib/wice/table_column_matrix.rb +65 -0
  67. data/lib/wice/wice_grid_controller.rb +223 -0
  68. data/lib/wice/wice_grid_core_ext.rb +142 -0
  69. data/lib/wice/wice_grid_misc.rb +209 -0
  70. data/lib/wice/wice_grid_serialized_queries_controller.rb +87 -0
  71. data/lib/wice/wice_grid_serialized_query.rb +14 -0
  72. data/lib/wice/wice_grid_spreadsheet.rb +20 -0
  73. data/lib/wice_grid.rb +676 -0
  74. data/release_notes/RELEASE_NOTES_3.2.pre1.rdoc +81 -0
  75. data/release_notes/RELEASE_NOTES_3.2.pre2.rdoc +6 -0
  76. data/release_notes/RELEASE_NOTES_3.3.0.rdoc +21 -0
  77. data/spec/schema.rb +9 -0
  78. data/spec/spec_helper.rb +75 -0
  79. data/spec/support/active_record.rb +11 -0
  80. data/spec/support/wice_grid_test_config.rb +175 -0
  81. data/spec/wice/grid_output_buffer_spec.rb +41 -0
  82. data/spec/wice/table_column_matrix_spec.rb +38 -0
  83. data/spec/wice/wice_grid_misc_spec.rb +159 -0
  84. data/spec/wice/wice_grid_spreadsheet_spec.rb +14 -0
  85. data/test/readme.txt +1 -0
  86. data/vendor/assets/javascripts/wice_grid.js +3 -0
  87. data/vendor/assets/javascripts/wice_grid_init.js.coffee +339 -0
  88. data/vendor/assets/javascripts/wice_grid_processor.js.coffee +133 -0
  89. data/vendor/assets/javascripts/wice_grid_saved_queries_init.js.coffee +103 -0
  90. data/vendor/assets/stylesheets/wice_grid.scss +81 -0
  91. data/wice_grid.gemspec +36 -0
  92. metadata +335 -0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ # encoding: utf-8
2
+ source 'https://rubygems.org'
3
+
4
+ gemspec
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008-2014 Yuri Leikind
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,1518 @@
1
+ [![Version](http://img.shields.io/gem/v/wice_grid.svg)](https://rubygems.org/gems/wice_grid)
2
+ [![Build](https://travis-ci.org/leikind/wice_grid.svg)](https://travis-ci.org/leikind/wice_grid)
3
+ [![Inline docs](http://inch-ci.org/github/leikind/wice_grid.svg?branch=rails3)](http://inch-ci.org/github/leikind/wice_grid/branch/rails3)
4
+ [![License](http://img.shields.io/badge/license-MIT-yellowgreen.svg)](#license)
5
+
6
+ <!-- let's disable for a while ;-)
7
+ [![Coverage Status](https://coveralls.io/repos/leikind/wice_grid/badge.svg?branch=development&service=github)](https://coveralls.io/github/leikind/wice_grid?branch=development)
8
+ -->
9
+
10
+ THE PROJECT IS LOOKING FOR CONTRIBUTORS. THE AUTHOR WILL NOT WORK ON IT ANYMORE!
11
+
12
+ * Yuri Leikind, yuri.leikind at gmail dot com
13
+ * Version 3.6.0.pre3; [What's New In 3.6.0](https://github.com/leikind/wice_grid/wiki/What's-New-In-3.6.0)
14
+ * This tutorial is accompanied by a sample application with WiceGrid examples which you can browse online:
15
+ http://wicegrid.herokuapp.com, or just view the code: https://github.com/leikind/wice_grid_testbed.
16
+
17
+
18
+
19
+ # WiceGrid
20
+
21
+ - [Intro](#intro)
22
+ - [Requirements and Rails versions](#requirements-and-rails-versions)
23
+ - [Installation](#installation)
24
+ - [Basics](#basics)
25
+ - [Rendering filter panel](#rendering-filter-panel)
26
+ - [Initial Ordering](#initial-ordering)
27
+ - [Records Per Page](#records-per-page)
28
+ - [Conditions](#conditions)
29
+ - [Queries with join tables](#queries-with-join-tables)
30
+ - [Joined associations referring to the same table](#joined-associations-referring-to-the-same-table)
31
+ - [More than one grid on a page](#more-than-one_grid-on-a-page)
32
+ - [Custom Ordering](#custom-ordering)
33
+ - [Filters](#filters)
34
+ - [Custom dropdown filters](#custom-dropdown-filters)
35
+ - [Numeric Filters](#numeric-filters)
36
+ - [Date and DateTime Filters](#date-and-datetime-filters)
37
+ - [Detached Filters](#detached-filters)
38
+ - [Defaults](#defaults)
39
+ - [Bug reports](#bug-reports)
40
+
41
+
42
+ ## Intro
43
+
44
+ WiceGrid is a Rails grid plugin.
45
+
46
+ One of the goals of this plugin was to allow the programmer to define the contents of the cell on their
47
+ own, just like one does when rendering a collection via a simple table (and this is what differentiates
48
+ WiceGrid from various scaffolding solutions), but automate implementation of filters, ordering,
49
+ paginations, CSV export, and so on. Ruby blocks provide an elegant means for this.
50
+
51
+
52
+ WiceGrid builds the call to the ActiveRecord layer for you and creates a table view with the results
53
+ of the call including:
54
+
55
+ * Pagination
56
+ * Sortable columns
57
+ * Filtering by multiple columns
58
+ * Export to CSV
59
+ * Saved queries
60
+
61
+ Filters are added automatically according to the type of the underlying DB column. Filtering by more
62
+ than one column at the same time is possible. More than one such grid can appear on a page, and
63
+ manipulations with one grid do not have any impact on others.
64
+
65
+ WiceGrid does not take a collection as an input, it works directly with ActiveRecord.
66
+
67
+ WiceGrid does not use XHR calls to reload itself, instead simple GET requests are used for this,
68
+ nevertheless, all other page parameters are respected and preserved. WiceGrid works well with Turbolinks.
69
+
70
+ WiceGrid views do not contain forms so you can include it in your own forms.
71
+
72
+ WiceGrid is known to work with MySQL, Postgres, and Oracle.
73
+
74
+
75
+ ## Requirements and Rails versions
76
+
77
+ For rails 2 use version 0.6 in [the master branch](https://github.com/leikind/wice_grid/tree/master).
78
+ That branch is hardly supported.
79
+
80
+ The main supported branch is `rails3`. Latest Rails 3.2.x and Rails 4.x.x are supported in this branch.
81
+
82
+ If you need to use the plugin in with Rails 3.0.x and 3.1.x versions, please use WiceGrid version 3.0.4.
83
+
84
+ WiceGrid relies on jQuery.
85
+
86
+ If you need a JS Datepicker, WiceGrid supports jQuery Datepicker or
87
+ [Bootstrap Datepicker](https://github.com/Nerian/bootstrap-datepicker-rails), so you might need one of
88
+ those. See section Installation for details on how to use datepickers.
89
+
90
+ WARNING: Since 3.2.pre2 WiceGrid is not compatible with `will_paginate` because internally it uses
91
+ `kaminari` for pagination, and `kaminari` is not compatible with `will_paginate`!
92
+
93
+
94
+ ## Installation
95
+
96
+ Add the following to your Gemfile:
97
+
98
+ ```ruby
99
+ gem "wice_grid", '3.6.0.pre4'
100
+ gem 'font-awesome-sass', '~> 4.3'
101
+ ```
102
+
103
+ and run the `bundle` command. `font-awesome-sass` is not a dependency of WiceGrid in case you decide to style WiceGrid icons differently.
104
+
105
+ Run the generator:
106
+
107
+ ```
108
+ rails g wice_grid:install
109
+ ```
110
+
111
+ This adds the following file:
112
+ * config/initializers/wice_grid_config.rb
113
+
114
+
115
+ Require WiceGrid javascript in your js index file:
116
+
117
+ ```
118
+ //= require wice_grid
119
+ ```
120
+
121
+ Make sure jQuery is loaded. If the application uses Date and DateTime filters, you have to install
122
+ jQuery Datepicker by yourself. You can also use
123
+ [Bootstrap Datepicker](https://github.com/Nerian/bootstrap-datepicker-rails).
124
+
125
+ Here is an example of `application.js` if jquery.ui.datepicker is used:
126
+
127
+ ```
128
+ //= require jquery
129
+ //= require jquery_ujs
130
+ //= require jquery-ui
131
+ //= require wice_grid
132
+ //= require jquery.ui.datepicker
133
+ //= require_tree .
134
+ ```
135
+
136
+ Here is `application.js` if [Bootstrap Datepicker](https://github.com/Nerian/bootstrap-datepicker-rails) is used:
137
+
138
+ ```
139
+ //= require jquery
140
+ //= require jquery_ujs
141
+ //= require jquery-ui
142
+ //= require wice_grid
143
+ //= require bootstrap-datepicker
144
+ //= require_tree .
145
+ ```
146
+
147
+ Require WiceGrid and [Font Awesome](http://fortawesome.github.io/Font-Awesome/) CSS in your `application.scss`:
148
+
149
+ ```
150
+ @import "wice_grid";
151
+ @import "font-awesome-sprockets";
152
+ @import "font-awesome";
153
+ ```
154
+
155
+ This will provide very basic styles, not specifying exactly how the table should look like, but if
156
+ the application uses Twitter Bootstrap, the markup generated by WiceGrid will have correct classes and
157
+ will fit nicely.
158
+
159
+ WiceGrid uses icons from [Font Awesome](http://fortawesome.github.io/Font-Awesome/).
160
+
161
+ Should you decide to write you own styles for WiceGrid, you can just remove these imports and write your own styles.
162
+
163
+
164
+ ## Basics
165
+
166
+ The simplest example of a WiceGrid for one simple DB table is the following:
167
+
168
+ Controller:
169
+
170
+ ```ruby
171
+ @tasks_grid = initialize_grid(Task)
172
+ ```
173
+
174
+ It is also possible to use an `ActiveRecord::Relation` instance as the first argument:
175
+
176
+ ```ruby
177
+ @tasks_grid = initialize_grid(Task.where(active: true))
178
+ ```
179
+
180
+ View:
181
+
182
+ ```erb
183
+ <%= grid(@tasks_grid) do |g|
184
+
185
+ g.column do |task|
186
+ task.id
187
+ end
188
+
189
+ g.column do |task|
190
+ task.title
191
+ end
192
+
193
+ g.column do |task|
194
+ task.description
195
+ end
196
+
197
+ g.column do |task|
198
+ task.archived? ? 'Yes' : 'No'
199
+ end
200
+
201
+ g.column do |task|
202
+ link_to('Edit', edit_task_path(task))
203
+ end
204
+ end -%>
205
+ ```
206
+
207
+ Code `g.column do |task| ... end`
208
+ defines everything related to a column in the resulting view table including column names, sorting,
209
+ filtering, the content of the column cells, etc. The return value of the block is the table cell content.
210
+
211
+ Column names are defined with parameter `:name`:
212
+
213
+ ```erb
214
+ <%= grid(@tasks_grid) do |g|
215
+
216
+ g.column name: 'ID' do |task|
217
+ task.id
218
+ end
219
+
220
+ g.column name: 'Title' do |task|
221
+ task.title
222
+ end
223
+
224
+ g.column name: 'Description' do |task|
225
+ task.description
226
+ end
227
+
228
+ g.column name: 'Archived' do |task|
229
+ task.archived? ? 'Yes' : 'No'
230
+ end
231
+
232
+ g.column do |task|
233
+ link_to('Edit', edit_task_path(task))
234
+ end
235
+ end -%>
236
+ ```
237
+
238
+ To add filtering and ordering, declare to which column in the underlying database table(s) the view
239
+ column corresponds using parameter `:attribute`:
240
+
241
+ ```erb
242
+ <%= grid(@tasks_grid) do |g|
243
+
244
+ g.column name: 'ID', attribute: 'id' do |task|
245
+ task.id
246
+ end
247
+
248
+ g.column name: 'Title', attribute: 'title' do |task|
249
+ task.title
250
+ end
251
+
252
+ g.column name: 'Description', attribute: 'description' do |task|
253
+ task.description
254
+ end
255
+
256
+ g.column name: 'Archived', attribute: 'archived' do |task|
257
+ task.archived? ? 'Yes' : 'No'
258
+ end
259
+
260
+ g.column do |task|
261
+ link_to('Edit', edit_task_path(task))
262
+ end
263
+ end -%>
264
+ ```
265
+
266
+ This will add sorting links and filters for columns `Username` and `Active`. The plugin automatically
267
+ creates filters according to the type of the database column. In the above example a text field will be
268
+ created for column Title (title is a string), for column `Archived` a dropdown filter will be created
269
+ with options 'Yes', 'No', and '--', and for the integer ID two short text fields are added which can
270
+ contain the numeric range (more than, less than).
271
+
272
+ It is important to remember that `:attribute` is the name of the database column, not a model attribute.
273
+ Of course, all database columns have corresponding model attributes, but not all model attributes map to
274
+ columns in the same table with the same name.
275
+
276
+ Read more about available filters in the documentation for the column method.
277
+
278
+ Read the section about custom dropdown filters for more advanced filters.
279
+
280
+ For columns like
281
+
282
+ ```ruby
283
+ g.column name: 'Title', attribute: 'title' do |task|
284
+ task.title
285
+ end
286
+ ```
287
+
288
+ where the block contains just a call to the same attribute declared by :attribute, the block can be
289
+ omitted:
290
+
291
+ ```erb
292
+ <%= grid(@tasks_grid) do |g|
293
+
294
+ g.column name: 'ID', attribute: 'id'
295
+
296
+ g.column name: 'Title', attribute: 'title'
297
+
298
+ g.column name: 'Description', attribute: 'description'
299
+
300
+ g.column name: 'Archived', attribute: 'archived' do |task|
301
+ task.archived? ? 'Yes' : 'No'
302
+ end
303
+
304
+ g.column do |task|
305
+ link_to('Edit', edit_task_path(task))
306
+ end
307
+ end -%>
308
+ ```
309
+
310
+ In this case `name` will be used as the method name to send to the ActiveRecord instance.
311
+
312
+ If only ordering is needed, and no filter, we can turn off filters using `:filter` :
313
+
314
+ ```ruby
315
+ g.column name: 'ID', attribute: 'id', filter: false
316
+ ```
317
+
318
+ If no ordering links are needed, use `ordering: false`:
319
+
320
+ ```ruby
321
+ g.column name: 'Added', attribute: 'created_at', ordering: false
322
+ ```
323
+
324
+ It is important to understand that it is up to the developer to make sure that the value returned by a
325
+ column block (the content of a cell) corresponds to the underlying database column specified by
326
+ `:attribute` (and `:assoc` discussed below).
327
+
328
+
329
+ ### Rendering filter panel
330
+
331
+ The filter panel can be shown and hidden clicking the icon with binoculars.
332
+
333
+ The way the filter panel is shown after the page is loaded is controlled via parameter
334
+ `:show_filters` of the `grid` helper.
335
+ Possible values are:
336
+
337
+ * `:when_filtered` - the filter is shown when the current table is the result of filtering
338
+ * `:always` - always show the filter
339
+ * `:no` - never show the filter
340
+
341
+ Example:
342
+
343
+ ```erb
344
+ <%= grid(@tasks_grid, show_filters: :always) do |g|
345
+ ......
346
+ end -%>
347
+ ```
348
+
349
+ Filter related icons (filter icon, reset icon, show/hide icon) are placed in the header of the last
350
+ column if it doesn't have any filter or a column name, otherwise an additional table column is added.
351
+ To always place the icons in the additional column, set
352
+ `Wice::Defaults::REUSE_LAST_COLUMN_FOR_FILTER_ICONS` to `false` in the configuration file.
353
+
354
+
355
+ ### Initial ordering
356
+
357
+ Initializing the grid we can also define the column by which the record will be ordered <em>on the first
358
+ rendering of the grid</em>, when the user has not set their ordering setting by clicking the column label,
359
+ and the order direction:
360
+
361
+ ```ruby
362
+ @tasks_grid = initialize_grid(Task,
363
+ order: 'tasks.title',
364
+ order_direction: 'desc'
365
+ )
366
+ ```
367
+
368
+ ### Records per page
369
+
370
+ The number of rows per page is set with `:per_page`:
371
+
372
+ ```ruby
373
+ @tasks_grid = initialize_grid(Task, per_page: 40)
374
+ ```
375
+
376
+ ### Conditions
377
+
378
+ The `initialize_grid` method supports a `:conditions` parameter which is passed on to the underlying
379
+ ActiveRecord, so it can be in any format processable by ActiveRecord:
380
+
381
+ ```ruby
382
+ @tasks_grid = initialize_grid(Task,
383
+ conditions: ["archived = false and estimated_time > ?", 100]
384
+ )
385
+
386
+ @tasks_grid = initialize_grid(Task,
387
+ include: :project,
388
+ conditions: {archived: false, project: {active: true}}
389
+ )
390
+ ```
391
+
392
+ A good example is substituting a common pattern like
393
+
394
+ ```ruby
395
+ @user_groups = @portal_application.user_groups
396
+ ```
397
+
398
+ with WiceGrid code:
399
+
400
+ ```ruby
401
+ @user_groups_grid = initialize_grid(
402
+ UserGroup,
403
+ conditions: ['portal_application_id = ?', @portal_application]
404
+ )
405
+ ```
406
+
407
+ Alternatively, instead of a Class object as the first parameter, you can use ActiveRecord::Relation:
408
+
409
+ ```ruby
410
+ @tasks_grid = initialize_grid(
411
+ Task.where(archived: false, projects: {active: true}).joins(:project)
412
+ )
413
+ ```
414
+
415
+ Please note that though all queries inside of WiceGrid are run without the default scope, if you use an
416
+ ActiveRecord::Relation instance to initialize grid, it will already include the default scope. Thus you
417
+ might consider using `unscoped`:
418
+
419
+ ```ruby
420
+ @tasks_grid = initialize_grid(
421
+ Task.unscoped.where(archived: false, projects: {active: true}).joins(:project)
422
+ )
423
+ ```
424
+
425
+ ### Queries with join tables
426
+
427
+ To join other tables, use `:include`:
428
+
429
+ ```ruby
430
+ @products_grid = initialize_grid(Product,
431
+ include: :category,
432
+ order: 'products.name',
433
+ per_page: 20
434
+ )
435
+ ```
436
+
437
+ The value of `:include` can be an array of association names:
438
+
439
+ ```ruby
440
+ include: [:category, :users, :status]
441
+ ```
442
+
443
+ If you need to join tables to joined tables, use hashes:
444
+
445
+
446
+ ```ruby
447
+ include: [:category, {users: :group}, :status]
448
+ ```
449
+
450
+
451
+ Note that if we want to order initially by a column from a joined table we have to specify the table and
452
+ the column name with the sql dot notation, that is, `products.name`.
453
+
454
+ To show columns of joined tables in the view table, specify the corresponding association with `:assoc`:
455
+
456
+ ```erb
457
+ <%= grid(@products_grid) do |g|
458
+ g.column name: 'Product Name', attribute: 'name' do |product| # primary table
459
+ link_to(product.name, product_path(product))
460
+ end
461
+
462
+ g.column name: 'Category', attribute: 'name', assoc: :category do |product| # joined table
463
+ product.category.name
464
+ end
465
+ %>
466
+ ```
467
+
468
+ Please note that the blockless definition of the column can also be used with joined tables:
469
+
470
+ ```
471
+ g.column name: 'Category', attribute: 'name', assoc: :category
472
+
473
+ ```
474
+
475
+ If an association is mentioned in the column definition, it can be omitted from `:include` in `initialize_grid`.
476
+ Thus, the above example can be rewritten without `:category` in `:include`:
477
+
478
+
479
+ ```ruby
480
+ @products_grid = initialize_grid(Product,
481
+ order: 'products.name',
482
+ per_page: 20
483
+ )
484
+ ```
485
+
486
+ ```erb
487
+ <%= grid(@products_grid) do |g|
488
+ g.column name: 'Product Name', attribute: 'name' do |product| # primary table
489
+ link_to(product.name, product_path(product))
490
+ end
491
+
492
+ g.column name: 'Category', attribute: 'name', assoc: :category
493
+
494
+ %>
495
+ ```
496
+
497
+ ### Joined associations referring to the same table
498
+
499
+ In case there are two joined associations both referring to the same table, ActiveRecord constructs a query
500
+ where the second join provides an alias for the joined table. To enable WiceGrid to order and filter by
501
+ columns belonging to different associations but originating from the same table, set `:table_alias`
502
+ to this alias:
503
+
504
+ Model:
505
+
506
+ ```ruby
507
+ class Project < ActiveRecord::Base
508
+ belongs_to :customer, class_name: 'Company'
509
+ belongs_to :supplier, class_name: 'Company'
510
+ end
511
+ ```
512
+
513
+ Controller:
514
+
515
+ ```ruby
516
+ @projects_grid = initialize_grid(Project)
517
+ ```
518
+
519
+ View:
520
+
521
+ ```erb
522
+ <%= grid(@projects_grid, show_filters: :always) do |g|
523
+
524
+ g.column name: 'Project Name', attribute: 'name'
525
+
526
+ g.column name: 'Customer company', assoc: :customer, attribute: 'name'
527
+
528
+ g.column name: 'Supplier company', assoc: :supplier, attribute: 'name', table_alias: 'suppliers_projects'
529
+
530
+ end -%>
531
+ ```
532
+
533
+ ### More than one grid on a page
534
+
535
+ It is possible to use more that one grid on a page, each with its own state. To do so, you must specify the
536
+ name of the grid in `initialize_grid` using parameter `:name`.
537
+
538
+ The name serves as the base name for HTTP parameters, DOM IDs, etc, so it is important that all grids on a
539
+ page have different names. The default name is 'grid'.
540
+
541
+ The name can only contain alphanumeric characters.
542
+
543
+ ```ruby
544
+ @projects_grid = initialize_grid(Project, name: 'g1')
545
+ @tasks_grid = initialize_grid(Task, name: 'g2')
546
+ ```
547
+
548
+ ### Custom Ordering
549
+
550
+ It is possible to change the way results are ordered injecting a chunk of SQL code, for example, use
551
+ `ORDER BY INET_ATON(ip_address)` instead of `ORDER BY ip_address`.
552
+
553
+ To do so, provide parameter `:custom_order` in the initialization of the grid with a hash where
554
+ keys are fully qualified names of database columns, and values the required chunks of SQL to use in the
555
+ `ORDER BY` clause.
556
+
557
+ For example:
558
+
559
+ ```ruby
560
+ @hosts_grid = initialize_grid(Host,
561
+ custom_order: {
562
+ 'hosts.ip_address' => 'INET_ATON(hosts.ip_address)'
563
+ })
564
+ ```
565
+
566
+ It is possible to use the '?' character instead of the name of the column in the hash value:
567
+
568
+ ```ruby
569
+ @hosts_grid = initialize_grid(Host,
570
+ custom_order: {
571
+ 'hosts.ip_address' => 'INET_ATON( ? )'
572
+ })
573
+ ```
574
+
575
+ Values can also be Proc objects. The parameter supplied to such a Proc object is the name of the column:
576
+
577
+ ```ruby
578
+ @hosts_grid = initialize_grid(Host,
579
+ custom_order: {
580
+ 'hosts.ip_address' => lambda{|f| "INET_ATON( #{f} )"}
581
+ })
582
+ ```
583
+
584
+ ## Filters
585
+
586
+ Each column filter type is supported by a `column processor`. Each `column processor` is
587
+ responsible for
588
+
589
+ * generating HTML and supporting Javascript for the filter, input fields, dropdowns, javascript calendars, etc
590
+ * converting HTTP parameters from those input fields into ActiveRelation instances
591
+
592
+ By default column filters depend on the type of the underlying database column.
593
+
594
+ You can override these defaults in two ways:
595
+
596
+ * defining a custom filter with `:custom_filter`. Read more about it section "Custom dropdown filters".
597
+ * overriding the `column processor` type with `:filter_type`.
598
+
599
+ Which Column Processor is instantiated for which data types is defined in file
600
+ `lib/wice/columns/column_processor_index.rb`:
601
+
602
+ ```ruby
603
+ module Wice
604
+ module Columns
605
+ COLUMN_PROCESSOR_INDEX = ActiveSupport::OrderedHash[ #:nodoc:
606
+ :action, 'column_action', # Special processor for action column, columns with checkboxes
607
+ :text, 'column_string',
608
+ :string, 'column_string',
609
+ :rails_datetime_helper, 'column_rails_datetime_helper', # standard Rails datepicker helper
610
+ :rails_date_helper, 'column_rails_date_helper', # standard Rails date helper
611
+ :jquery_datepicker, 'column_jquery_datepicker',
612
+ :bootstrap_datepicker, 'column_bootstrap_datepicker',
613
+ :html5_datepicker, 'column_html5_datepicker', # not ready
614
+ :integer, 'column_integer',
615
+ :range, 'column_range',
616
+ :float, 'column_float',
617
+ :decimal, 'column_float',
618
+ :custom, 'column_custom_dropdown', # Special processor for custom filter columns
619
+ :boolean, 'column_boolean'
620
+ ]
621
+ end
622
+ end
623
+ ```
624
+
625
+ A good example for using `:filter_type` to change th default is numeric columns. By default
626
+ `'column_integer'` is instantiated for `integer` columns, and it renders one input field.
627
+ But it is also possible to use another Column Processor called `'column_range'` which renders two
628
+ input fields and searches for values in the given the range instead of searching for values which equal
629
+ the given search term.
630
+
631
+ It also possible to define and use your own column processors outside of the plugin, in you application.
632
+ Read more about this in section "Defining your own external filter processors".
633
+
634
+
635
+ ### Custom dropdown filters
636
+
637
+ It is possible to construct custom dropdown filters. A custom dropdown filter is essentially a dropdown
638
+ list.
639
+
640
+ Depending on the value of `column` parameter`:custom_filter` different modes are available:
641
+
642
+
643
+ #### Array of two-element arrays or a hash
644
+
645
+ An array of two-element arrays or a hash are semantically identical ways of creating a custom filter.
646
+
647
+ Every first item of the two-element array is used for the label of the select option while the second
648
+ element is the value of the select option. In case of a hash the keys become the labels of the generated
649
+ dropdown list, while the values will be values of options of the dropdown list:
650
+
651
+ ```ruby
652
+ g.column name: 'Status', attribute: 'status',
653
+ custom_filter: {'Development' => 'development', 'Testing' => 'testing', 'Production' => 'production'}
654
+
655
+ g.column name: 'Status', attribute: 'status',
656
+ custom_filter: [['Development', 'development'], ['Testing', 'testing'], ['Production', 'production']]
657
+ ```
658
+
659
+ It is also possible to submit a array of strings or numbers, in this case every item will be used both as
660
+ the value of the select option and as its label:
661
+
662
+ ```ruby
663
+ g.column name: 'Status', attribute: 'status', custom_filter: ['development', 'testing', 'production']
664
+ ```
665
+
666
+ #### :auto
667
+
668
+ `:auto` - a powerful option which populates the dropdown list with all unique values of the column
669
+ specified by `:attribute` and `:assoc`, if present.
670
+
671
+ ```ruby
672
+ g.column name: 'Status', attribute: 'status', custom_filter: :auto
673
+ ```
674
+
675
+ In the above example all statuses will appear in the dropdown even if they don't appear in the current
676
+ resultset.
677
+
678
+
679
+ #### Custom filters and associations (joined tables)
680
+
681
+ In most cases custom fields are needed for one-to-many and many-to-many associations.
682
+
683
+ To correctly build a filter condition foreign keys have to be used, not the actual values rendered in the
684
+ column.
685
+
686
+ For example, if there is a column:
687
+
688
+ ```ruby
689
+ g.column name: 'Project Name', attribute: 'name', assoc: :project do |task|
690
+ task.project.name if task.project
691
+ end
692
+ ```
693
+
694
+ adding `:custom_filter` like this:
695
+
696
+ ```ruby
697
+ g.column name: 'Project Name', attribute: 'name', assoc: :project,
698
+ custom_filter: Project.find(:all).map{|pr| [pr.name, pr.name]} do |task|
699
+ task.project.name if task.project
700
+ end
701
+ ```
702
+
703
+ is bad style and can fail, because the resulting condition will compare the name of the project,
704
+ `projects.name` to a string, and in some databases it is possible that different records
705
+ (projects in our example) have the same name.
706
+
707
+ To use filter with foreign keys, it is advised to change the declaration of the column from
708
+ `projects.name`, to `tasks.project_id`, and build the dropdown with foreign keys as values:
709
+
710
+ ```ruby
711
+ g.column name: 'Project Name', attribute: 'tasks.project_id',
712
+ custom_filter: Project.find(:all).map{|pr| [pr.id, pr.name]} do |task|
713
+ task.project.name if task.project
714
+ end
715
+ ```
716
+
717
+ However, this will break the ordering of the column - the column will be ordered by the integer foreign
718
+ key. To fix this, we can override the ordering using `:custom_order`:
719
+
720
+ ```ruby
721
+ @tasks_grid = initialize_grid(Task,
722
+ include: :project,
723
+ custom_order: {
724
+ 'tasks.project_id' => 'projects.name'
725
+ }
726
+ )
727
+ ```
728
+
729
+ #### Any other symbol (method name) or an array of symbols (method names)
730
+
731
+
732
+ For one symbol (different from `:auto`) the dropdown list is populated by all unique values returned
733
+ by the method with this name sent to <em>all</em> ActiveRecord objects throughout all pages.
734
+
735
+ The conditions set up by the user are ignored, that is, the records used are all those found on all pages
736
+ without any filters active.
737
+
738
+ For an array of symbols, the first method name is sent to the ActiveRecord object if it responds to this
739
+ method, the second method name is sent to the returned value unless it is `nil`, and so on. In other
740
+ words, a single symbol mode is the same as an array of symbols where the array contains just one element.
741
+
742
+ ```ruby
743
+ g.column name: 'Version', attribute: 'expected_version_id', custom_filter: [:expected_version, :to_option] do |task|
744
+ task.expected_version.name if task.expected_version
745
+ end
746
+ ```
747
+
748
+ There are two important differences from `:auto`:
749
+
750
+ 1. The method does not have to be a field in the result set, it is just some value computed in the method after the database call and ActiveRecord instantiation.
751
+ 2. Filtering by any option of such a custom filter will bring a non-empty list, unlike with `:auto`.
752
+
753
+
754
+ This mode has one major drawback - this mode requires an additional query without `offset` and `limit`
755
+ clauses to instantiate _all_ ActiveRecord objects, and performance-wise it brings all the advantages of
756
+ pagination to nothing. Thus, memory- and performance-wise this can be really bad for some queries and
757
+ tables and should be used with care.
758
+
759
+
760
+ If the final method returns a atomic value like a string or an integer, it is used for both the value and
761
+ the label of the select option element:
762
+
763
+ ```html
764
+ <option value="returned value">returned value</option>
765
+ ```
766
+
767
+ However, if the retuned value is a two element array, the first element is used for the option label and
768
+ the second - for the value.
769
+
770
+ Typically, a model method like the following:
771
+
772
+ ```ruby
773
+ def to_option
774
+ [name, id]
775
+ end
776
+ ```
777
+
778
+ together with
779
+
780
+ ```ruby
781
+ custom_filter: :to_option
782
+ ```
783
+
784
+ would do the trick:
785
+
786
+ ```html
787
+ <option value="id">name</option>
788
+ ```
789
+
790
+ Alternatively, a hash with the single key-value pair can be used, where the key will be used for the
791
+ label, and the key - for the value:
792
+
793
+ ```ruby
794
+ def to_option
795
+ {name => id}
796
+ end
797
+ ```
798
+
799
+ #### Special treatment of values 'null' and 'not null'
800
+
801
+ Values `null` and `not null` in a generated custom filter are treated specially, as SQL `null` statement
802
+ and not as strings. Value `null` is transformed into SQL condition `IS NULL`, and `not null` into
803
+ `IS NOT NULL`.
804
+
805
+ Thus, if in a filter defined by
806
+
807
+ ```ruby
808
+ custom_filter: {'No' => 'null', 'Yes' => 'not null', '1' => 1, '2' => '2', '3' => '3'}
809
+ ```
810
+
811
+ values '1', '2' and 'No' are selected (in a multi-select mode), this will result in the following SQL:
812
+
813
+ ```sql
814
+ ( table.field IN ( '1', '2' ) OR table.field IS NULL )
815
+ ```
816
+
817
+ #### Multiple selection
818
+
819
+ By default it is possible for any dropdown list to switch between single and multiple selection modes.
820
+ To only allow single selection use `:allow_multiple_selection`:
821
+
822
+ ```ruby
823
+ g.column name: 'Expected in version', attribute: 'expected_version_id',
824
+ custom_filter: [:expected_version, :to_option], allow_multiple_selection: false do |task|
825
+ ...
826
+ end
827
+ ```
828
+
829
+ ### Numeric Filters
830
+
831
+ Before version 3.2.1 the filter used for numeric columns was a range filter with two limits. Beginning
832
+ with version 3.2.1 the default is a direct comparison filter with one input field. The old range filter
833
+ can still be loaded using parameter `:filter_type` with value `:range`:
834
+
835
+ ```ruby
836
+ g.column filter_type: :range do |task|
837
+ ...
838
+ end
839
+ ```
840
+
841
+ ### Date and DateTime Filters
842
+
843
+ WiceGrid provides four filters for selecting dates and time:
844
+
845
+ * ```:jquery_datepicker``` - Jquery datepicker (works for datetime, too)
846
+ * ```:bootstrap_datepicker``` - Bootstrap datepicker (works for datetime, too)
847
+ * ```:rails_date_helper``` - standard Rails date helper
848
+ * ```:rails_datetime_helper``` - standard Rails datetime helper
849
+
850
+ Specify a date/datetime filter just like you specify any other filter:
851
+
852
+ ```
853
+ g.column name: 'Updated', attribute: 'updated_at', filter_type: :rails_datetime_helper do |task|
854
+ task.updated_at.to_s(:db)
855
+ end
856
+ ```
857
+
858
+ Default filters are defined in configuration constants Wice::Defaults::DEFAULT_FILTER_FOR_DATE and
859
+ Wice::Defaults::DEFAULT_FILTER_FOR_DATETIME.
860
+
861
+
862
+
863
+ #### jQuery UI DatePicker `(HELPER_STYLE = :calendar)`
864
+
865
+ By default WiceGrid uses jQuery UI datepicker[http://jqueryui.com/demos/datepicker/] for Date and DateTime
866
+ filters. Because this is part of the standard jQuery UI codebase, it is not bundled together with the
867
+ plugin, and it is the responsibility of the programmer to include all necessary assets including
868
+ localization files if the application is multilingual.
869
+
870
+ jQuery UI datepicker does not have any time related controls, and when dealing with DateTime filters, the
871
+ time value is ignored.
872
+
873
+ Constants `DATE_FORMAT` and `DATETIME_FORMAT` in the configuration file define the format of dates the
874
+ user will see, as well as the format of the string sent in a HTTP parameter. If you change the formats,
875
+ make sure that lamdbas defined in `DATETIME_PARSER` and `DATE_PARSER` return valid DateTime and Date
876
+ objects.
877
+
878
+ jQuery `datepicker` uses a different format flavor, therefore there is an additional constant
879
+ `DATE_FORMAT_JQUERY`. While `DATE_FORMAT_JQUERY` is fed to `datepicker`, `DATE_FORMAT` is still used
880
+ for presenting initial date values in filters, so make sure that `DATE_FORMAT_JQUERY` and `DATE_FORMAT`
881
+ result in an identical date representation.
882
+
883
+ Constant `DATEPICKER_YEAR_RANGE` defines the range of years in the Datepicker year dropdown. Alternatively,
884
+ you can always change this range dynamically with the following javascript:
885
+
886
+ ```js
887
+ $( ".hasDatepicker" ).datepicker( "option", "yearRange", "2000:2042" );
888
+ ```
889
+
890
+ #### jQuery UI DatePicker `(HELPER_STYLE = :bootstrap)`
891
+
892
+ WiceGrid also supports [Bootstrap Datepicker](https://github.com/Nerian/bootstrap-datepicker-rails).
893
+
894
+ #### Rails standard input fields `(HELPER_STYLE = :standard)`
895
+
896
+ Another option is standard Rails helpers for date fields, these are separate select fields for years,
897
+ months and days (also for hour and minute if it is a datetime field).
898
+
899
+ ### Detached Filters
900
+
901
+ Filters can also be detached from the grid table and placed anywhere on page.
902
+
903
+ This is a 3-step process.
904
+
905
+ First, define the grid with helper `define_grid` instead of `grid`. Everything should be done the same way
906
+ as with `grid`, but every column which will have an external filter, add
907
+ `detach_with_id: :some_filter_name`` in the column definition. The value of `:detach_with_id` is an
908
+ arbitrary string or a symbol value which will be used later to identify the filter.
909
+
910
+ ```erb
911
+ <%= define_grid(@tasks_grid, show_filters: :always) do |g|
912
+
913
+ g.column name: 'Title', attribute: 'title', detach_with_id: :title_filter do |task|
914
+ link_to('Edit', edit_task_path(task.title))
915
+ end
916
+
917
+ g.column name: 'Archived', attribute: 'archived', detach_with_id: :archived_filter do |task|
918
+ task.archived? ? 'Yes' : 'No'
919
+ end
920
+
921
+ g.column name: 'Added', attribute: 'created_at', detach_with_id: :created_at_filter do |task|
922
+ task.created_at.to_s(:short)
923
+ end
924
+
925
+ end -%>
926
+ ```
927
+
928
+ Then, use `grid_filter(grid, :some_filter_name)` to render filters:
929
+
930
+ ```erb
931
+ <% # rendering filter with key :title_filter %>
932
+ <%= grid_filter @tasks_grid, :title_filter %>
933
+
934
+ <% # rendering filter with key :archived_filter %>
935
+ <%= grid_filter @tasks_grid, :archived_filter %>
936
+
937
+ <% # rendering filter with key :created_at_filter %>
938
+ <%= grid_filter @tasks_grid, :created_at_filter %>
939
+
940
+ <% # Rendering the grid body %>
941
+ <%= grid(@tasks_grid) %>
942
+ ```
943
+
944
+ Finally, use `render_grid(@grid)` to actually output the grid table.
945
+
946
+
947
+ Using custom submit and reset buttons together with `hide_submit_button: true` and
948
+ `hide_reset_button: true` allows to completely get rid of the default filter row and the default
949
+ icons (see section 'Submit/Reset Buttons').
950
+
951
+
952
+ If a column was declared with `:detach_with_id`, but never output with `grid_filter`, filtering
953
+ the grid in development mode will result in an warning javascript message and the missing filter will be
954
+ ignored. There is no such message in production.
955
+
956
+
957
+ ### Defining your own external filter processors
958
+
959
+
960
+ It possible to define and use your own column processors outside of the plugin, in you application.
961
+
962
+ The first step is to edit `Wice::Defaults::ADDITIONAL_COLUMN_PROCESSORS` in
963
+ `wice_grid_config.rb`:
964
+
965
+ ```ruby
966
+
967
+ Wice::Defaults::ADDITIONAL_COLUMN_PROCESSORS = {
968
+ my_own_filter: ['ViewColumnMyOwnFilter', 'ConditionsGeneratorMyOwnFilter'],
969
+ another_filter: ['ViewColumnAnotherFilter', 'ConditionsGeneratorAnotherFilter']
970
+ }
971
+ ```
972
+
973
+ The first element in the two-item array is the name of a class responsible for rendering
974
+ the filter view. The second element is the name of a class responsible for processing
975
+ filter parameters.
976
+
977
+ For examples of these two classes look at the existing column processors in `lib/wice/columns/`
978
+
979
+ The structure of these two classes is as follows:
980
+
981
+ ```ruby
982
+ class ViewColumnMyOwnFilter < Wice::Columns::ViewColumn
983
+
984
+ def render_filter_internal(params)
985
+ ...
986
+ end
987
+
988
+ def yield_declaration_of_column_filter
989
+ {
990
+ templates: [...],
991
+ ids: [...]
992
+ }
993
+ end
994
+ end
995
+
996
+
997
+ class ConditionsGeneratorMyOwnFilter < Wice::Columns::ConditionsGeneratorColumn
998
+
999
+ def generate_conditions(table_name, opts)
1000
+ ...
1001
+ end
1002
+
1003
+ end
1004
+ ```
1005
+
1006
+ To use an external column processor use `:filter_type` in a column definition:
1007
+
1008
+ ```ruby
1009
+ column name: 'name', attribute: 'attribute', filter_type: :my_own_filter do |rec|
1010
+ ...
1011
+ end
1012
+ ```
1013
+
1014
+ ## Defaults
1015
+
1016
+ Default values like can be changed in `config/initializers/wice_grid_config.rb`.
1017
+
1018
+ ## Submit/Reset buttons
1019
+ Instead of using default Submit and Reset icons you can use external HTML elements to trigger
1020
+ these actions. Add a button or any other clickable HTML element with class
1021
+ `wg-external-submit-button` or `wg-external-reset-button`, and attribute `data-grid-name`
1022
+ whose value is the name of the grid:
1023
+
1024
+ ```html
1025
+ <button class="wg-external-submit-button" data-grid-name="grid">Submit</button>
1026
+ <button class="wg-external-reset-button" data-grid-name="grid">Reset</button>
1027
+ ```
1028
+
1029
+ To hide the default icons use `hide_submit_button: true` and
1030
+ `hide_reset_button: true` in the `grid` helper.
1031
+
1032
+
1033
+ ## Auto-reloading filters
1034
+
1035
+ It is possible to configure a grid to reload itself once a filter has been changed. It works with all
1036
+ filter types including the JS calendar, the only exception is the standard Rails date/datetime filters.
1037
+
1038
+ Use option `:auto_reload` in the column definiton:
1039
+
1040
+ ```erb
1041
+
1042
+ <%= grid(@tasks_grid, show_filters: :always, hide_submit_button: true) do |g|
1043
+
1044
+ # String
1045
+ g.column name: 'Title', attribute: 'title', auto_reload: true
1046
+
1047
+ # Boolean
1048
+ g.column name: 'Archived', attribute: 'archived', auto_reload: true
1049
+
1050
+ # Custom (dropdown)
1051
+ g.column name: 'Status', attribute: 'status_id', custom_filter: Status.to_dropdown, auto_reload: true do |task|
1052
+ task.status.name if task.status
1053
+ end
1054
+
1055
+ # Datetime
1056
+ g.column name: 'Added', attribute: 'created_at', auto_reload: true, helper_style: :calendar do |task|
1057
+ task.created_at.to_s(:short)
1058
+ end
1059
+
1060
+ end -%>
1061
+ ```
1062
+
1063
+ To make this behavior default change constant `AUTO_RELOAD` in the configuration file.
1064
+
1065
+ ## Styling the grid
1066
+
1067
+
1068
+ ### Adding classes and styles
1069
+
1070
+ The `grid` helper accepts parameter `:html` which is a hash of HTML attributes for the table tag.
1071
+
1072
+ Another `grid` parameter is `header_tr_html` which is a hash of HTML attributes to
1073
+ be added to the first `tr` tag (or two first `tr`'s if the filter row is present).
1074
+
1075
+ `:html` is a parameter for the `column` method setting HTML attributes of `td` tags for a certain column.
1076
+
1077
+ ### Adding classes and styles dynamically
1078
+
1079
+ WiceGrid offers ways to dynamically add classes and styles to `TR` and `TD` based on the current ActiveRecord instance.
1080
+
1081
+
1082
+ For `<TD>`, let the `column` return an array where the first item is the usual
1083
+ string output whole the second is a hash of HTML attributes to be added for the
1084
+ `<td>` tag of the current cell:
1085
+
1086
+ ```ruby
1087
+ g.column do |portal_application|
1088
+ css_class = portal_application.public? ? 'public' : 'private'
1089
+ [portal_application.name, {class: css_class}]
1090
+ end
1091
+ ```
1092
+
1093
+ For adding classes/styles to `<TR>` use special clause `row_attributes` ,
1094
+ similar to `column`, only returning a hash:
1095
+
1096
+ ```erb
1097
+ <%= grid(@versions_grid) do |g|
1098
+ g.row_attributes do |version|
1099
+ if version.in_production?
1100
+ {style: 'background-color: rgb(255, 255, 204);'}
1101
+ end
1102
+ end
1103
+
1104
+ g.column{|version| ... }
1105
+ g.column{|version| ... }
1106
+ end -%>
1107
+ ```
1108
+
1109
+ Naturally, there can be only one `row_attributes` definition for a WiceGrid instance.
1110
+
1111
+ Various classes do not overwrite each other, instead, they are concatenated.
1112
+
1113
+
1114
+ ## Adding rows to the grid
1115
+
1116
+ It is possible to add your own handcrafted HTML after and/or before each grid row.
1117
+ This works similar to `row_attributes`, by adding blocks `after_row`, `before_row`, and `last_row`:
1118
+
1119
+ ```erb
1120
+ <%= grid(@tasks_grid) do |g|
1121
+ g.before_row do |task, number_of_columns|
1122
+ if task.active?
1123
+ "<tr><td colspan=\"10\">Custom line for #{t.name}</td></tr>" # this would add a row
1124
+ # before every active task row
1125
+ else
1126
+ nil
1127
+ end
1128
+ end
1129
+
1130
+ g.last_row do |number_of_columns| # This row will always be added to the bottom of the grid
1131
+ content_tag(:tr,
1132
+ content_tag(:td,
1133
+ 'Last row',
1134
+ colspan: 10),
1135
+ class: 'last_row')
1136
+ end
1137
+
1138
+ .......
1139
+ end %>
1140
+ ```
1141
+
1142
+ It is up for the developer to return the correct HTML code, or return `nil` if no row is needed for this record.
1143
+ Naturally, there is only one `before_row` definition and one `after_row` definition for a WiceGrid instance.
1144
+
1145
+ The second variable injected into to `before_row` and `after_row` block, and the first parameter injected
1146
+ into the `last_row` is the number of columns in the current grid.
1147
+
1148
+ ## Rendering a grid without records
1149
+
1150
+ If the grid does not contain any records to show, it is possible show some alternative view instead of
1151
+ an empty grid. Bear in mind that if the user sets up the filters in such a way that the selection of
1152
+ records is empty, this will still render the grid and it will be possible to reset the grid clicking
1153
+ on the Reset button. Thus, this only works if the initial number of records is 0.
1154
+
1155
+ ```erb
1156
+ <%= grid(@grid) do |g|
1157
+
1158
+ g.blank_slate do
1159
+ "There are no records"
1160
+ end
1161
+
1162
+ g.column do |product|
1163
+ ...
1164
+ end
1165
+ end -%>
1166
+ ```
1167
+
1168
+ There are two alternative ways to do the same, submitting a string to `blank_slate`:
1169
+
1170
+ ```ruby
1171
+ g.blank_slate "some text to be rendered"
1172
+ ```
1173
+
1174
+ Or a partial:
1175
+
1176
+ ```ruby
1177
+ g.blank_slate partial: "partial_name"
1178
+ ```
1179
+
1180
+ ## Action Column
1181
+
1182
+ It is possible to add a column with checkboxes for each record. This is useful for actions with multiple records,
1183
+ for example, deleting selected records. Please note that `action_column` only creates the checkboxes and the
1184
+ 'Select All' and 'Deselect All' buttons, and the form itself as well as processing the parameters should be
1185
+ taken care of by the application code.
1186
+
1187
+ ```erb
1188
+ <%= grid(@tasks_grid, show_filters: :always) do |g|
1189
+
1190
+ ...
1191
+
1192
+ g.action_column
1193
+
1194
+ ...
1195
+
1196
+ end -%>
1197
+ ```
1198
+
1199
+ By default the name of the HTTP parameter follows pattern `"#{grid_name}[#{param_name}][]"`, thus
1200
+ `params[grid_name][param_name]` will contain an array of object IDs.
1201
+
1202
+ You can hide a certain action checkbox if you add the usual block to `g.action_column`, just like with the
1203
+ `g.column` definition. If the block returns `nil` or `false` no checkbox will be rendered.
1204
+
1205
+ ```erb
1206
+ <%= grid(@tasks_grid, show_filters: :always) do |g|
1207
+
1208
+ ...
1209
+
1210
+ g.action_column do |task|
1211
+ task.finished?
1212
+ end
1213
+
1214
+ ...
1215
+
1216
+ end -%>
1217
+ ```
1218
+
1219
+ WiceGrid is form-friendly: submitting grid in a form retains the state of the form.
1220
+
1221
+
1222
+
1223
+ ## Integration of the grid with other forms on page
1224
+
1225
+ Imagine that the user should be able to change the behavior of the grid using some other control
1226
+ on the page, and not a grid filter.
1227
+
1228
+ For example, on a page showing tasks, change between 'Show active tasks' to 'Show archived tasks' using a dropdown box.
1229
+ WiceGrid allows to keep the status of the grid with all the filtering and sorting using helper
1230
+ `dump_filter_parameters_as_hidden_fields` which takes a grid object and dumps
1231
+ all current sorting and filtering parameters as hidden fields. Just include
1232
+ `dump_filter_parameters_as_hidden_fields(@grid)` inside your form, and the newly rendered grid will keep ordering and filtering.
1233
+
1234
+ ```erb
1235
+ <% form_tag('', method: :get) do %>
1236
+ <%= dump_filter_parameters_as_hidden_fields(@tasks_grid) %>
1237
+ <%= select_tag 'archived',
1238
+ options_for_select([['View active tasks', 0], ['View archived tasks', 1]], @archived ? 1 : 0),
1239
+ onchange: 'this.form.submit()' %>
1240
+ <% end -%>
1241
+ ```
1242
+
1243
+
1244
+ ## Show All Records
1245
+
1246
+ It is possible to switch to the All Records mode clicking on link "show all" in the bottom right corner.
1247
+ This functionality should be used with care. To turn this mode off for all grid instances,
1248
+ change constant `ALLOW_SHOWING_ALL_RECORDS` in `config/initializers/wice_grid_config.rb` to
1249
+ `false`. To do so for a specific grid, use initializer parameter `:allow_showing_all_records`.
1250
+
1251
+ Configuration constant `START_SHOWING_WARNING_FROM` sets the threshold number of all records after
1252
+ which clicking on the link results in a javascript confirmation dialog.
1253
+
1254
+
1255
+ ## CSV Export
1256
+
1257
+ It is possible to export the data displayed on a grid to a CSV file. The dumped data is the current resultset
1258
+ with all the current filters and sorting applied, only without the pagination constraint (i.e. all pages).
1259
+
1260
+ To enable CSV export add parameters `enable_export_to_csv` and `csv_file_name` to the initialization of the grid:
1261
+
1262
+ ```ruby
1263
+ @projects_grid = initialize_grid(Project,
1264
+ include: [:customer, :supplier],
1265
+ name: 'g2',
1266
+ enable_export_to_csv: true,
1267
+ csv_file_name: 'projects'
1268
+ )
1269
+ ```
1270
+
1271
+ `csv_file_name` is the name of the downloaded file. This parameter is optional, if it is missing, the name of
1272
+ the grid is used instead. The export icon will appear at the bottom right corner of the grid. If the program you are importing the generated CSV into has problem processing UTF-8, you can change the character encoding using the `csv_encoding` option. P.e. setting `csv_encoding: 'CP1252:UTF-8'` will make older versions of Excel happy. The format used is `<output encoding>:<input encoding>`.
1273
+
1274
+ Next, each grid view helper should be placed in a partial of its own, requiring it from the master
1275
+ template for the usual flow. There must be no HTML or ERB code in this partial except for the grid helper.
1276
+
1277
+ By convention the name of such a partial follows the following pattern:
1278
+
1279
+ ```
1280
+ _GRID_NAME_grid.html.erb
1281
+ ```
1282
+
1283
+ In other words, a grid named `tasks` is expected to be found in a template called
1284
+ `_tasks_grid.html.erb` (remember that the default name of grids is '`grid`'.)
1285
+
1286
+ Next, method `export_grid_if_requested` should be added to the end of each action
1287
+ containing grids with enabled CSV export.
1288
+
1289
+ `export_grid_if_requested` intercepts CSV export requests and evaluates the partial with the required grid helper.
1290
+
1291
+ The naming convention for grid partials can be easily overridden by supplying a hash parameter
1292
+ to `export_grid_if_requested` where each key is the name of a grid, and the value is the name of
1293
+ the template (like it is specified for `render`, i.e. without '_' and extensions):
1294
+
1295
+ ```ruby
1296
+ export_grid_if_requested('g1' => 'tasks_grid', 'g2' => 'projects_grid')
1297
+ ```
1298
+
1299
+ If the request is not a CSV export request, `export_grid_if_requested` does nothing and returns
1300
+ `false`, if it is a CSV export request, the method returns `true`.
1301
+
1302
+
1303
+ If the action has no explicit `render` call, it's OK to just place `export_grid_if_requested`
1304
+ as the last line of the action:
1305
+
1306
+ ```ruby
1307
+ def index
1308
+
1309
+ @tasks_grid = initialize_grid(Task,
1310
+ name: 'g1',
1311
+ enable_export_to_csv: true,
1312
+ csv_file_name: 'tasks'
1313
+ )
1314
+
1315
+ @projects_grid = initialize_grid(Project,
1316
+ name: 'g2',
1317
+ enable_export_to_csv: true,
1318
+ csv_file_name: 'projects'
1319
+ )
1320
+
1321
+ export_grid_if_requested
1322
+ end
1323
+ ```
1324
+
1325
+ Otherwise, to avoid double rendering, use the return value of the method to conditionally call your `render` :
1326
+
1327
+ ```ruby
1328
+
1329
+ def index
1330
+
1331
+ ...........
1332
+
1333
+ export_grid_if_requested || render(action: 'my_template')
1334
+ end
1335
+ ```
1336
+
1337
+ It's also possible to supply a block which will be called if no CSV export is requested:
1338
+
1339
+ ```ruby
1340
+ def index
1341
+
1342
+ ...........
1343
+
1344
+ export_grid_if_requested do
1345
+ render(action: 'my_template')
1346
+ end
1347
+ end
1348
+ ```
1349
+
1350
+ If a column has to be excluded from the CSV export,
1351
+ set `column` parameter `in_csv` to `false`:
1352
+
1353
+ ```ruby
1354
+ g.column in_csv: false do |task|
1355
+ link_to('Edit', edit_task_path(task))
1356
+ end
1357
+ ```
1358
+
1359
+ If a column must appear both in HTML and CSV, but with different output, duplicate the column and use
1360
+ parameters `in_csv` and `in_html` to include one of them to html output only, the other to CSV only:
1361
+
1362
+ ```ruby
1363
+ # html version
1364
+ g.column name: 'Title', attribute: 'title', in_csv: false do |task|
1365
+ link_to('Edit', edit_task_path(task.title))
1366
+ end
1367
+ # plain text version
1368
+ g.column name: 'Title', in_html: false do |task|
1369
+ task.title
1370
+ end
1371
+ ```
1372
+
1373
+ The default field separator in generated CSV is a comma, but it's possible to override it:
1374
+
1375
+ ```ruby
1376
+ @products_grid = initialize_grid(Product,
1377
+ enable_export_to_csv: true,
1378
+ csv_field_separator: ';',
1379
+ csv_file_name: 'products'
1380
+ )
1381
+ ```
1382
+
1383
+ If you need an external CSV export button , add class `wg-external-csv-export-button`
1384
+ to any clickable element on page and set its attribute `data-grid-name` to the name of the grid:
1385
+
1386
+ ```html
1387
+ <button class="wg-external-csv-export-button" data-grid-name="grid">Export To CSV</button>
1388
+ ```
1389
+
1390
+ If you need to disable the default export icon in the grid, add `hide_csv_button: true` to the `grid` helper.
1391
+
1392
+
1393
+ ## Access to Records From Outside The Grid
1394
+
1395
+ There are two ways you can access the records outside the grid - using methods of the WiceGrid
1396
+ object and using callbacks.
1397
+
1398
+ ### Accessing Records Via The WiceGrid Object
1399
+
1400
+ Method `current_page_records` returns exactly the same list of objects displayed on page:
1401
+
1402
+ ```erb
1403
+ <%= grid(@tasks_grid) do |g|
1404
+ ...
1405
+ end -%>
1406
+
1407
+ <p>
1408
+ IDs of records on the current page:
1409
+ <%= @tasks_grid.current_page_records.map(&:id).to_sentence %>
1410
+ </p>
1411
+ ```
1412
+
1413
+ Method `all_pages_records` returns a list of objects browsable through all pages with the current filters:
1414
+
1415
+ ```erb
1416
+ <%= grid(@tasks_grid) do |g|
1417
+ ...
1418
+ end -%>
1419
+
1420
+ <p>
1421
+ IDs of all records:
1422
+ <%= @tasks_grid.all_pages_records.map(&:id).to_sentence %>
1423
+ </p>
1424
+ ```
1425
+
1426
+ Mind that this helper results in an additional SQL query.
1427
+
1428
+
1429
+ Because of the current implementation of WiceGrid these helpers work only after the declaration
1430
+ of the grid in the view.
1431
+ This is due to the lazy nature of WiceGrid - the actual call to the database is made during
1432
+ the execution of
1433
+ the `grid` helper, because to build the correct query columns declarations are required.
1434
+
1435
+ ### Accessing Records Via Callbacks
1436
+
1437
+ It is possible to set up callbacks which are executed from within the plugin just after the call to the database.
1438
+ The callbacks are called before rendering the grid cells, so the results of this processing can be used in the grid.
1439
+ There are 3 ways you can set up such callbacks:
1440
+
1441
+ Via a lambda object:
1442
+
1443
+ ```ruby
1444
+ def index
1445
+ @tasks_grid = initialize_grid(Task,
1446
+ with_paginated_resultset: ->(records){
1447
+ ...
1448
+ }
1449
+ )
1450
+ end
1451
+ ```
1452
+
1453
+ Via a symbol which is the name of a controller method:
1454
+
1455
+ ```ruby
1456
+ def index
1457
+ @tasks_grid = initialize_grid(Task,
1458
+ with_paginated_resultset: :process_selection
1459
+ )
1460
+ end
1461
+
1462
+ def process_selection(records)
1463
+ ...
1464
+ end
1465
+ ```
1466
+
1467
+ Via a separate block:
1468
+
1469
+ ```ruby
1470
+ def index
1471
+ @tasks_grid = initialize_grid(Task)
1472
+
1473
+ @tasks_grid.with_paginated_resultset do |records|
1474
+ ...
1475
+ end
1476
+ end
1477
+ ```
1478
+
1479
+ There are two callbacks:
1480
+
1481
+ * `:with_paginated_resultset` - used to process records of the current page
1482
+ * `:with_resultset` - used to process all records browsable through all pages with the current filters
1483
+
1484
+ While the `:with_paginated_resultset` callback just receives the list of records, `:with_resultset`
1485
+ receives an ActiveRelation object which can be used to obtain the list of all records:
1486
+
1487
+ ```ruby
1488
+ def index
1489
+ @tasks_grid = initialize_grid(Task)
1490
+
1491
+ @tasks_grid.with_resultset do |active_relation|
1492
+ all_records = active_relation.all
1493
+ ...
1494
+ end
1495
+ end
1496
+ ```
1497
+
1498
+ This lazy nature exists for performance reasons.
1499
+ Reading all records leads to an additional call, and there can be cases when processing all records should be triggered
1500
+ only under certain circumstances:
1501
+
1502
+ ```ruby
1503
+ def index
1504
+ @tasks_grid = initialize_grid(Task)
1505
+
1506
+ @tasks_grid.with_resultset do |active_relation|
1507
+ if params[:process_all_records]
1508
+ all_records = active_relation.all
1509
+ ...
1510
+ end
1511
+ end
1512
+ end
1513
+ ```
1514
+
1515
+ ## Bug reports
1516
+
1517
+ The author of the plugin welcomes any contribution.
1518
+ Please follow [these guidelines](https://github.com/leikind/wice_grid/wiki/How-to-submit-a-bug-report-or-a-question) when submitting a bug report.