mongoid_wice_grid 4.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (72) hide show
  1. data/.gitignore +9 -0
  2. data/CHANGELOG +409 -0
  3. data/Gemfile +17 -0
  4. data/Gemfile.lock +140 -0
  5. data/MIT-LICENSE +20 -0
  6. data/README.rdoc +1188 -0
  7. data/Rakefile +40 -0
  8. data/SAVED_QUERIES_HOWTO.rdoc +123 -0
  9. data/VERSION +1 -0
  10. data/lib/filter_conditions_generators.rb +126 -0
  11. data/lib/generators/wice_grid/templates/calendarview.css +107 -0
  12. data/lib/generators/wice_grid/templates/calendarview.js +1168 -0
  13. data/lib/generators/wice_grid/templates/icons/arrow_down.gif +0 -0
  14. data/lib/generators/wice_grid/templates/icons/arrow_up.gif +0 -0
  15. data/lib/generators/wice_grid/templates/icons/calendar_view_month.png +0 -0
  16. data/lib/generators/wice_grid/templates/icons/delete.png +0 -0
  17. data/lib/generators/wice_grid/templates/icons/expand.png +0 -0
  18. data/lib/generators/wice_grid/templates/icons/page_white_excel.png +0 -0
  19. data/lib/generators/wice_grid/templates/icons/page_white_find.png +0 -0
  20. data/lib/generators/wice_grid/templates/icons/table.png +0 -0
  21. data/lib/generators/wice_grid/templates/icons/table_refresh.png +0 -0
  22. data/lib/generators/wice_grid/templates/icons/tick_all.png +0 -0
  23. data/lib/generators/wice_grid/templates/icons/untick_all.png +0 -0
  24. data/lib/generators/wice_grid/templates/wice_grid.css +173 -0
  25. data/lib/generators/wice_grid/templates/wice_grid.yml +269 -0
  26. data/lib/generators/wice_grid/templates/wice_grid_config.rb +215 -0
  27. data/lib/generators/wice_grid/templates/wice_grid_jquery.js +161 -0
  28. data/lib/generators/wice_grid/templates/wice_grid_prototype.js +153 -0
  29. data/lib/generators/wice_grid/wice_grid_assets_jquery_generator.rb +32 -0
  30. data/lib/generators/wice_grid/wice_grid_assets_prototype_generator.rb +34 -0
  31. data/lib/grid_output_buffer.rb +52 -0
  32. data/lib/grid_renderer.rb +547 -0
  33. data/lib/helpers/js_calendar_helpers.rb +183 -0
  34. data/lib/helpers/wice_grid_misc_view_helpers.rb +113 -0
  35. data/lib/helpers/wice_grid_serialized_queries_view_helpers.rb +82 -0
  36. data/lib/helpers/wice_grid_view_helpers.rb +761 -0
  37. data/lib/js_adaptors/jquery_adaptor.rb +145 -0
  38. data/lib/js_adaptors/js_adaptor.rb +12 -0
  39. data/lib/js_adaptors/prototype_adaptor.rb +168 -0
  40. data/lib/mongoid_field.rb +50 -0
  41. data/lib/tasks/wice_grid_tasks.rake +28 -0
  42. data/lib/view_columns.rb +464 -0
  43. data/lib/views/create.rjs +13 -0
  44. data/lib/views/delete.rjs +12 -0
  45. data/lib/wice_grid.rb +521 -0
  46. data/lib/wice_grid_controller.rb +165 -0
  47. data/lib/wice_grid_core_ext.rb +179 -0
  48. data/lib/wice_grid_misc.rb +99 -0
  49. data/lib/wice_grid_serialized_queries_controller.rb +77 -0
  50. data/lib/wice_grid_serialized_query.rb +14 -0
  51. data/lib/wice_grid_spreadsheet.rb +33 -0
  52. data/test/.gitignore +2 -0
  53. data/test/blueprint.rb +17 -0
  54. data/test/database.yml +21 -0
  55. data/test/public/javascripts/jquery-1.4.2.min.js +154 -0
  56. data/test/public/javascripts/wice_grid.js +163 -0
  57. data/test/rails_mongoid_test.rb +104 -0
  58. data/test/rails_test_app.rb +71 -0
  59. data/test/require_gems.rb +19 -0
  60. data/test/schema.rb +33 -0
  61. data/test/spec_helper.rb +22 -0
  62. data/test/test_helper.rb +89 -0
  63. data/test/views/projects_and_people_grid.html.erb +12 -0
  64. data/test/views/projects_and_people_grid_invalid.html.erb +12 -0
  65. data/test/views/simple_projects_grid.html.erb +9 -0
  66. data/test/wice_grid_core_ext_test.rb +183 -0
  67. data/test/wice_grid_functional_test.rb +68 -0
  68. data/test/wice_grid_initializer.rb +215 -0
  69. data/test/wice_grid_misc_test.rb +41 -0
  70. data/test/wice_grid_test.rb +42 -0
  71. data/test/wice_grid_view_helper_test.rb +12 -0
  72. metadata +150 -0
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Yuri Leikind & the WICE team (http://wice.eu)
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.rdoc ADDED
@@ -0,0 +1,1188 @@
1
+ = WiceGrid
2
+
3
+ Version:: 3.0.0.pre1
4
+ Author:: Yuri Leikind
5
+ Contributor:: Xavier Defrang
6
+ Homepage:: http://leikind.org/pages/wicegrid
7
+ Examples online:: http://grid.leikind.org
8
+ News:: http://leikind.org/wicegrid
9
+ Email:: "Yuri Leikind" <yuri.leikind at gmail dot com>
10
+
11
+ == Intro
12
+
13
+ afurmanov> The intention of this fork is to make WiseGrid compatible with Mongoid.
14
+
15
+ THIS IS AN EARLY ALPHA VERSION OF THE PLUGIN FOR RAILS 3.0.
16
+ BASIC STUFF WORKS, BUT A LOT OF FUNCTIONALITIES ARE BROKEN.
17
+ THE DOCUMENTATION BELOW CAN BE OUTDATED.
18
+
19
+ FOR RAILS 3.0 IT ONLY WORKS AS A GEM.
20
+ AT THE SAME TIME NO OFFICIAL GEM IS YET RELEASED, SO IF YOU WANT TO TRY IT, CLONE THE REPOSITORY, PUT IT INTO vendor/wice_grid AND ADD THIS TO YOUR GEMFILE:
21
+
22
+ gem "wice_grid", '3.0.0.pre1', :path => 'vendor/wice_grid'
23
+
24
+
25
+ OR BUILD THE GEM.
26
+
27
+ =========================
28
+
29
+ WiceGrid is a Rails grid plugin.
30
+
31
+ One of the goals of this plugin was to allow the programmer to define the contents of the cell by himself, just like one does when rendering a collection via a simple table (and this is what differentiates WiceGrid from various scaffolding solutions), but automate implementation of filters, ordering, paginations, CSV export, and so on. Ruby blocks provide an elegant means for this.
32
+
33
+
34
+ WiceGrid builds the call to the ActiveRecord layer for you and creates a table view with the results of the call including:
35
+
36
+ * paging
37
+ * sortable columns
38
+ * filtering by multiple columns
39
+ * CSV export
40
+ * saving queries
41
+
42
+ All working nicely together. Filters are added automatically according to the type of the underlying DB column. Filtering by more than one column at the same time is possible. More than one such grid can appear on a page, and manipulations with one grid do not have any impact on the other.
43
+
44
+ WiceGrid does not take a collection as an input, it works directly with ActiveRecord (with the help of <tt>will_paginate[http://github.com/mislav/will_paginate/wikis]</tt>).
45
+
46
+ WiceGrid does not use AJAX calls to reload itself, instead simple GET requests are used for this, nevertheless, all other page parameters are respected and preserved. WiceGrid views do not contain forms so you can include it in your own forms.
47
+
48
+ WiceGrid is known to work with Rails versions from 2.1.x to 2.3.9. WiceGrid with Rails versions older
49
+ than 2.1 can still be functional depending on the functionalities used.
50
+
51
+ WiceGrid is known to work with MySQL and Postgres.
52
+
53
+ WiceGrid works with Prototype and jQuery (experimental).
54
+
55
+ WiceGrid is compliant with Ruby 1.9.1.
56
+
57
+ === Examples
58
+
59
+ This tutorial is accompanied by a sample application with WiceGrid examples which you can browse online ( http://grid.leikind.org ), or just view the code ( http://github.com/leikind/wice_grid_examples ).
60
+
61
+
62
+ == Requirements
63
+
64
+ Rails version 2.1.0 or newer.
65
+
66
+ <tt>will_paginate[http://github.com/mislav/will_paginate/wikis]</tt> version 2.3.2 or newer.
67
+
68
+ <tt>Prototype[http://www.prototypejs.org]</tt> version 1.5.1 or newer, or <tt>jQuery[http://jquery.com/]</tt>.
69
+
70
+ == How-To
71
+
72
+ === Installation
73
+
74
+ Run the following command to install the plugin:
75
+
76
+ script/plugin install git://github.com/leikind/wice_grid.git
77
+
78
+ If your application is Prototype-based (default in Rails 2.x), run the following generator to copy all plugin assets:
79
+
80
+ ./script/generate wice_grid_assets_prototype
81
+
82
+ For jQuery:
83
+
84
+ ./script/generate wice_grid_assets_jquery
85
+
86
+ This will copy images, stylesheets, configuration file <tt>config/initializers/wice_grid_config.rb</tt>, and the correct version
87
+ of javascript files.
88
+
89
+ === Support for Prototype and jQuery
90
+
91
+ WiceGrid started as a plugin using the Prototype javascript framework. Support for jQuery was added for version 0.6.
92
+ The plugin contains two versions of the main javascript file <tt>wice_grid.js</tt>, and depending on the generator run to
93
+ install the assets (+wice_grid_assets_prototype+ or +wice_grid_assets_jquery+) the correct file is copied into <tt>public/javascripts</tt>.
94
+
95
+ Some javascript code is also generated and injected into the HTML page. The value of constant <tt>Wice::Defaults::JS_FRAMEWORK</tt> in
96
+ configuration file <tt>wice_grid_config.rb</tt> defines for which JS framework code should be generated.
97
+ Generators +wice_grid_assets_prototype+ and +wice_grid_assets_jquery+ create a version of <tt>wice_grid_config.rb</tt>
98
+ with the corresponding value of <tt>Wice::Defaults::JS_FRAMEWORK</tt>.
99
+
100
+
101
+ In the Prototype mode the plugin uses a forked[http://github.com/leikind/calendarview] version of Calendarview[http://github.com/jsmecham/calendarview]. It is bundled with the plugin and the generator task
102
+ +wice_grid_assets_prototype+ will install al necessary assets.
103
+
104
+ The jQuery version uses jQuery datepicker[http://jqueryui.com/demos/datepicker/]. Because this is part
105
+ of the standard jQuery codebase, it is not bundled together with the plugin, and it is the responsibility
106
+ of the programmer to include all necessary assets including localization files if the application is
107
+ multilingual.
108
+
109
+ It is always possible to fall back to simple dropdown lists that the standard date/datetime Rails helpers use.
110
+
111
+ JQUERY DATEPICKER IS ONLY USED FOR DATE FILTERS AT THE MOMENT. DATETIME FILTERS FALL BACK TO THE DEFAULT
112
+ RAILS DATE/DATETIME HELPERS.
113
+
114
+
115
+ === Basics
116
+
117
+ The Prototype version of WiceGrid requires <tt>prototype.js</tt> and <tt>effects.js</tt> in order to function.
118
+ The jQuery version of WiceGrid requires <tt>jquery.js</tt> and <tt>jquery-ui.js</tt> in order to function.
119
+
120
+ Thus, make sure you have <tt><%=javascript_include_tag :defaults %></tt> in your layout template.
121
+
122
+ To include WiceGrid javascript and stylesheet files to the page use helper +include_wice_grid_assets+ :
123
+
124
+ <%= include_wice_grid_assets %>
125
+
126
+ The default behavior of +include_wice_grid_assets+ is a bit magical in the sense that it
127
+ produces no output if the page contains no grids, so don't panic if you see no WiceGrid includes.
128
+ If you prefer to always have WiceGrid stylesheet and javascript, use
129
+
130
+ <%= include_wice_grid_assets :load_on_demand => false %>
131
+
132
+ The simplest example of a WiceGrid for one simple DB table called ApplicationAccount is the following:
133
+
134
+ Controller:
135
+
136
+ @tasks_grid = initialize_grid(Task)
137
+
138
+ View:
139
+
140
+ <%= grid(@tasks_grid) do |g|
141
+
142
+ g.column do |task|
143
+ task.id
144
+ end
145
+
146
+ g.column do |task|
147
+ task.title
148
+ end
149
+
150
+ g.column do |task|
151
+ task.description
152
+ end
153
+
154
+ g.column do |task|
155
+ task.archived? ? 'Yes' : 'No'
156
+ end
157
+
158
+ g.column do |task|
159
+ link_to('Edit', edit_task_path(task))
160
+ end
161
+ end -%>
162
+
163
+ Code <tt>g.column do |task| ... end</tt>
164
+ defines everything related to a column in the resulting view table including column names, sorting, filtering, the content of the column cells, etc.
165
+ The return value of the block is the table cell content.
166
+
167
+ In the above view code five columns were defined, all without names, no sorting or filtering is available. Still, pagination becomes active if
168
+ the number of all extracted records exceeds the default number of rows per page.
169
+
170
+ Column names are defined with parameter <tt>:column_name</tt>:
171
+
172
+ <%= grid(@tasks_grid) do |g|
173
+
174
+ g.column :column_name => 'ID' do |task|
175
+ task.id
176
+ end
177
+
178
+ g.column :column_name => 'Title' do |task|
179
+ task.title
180
+ end
181
+
182
+ g.column :column_name => 'Description' do |task|
183
+ task.description
184
+ end
185
+
186
+ g.column :column_name => 'Archived' do |task|
187
+ task.archived? ? 'Yes' : 'No'
188
+ end
189
+
190
+ g.column do |task|
191
+ link_to('Edit', edit_task_path(task))
192
+ end
193
+ end -%>
194
+
195
+ To add filtering and ordering, declare to which column in the underlying database table(s) the view column corresponds using
196
+ parameter <tt>:attribute_name</tt> :
197
+
198
+ <%= grid(@tasks_grid) do |g|
199
+
200
+ g.column :column_name => 'ID', :attribute_name => 'id' do |task|
201
+ task.id
202
+ end
203
+
204
+ g.column :column_name => 'Title', :attribute_name => 'title' do |task|
205
+ task.title
206
+ end
207
+
208
+ g.column :column_name => 'Description', :attribute_name => 'description' do |task|
209
+ task.description
210
+ end
211
+
212
+ g.column :column_name => 'Archived', :attribute_name => 'archived' do |task|
213
+ task.archived? ? 'Yes' : 'No'
214
+ end
215
+
216
+ g.column do |task|
217
+ link_to('Edit', edit_task_path(task))
218
+ end
219
+ end -%>
220
+
221
+ This will add sorting links and filters for columns +Username+ and +Active+.
222
+ The plugin automatically creates filters according to the type
223
+ of the database column. In the above example a text field will be created for column Title (title is a string),
224
+ for column +Archived+ a dropdown filter will be created with options 'Yes', 'No', and '--', and for the integer ID two short text fields are
225
+ added which can contain the numeric range (more than, less than).
226
+
227
+ It is important to remember that <tt>:attribute_name</tt> is the name of the database column, not a model attribute. Of course, all database columns have corresponding model attributes, but not all model attributes map to columns in the same table with the same name.
228
+
229
+ Read more about available filters in the documentation
230
+ for the column method. Read the section about custom dropdown filters for more advanced filters.
231
+
232
+
233
+ For columns like
234
+
235
+ g.column :column_name => 'Title', :attribute_name => 'title' do |task|
236
+ task.title
237
+ end
238
+
239
+ where the block contains just a call to the same attribute declared by :attribute_name, the block can be omitted:
240
+
241
+ <%= grid(@tasks_grid) do |g|
242
+
243
+ g.column :column_name => 'ID', :attribute_name => 'id'
244
+
245
+ g.column :column_name => 'Title', :attribute_name => 'title'
246
+
247
+ g.column :column_name => 'Description', :attribute_name => 'description'
248
+
249
+ g.column :column_name => 'Archived', :attribute_name => 'archived' do |task|
250
+ task.archived? ? 'Yes' : 'No'
251
+ end
252
+
253
+ g.column do |task|
254
+ link_to('Edit', edit_task_path(task))
255
+ end
256
+ end -%>
257
+
258
+
259
+ In this case +attribute_name+ will be used as the method name to send to the ActiveRecord instance.
260
+
261
+ If only sorting is needed, we can turn off filters using <tt>:no_filter</tt> :
262
+
263
+ g.column :column_name => 'ID', :attribute_name => 'id', :no_filter => true
264
+
265
+ If no ordering links are needed, use <tt>:allow_ordering</tt>:
266
+
267
+ g.column :column_name => 'Added', :attribute_name => 'created_at', :allow_ordering => false
268
+
269
+ It is important to understand that it is up to the developer to make sure that the value returned by a column block (the content of a cell) corresponds to the underlying database column specified by <tt>:attribute_name</tt> (and <tt>:model_class</tt> discussed below).
270
+
271
+
272
+ === Rendering filter panel
273
+
274
+ The filter panel can be shown and hidden clicking the icon with binoculars.
275
+
276
+ The way the filter panel is shown after the page is loaded is controlled via parameter <tt>:show_filters</tt> of the <tt>grid</tt> helper.
277
+ Possible values are:
278
+
279
+ * <tt>:when_filtered</tt> - the filter is shown when the current table is the result of filtering
280
+ * <tt>:always</tt> - show the filter always
281
+ * <tt>:no</tt> - never show the filter
282
+
283
+ Example:
284
+
285
+ <%= grid(@tasks_grid, :show_filters => :always) do |g|
286
+ ......
287
+ end -%>
288
+
289
+
290
+ Filter related icons (filter icon, reset icon, show/hide icon) are placed in the header of the last column if
291
+ it doesn't have any filter or a column name, otherwise an additional table column is added. To always place
292
+ the icons in the additional column, set <tt>Wice::Defaults::REUSE_LAST_COLUMN_FOR_FILTER_ICONS</tt> to +false+ in
293
+ the configuration file.
294
+
295
+
296
+ === Initial Ordering
297
+
298
+ Initializing the grid we can also define the column by which the record will be ordered <em>on the first rendering of the grid</em>, when the user has not set their ordering setting by clicking the column label, and the order direction:
299
+
300
+ @tasks_grid = initialize_grid(Task,
301
+ :order => 'tasks.title',
302
+ :order_direction => 'desc'
303
+ )
304
+
305
+ === Records Per Page
306
+
307
+ The number of rows per page is set with <tt>:per_page</tt>:
308
+
309
+ @tasks_grid = initialize_grid(Task, :per_page => 40)
310
+
311
+ === Conditions
312
+
313
+ The +initialize_grid+ method supports a <tt>:conditions</tt> parameter which is passed on to the underlying ActiveRecord (via will_paginate), so it can be in any format processable by ActiveRecord:
314
+
315
+ @tasks_grid = initialize_grid(Task,
316
+ :conditions => ["archived = false and estimated_time > ?", 100]
317
+ )
318
+
319
+ @tasks_grid = initialize_grid(Task,
320
+ :include => :project,
321
+ :conditions => {:archived => false, :project => {:active => true}}
322
+ )
323
+
324
+
325
+
326
+ A good example is substituting a common pattern like
327
+
328
+ @user_groups = @portal_application.user_groups
329
+
330
+ with WiceGrid code:
331
+
332
+ @user_groups_grid = initialize_grid(UserGroup, :conditions => ['portal_application_id = ?', @portal_application])
333
+
334
+
335
+ === Queries with join tables
336
+
337
+ WiceGrid also supports ActiveRecord's <tt>:joins</tt> and <tt>:include</tt>.
338
+
339
+ @products_grid = initialize_grid(Product,
340
+ :include => :category,
341
+ :order => 'products.name',
342
+ :per_page => 20)
343
+
344
+ Note that if we want to order initially by a column from a joined table we have to specify the table and the column name with the sql dot notation, that is, <tt>products.name</tt>
345
+
346
+ To show columns of joined tables in the view table, the ActiveRecord model class name has to be specified, that corresponds to the joined table:
347
+
348
+ <%= grid(@products_grid) do |g|
349
+ g.column :column_name => 'Product Name', :attribute_name => 'name' do |product| # primary table
350
+ link_to(product.name, product_path(product))
351
+ end
352
+
353
+ g.column :column_name => 'Category', :attribute_name => 'name', :model_class => Category |product| # joined table
354
+ product.category.name
355
+ end
356
+ %>
357
+
358
+ Please note that the blockless definition of the column only works with columns from the main table and it won't work with columns with <tt>:model_class</tt>
359
+
360
+ === Joined associations referring to the same table
361
+
362
+ In case there are two joined associations both referring to the same table, ActiveRecord constructs a query where
363
+ the second join provides an alias for the joined table. To enable WiceGrid to order and filter by columns belonging to different associations
364
+ but originating from the same table, set <tt>:table_alias</tt> to this alias:
365
+
366
+ Model:
367
+
368
+ class Project < ActiveRecord::Base
369
+ belongs_to :customer, :class_name => 'Company'
370
+ belongs_to :supplier, :class_name => 'Company'
371
+ end
372
+
373
+ Controller:
374
+
375
+ @projects_grid = initialize_grid(Project, :include => [:customer, :supplier] )
376
+
377
+
378
+ View:
379
+
380
+ <%= grid(@projects_grid, :show_filters => :always) do |g|
381
+
382
+ g.column :column_name => 'Project Name', :attribute_name => 'name'
383
+
384
+ g.column :column_name => 'Customer company', :model_class => 'Company', :attribute_name => 'name' do |task|
385
+ task.customer.name if task.customer
386
+ end
387
+
388
+ g.column :column_name => 'Supplier company', :model_class => 'Company', :attribute_name => 'name', :table_alias => ' suppliers_projects' do |task|
389
+ task.supplier.name if task.supplier
390
+ end
391
+
392
+ end -%>
393
+
394
+
395
+ === More than one grid on a page
396
+
397
+ It is possible to use more that one grid on a page, each with its own state. To do so, you must specify the name of the grid in +initialize_grid+ using
398
+ parameter <tt>:name</tt>
399
+
400
+ The name serves as the base name for HTTP parameters, DOM IDs, etc, so it is important that all grids on a page have different names. The default
401
+ name is 'grid'.
402
+
403
+ The name can only contain alphanumeric characters.
404
+
405
+ @projects_grid = initialize_grid(Project, :name => 'g1' )
406
+ @tasks_grid = initialize_grid(Task, :name => 'g2')
407
+
408
+
409
+ === Custom Ordering
410
+
411
+ It is possible to change the way results are ordered injecting a piece of SQL code, for example, use
412
+ <tt>ORDER BY INET_ATON(ip_address)</tt> instead of <tt>ORDER BY ip_address</tt>.
413
+
414
+ To do so, provide parameter <tt>:custom_order</tt> in the initialization of the grid with a hash where keys are fully qualified names
415
+ of database columns, and values the required chunks of SQL to use in the ORDER BY clause.
416
+
417
+ For example:
418
+
419
+
420
+ @hosts_grid = initialize_grid(Host,
421
+ :custom_order => {
422
+ 'hosts.ip_address' => 'INET_ATON(hosts.ip_address)'
423
+ })
424
+
425
+
426
+ It is possible to use the '?' character instead of the name of the column in the hash value:
427
+
428
+ @hosts_grid = initialize_grid(Host,
429
+ :custom_order => {
430
+ 'hosts.ip_address' => 'INET_ATON( ? )'
431
+ })
432
+
433
+ Values can also be Proc objects. The parameter supplied to such a Proc object is the name of the column:
434
+
435
+ @hosts_grid = initialize_grid(Host,
436
+ :custom_order => {
437
+ 'hosts.ip_address' => lambda{|f| "INET_ATON( #{f} )"}
438
+ })
439
+
440
+
441
+ === Custom dropdown filters
442
+
443
+ It is possible to construct custom dropdown filters. Depending on the value of <tt>column</tt> parameter<tt>:custom_filter</tt> different
444
+ modes are available:
445
+
446
+
447
+ ==== Array of two-element arrays or a hash
448
+
449
+ An array of two-element arrays or a hash are semantically identical ways of creating a custom filter.
450
+
451
+ Every first item of the two-element array is used for the label of the select option while the second element is the value of the select option. In case of a hash the keys become the labels of the generated dropdown list, while the values will be values of options of the dropdown list:
452
+
453
+ g.column :column_name => 'Status', :attribute_name => 'status',
454
+ :custom_filter => {'Development' => 'development', 'Testing' => 'testing', 'Production' => 'production'}
455
+
456
+ g.column :column_name => 'Status', :attribute_name => 'status',
457
+ :custom_filter => [['Development', 'development'], ['Testing', 'testing'], ['Production', 'production']]
458
+
459
+ It is also possible to submit a array of strings or numbers, in this case every item will be used both as the value of the select option and as its label:
460
+
461
+ g.column :column_name => 'Status', :attribute_name => 'status',
462
+ :custom_filter => ['development', 'testing', 'production']
463
+
464
+
465
+ ==== :auto
466
+
467
+ <tt>:auto</tt> - a powerful option which populates the dropdown list with all unique values of the column specified by
468
+ <tt>:attribute_name</tt> and <tt>:model_class</tt>.
469
+
470
+ g.column :column_name => 'Status', :attribute_name => 'status',
471
+ :custom_filter => ['development', 'testing', 'production']
472
+
473
+ Note that in the above example all names of all possible categories will appear even if they don't appear in the current resultset.
474
+ To only show those which do appear in the resutset, use an array of symbol messages (see section 'An array of symbols').
475
+
476
+
477
+ ==== Custom filters and associations (joined tables)
478
+
479
+ In most cases custom fields are required for one-to-many and many-to-many associations.
480
+
481
+ To correctly build a filter condition foreign keys have to be matched.
482
+
483
+ For example, if there is a column:
484
+
485
+ g.column :column_name => 'Project Name', :attribute_name => 'name', :model_class => 'Project' do |task|
486
+ task.project.name if task.project
487
+ end
488
+
489
+ adding <tt>:custom_filter</tt> like this:
490
+
491
+ g.column :column_name => 'Project Name', :attribute_name => 'name', :model_class => 'Project',
492
+ :custom_filter => Project.find(:all).map{|pr| [pr.name, pr.name]} do |task|
493
+ task.project.name if task.project
494
+ end
495
+
496
+ is bad style and can fail, because the resulting condition will compare the name of the project, <tt>projects.name</tt> to a string,
497
+ and in some databases it is possible that different records (projects in our example) have the same name.
498
+
499
+ To use filter with foreign keys, we have to change the declaration of the column from <tt>projects.name</tt>, to
500
+ <tt>tasks.project_id</tt>, and build the dropdown with foreign keys as values:
501
+
502
+
503
+ g.column :column_name => 'Project Name', :attribute_name => 'tasks.project_id',
504
+ :custom_filter => Project.find(:all).map{|pr| [pr.id, pr.name]} do |task|
505
+ task.project.name if task.project
506
+ end
507
+
508
+ However, this will break the ordering of the column - the column will be ordered by the integer foreign key. To fix this,
509
+ we can override the ordering using <tt>:custom_order</tt>:
510
+
511
+ @tasks_grid = initialize_grid(Task,
512
+ :include => :project,
513
+ :custom_order => {
514
+ 'tasks.project_id' => 'projects.name'
515
+ }
516
+ )
517
+
518
+
519
+ ==== Any other symbol (method name) or an array of symbols (method names)
520
+
521
+
522
+ For one symbol (different from <tt>:auto</tt>) the dropdown list is populated by all unique values returned by the method with this name
523
+ sent to <em>all</em> ActiveRecord objects throughout all pages.
524
+
525
+ The conditions set up by the user are ignored, that is, the records used are all those found on all pages without any filters active.
526
+
527
+ For an array of symbols, the first method name is sent to the ActiveRecord
528
+ object if it responds to this method, the second method name is sent to the
529
+ returned value unless it is +nil+, and so on. In other words, a single symbol mode is the
530
+ same as an array of symbols where the array contains just one element.
531
+
532
+ g.column :column_name => 'Version', :attribute_name => 'expected_version_id', :custom_filter => [:expected_version, :to_option] do |task|
533
+ task.expected_version.name if task.expected_version
534
+ end
535
+
536
+
537
+ There are two important differences from <tt>:auto</tt>:
538
+
539
+ 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.
540
+ 2. Filtering by any option of such a custom filter will bring a non-empty list, unlike with <tt>:auto</tt>.
541
+
542
+
543
+ This mode has one major drawback - this mode requires an additional query without +offset+ and +limit+ clauses to instantiate _all_
544
+ ActiveRecord objects, and performance-wise it brings all the advantages of pagination to nothing. Thus, memory- and performance-wise this can be really bad for some queries and tables and should be used with care.
545
+
546
+
547
+ If the final method returns a atomic value like a string or an integer, it is used for both the value and the label of the select option element:
548
+
549
+ <option value="returned value">returned value</option>
550
+
551
+ However, if the retuned value is a two element array, the first element is used for the option label and the second - for the value.
552
+
553
+ Typically, a model method like the following:
554
+
555
+ def to_option
556
+ [name, id]
557
+ end
558
+
559
+ together with
560
+
561
+ :custom_filter => :to_option
562
+
563
+ would do the trick:
564
+
565
+ <option value="id">name</option>
566
+
567
+ Alternatively, a hash with the single key-value pair can be used, where the key will be used for the label, and the key - for the value:
568
+
569
+ def to_option
570
+ {name => id}
571
+ end
572
+
573
+
574
+ ==== Special treatment of values 'null' and 'not null'
575
+
576
+ Values 'null' and 'not null' in a generated custom filter are treated specially, as SQL +null+ statement and not as strings.
577
+ Value 'null' is transformed into SQL condition <tt>IS NULL</tt>, and 'not null' into <tt>IS NOT NULL</tt>
578
+
579
+ Thus, if in a filter defined by
580
+ :custom_filter => {'No' => 'null', 'Yes' => 'not null', '1' => 1, '2' => '2', '3' => '3'}
581
+ values '1', '2' and 'No' are selected (in a multi-select mode), this will result in the following SQL:
582
+
583
+ ( table.field IN ( '1', '2' ) OR table.field IS NULL )
584
+
585
+
586
+ ==== Multiple selection
587
+
588
+ By default it is possible for any dropdown list to switch between single and multiple selection modes. To only allow
589
+ single selection use <tt>:allow_multiple_selection</tt>:
590
+
591
+
592
+ g.column :column_name => 'Expected in version', :attribute_name => 'expected_version_id',
593
+ :custom_filter => [:expected_version, :to_option], :allow_multiple_selection => false do |task|
594
+ ...
595
+ end
596
+
597
+
598
+ === Defaults
599
+
600
+ Default values like can be changed in <tt>config/initializers/wice_grid_config.rb</tt>, as well grid labels and paths to some images.
601
+
602
+
603
+
604
+ === Submit/Reset buttons
605
+ View helper +submit_grid_javascript+ returns javascript which applies current filters.
606
+ View helper +reset_grid_javascript+ returns javascript which resets the grid, clearing the state of filters.
607
+ This allows to create your own Submit and Reset buttons anywhere on the page with the help of +button_to_function+:
608
+
609
+ <%= button_to_function "Submit", submit_grid_javascript(@grid) %>
610
+ <%= button_to_function "Reset", reset_grid_javascript(@grid) %>
611
+
612
+ To complement this feature there are two parameters in the +grid+ helper <tt>:hide_submit_button</tt> and <tt>:hide_reset_button</tt> which
613
+ hide default buttons in the grid if set to true.
614
+
615
+
616
+ === Auto-reloading filters
617
+
618
+ It is possible to configure a grid to reload itself once a filter has been changed. It works with all
619
+ filter types including the JS calendar, the only exception is the standard Rails date/datetime filters.
620
+
621
+ Use option <tt>:auto_reload</tt> in the column definiton:
622
+
623
+
624
+ <%= grid(@tasks_grid, :show_filters => :always, :hide_submit_button => true) do |g|
625
+
626
+ # String
627
+ g.column :column_name => 'Title', :attribute_name => 'title', :auto_reload => true
628
+
629
+ # Boolean
630
+ g.column :column_name => 'Archived', :attribute_name => 'archived', :auto_reload => true
631
+
632
+ # Custom (dropdown)
633
+ g.column :column_name => 'Status', :attribute_name => 'status_id', :custom_filter => Status.to_dropdown, :auto_reload => true do |task|
634
+ task.status.name if task.status
635
+ end
636
+
637
+ # Datetime
638
+ g.column :column_name => 'Added', :attribute_name => 'created_at', :auto_reload => true, :helper_style => :calendar do |task|
639
+ task.created_at.to_s(:short)
640
+ end
641
+
642
+ end -%>
643
+
644
+ To make this behavior default change constant +AUTO_RELOAD+ in the configuration file.
645
+
646
+ === Styling the grid
647
+
648
+ ==== Predefined css classes used by the grid
649
+
650
+ +td+ tags are assigned two styles automatically - +sorted+ if the column is the one by which the grid is ordered,
651
+ and +active_filter+ if the column's filter is on.
652
+
653
+ Odd and even +tr+ tags are assigned styles +odd+ and +even+, correspondingly.
654
+
655
+ For other classes see <tt>wice_grid.css</tt>. Feel free to customize it according to your needs.
656
+
657
+ ==== Adding classes and styles
658
+
659
+ The +grid+ helper accepts parameter <tt>:table_html_attrs</tt> which is a hash of HTML
660
+ attributes for the table tag.
661
+
662
+ Another +grid+ parameter is <tt>header_tr_html_attrs</tt> which is a hash of HTML attributes to be added to the first +tr+ tag (or two first +tr+'s if the filter row is present).
663
+
664
+ <tt>:td_html_attrs</tt> is a parameter for the +column+ method setting HTML attributes of +td+ tags for a certain column.
665
+
666
+ ==== Adding classes and styles dynamically
667
+
668
+ WiceGrid offers ways to dynamically add classes and styles to +TR+ and +TD+ based on the current ActiveRecord instance.
669
+
670
+
671
+ For <tt><TD></tt>, let the +column+ return an array where the first item is the usual
672
+ string output whole the second is a hash of HTML attributes to be added for the <tt><td></tt> tag of the current cell:
673
+
674
+ g.column do |portal_application|
675
+ css_class = portal_application.public? ? 'public' : 'private'
676
+ [portal_application.name, {:class => css_class}]
677
+ end
678
+
679
+ For adding classes/styles to <tt><TR></tt> use special clause +row_attributes+ , similar to +column+, only returning a hash:
680
+
681
+ <%= grid(@versions_grid) do |g|
682
+ g.row_attributes do |version|
683
+ if version.in_production?
684
+ {:style => 'background-color: rgb(255, 255, 204);'}
685
+ end
686
+ end
687
+
688
+ g.column{|version| ... }
689
+ g.column{|version| ... }
690
+ end -%>
691
+
692
+ Naturally, there can be only one +row_attributes+ definition for a WiceGrid instance.
693
+
694
+ Various classes do not overwrite each other, instead, they are concatenated.
695
+
696
+ WiceGrid icons are in directory <tt>public/images/icons/grid/</tt>.
697
+
698
+
699
+ === Adding rows to the grid
700
+
701
+ It is possible to add your own handcrafted HTML after and/or before each grid row. This works similar to +row_attributes+, by adding blocks +after_row+ and +before_row+:
702
+
703
+ <%= grid(@tasks_grid) do |g|
704
+ g.before_row do |task|
705
+ if task.active?
706
+ "<tr><td colspan=\"10\">Custom line for #{t.name}</td></tr>" # this would add a row
707
+ # before every active task row
708
+ else
709
+ nil
710
+ end
711
+ end
712
+ .......
713
+ end %>
714
+
715
+ It is up for the developer to return the correct HTML code, or return +nil+ if no row is needed for this record. Naturally, there is only one +before_row+ definition and one +after_row+ definition for a WiceGrid instance.
716
+
717
+ A real life example might be some enterprisy tables inside a table.
718
+
719
+ === Rendering a grid without records
720
+
721
+ If the grid does not contain any records to show, it is possible show some alternative view instead of an empty grid. Bear in mind that if the user
722
+ sets up the filters in such a way that the selection of records is empty, this will still render the grid and it will be possible to
723
+ reset the grid clicking on the Reset button. Thus, this only works if the initial number of records is 0.
724
+
725
+ <%= grid(@grid) do |g|
726
+
727
+ g.blank_slate do
728
+ "There are no records"
729
+ end
730
+
731
+ g.column do |product|
732
+ ...
733
+ end
734
+ end -%>
735
+
736
+ There are two alternative ways to do the same, submitting a string to +blank_slate+:
737
+
738
+ g.blank_slate "some text to be rendered"
739
+
740
+ Or a partial:
741
+
742
+ g.blank_slate :partial => "partial_name"
743
+
744
+
745
+ === Localization
746
+
747
+ Versions of WiceGrid newer than version 0.5 support localization via Rails I18n, however, without breaking
748
+ compatibility with pre-I18n versions of Rails (2.1.0 and older).
749
+
750
+ Running
751
+
752
+ ./script/generate wice_grid_assets
753
+
754
+ will copy file <tt>wice_grid.yml</tt> to <tt>config/locales</tt>.
755
+
756
+ The current locale is taken from <tt>I18n.locale</tt>. The locale also propagates to the javascript calendar
757
+ widget (see <tt>Calendar.messagebundle</tt> in <tt>calendarview.js</tt> to add languages).
758
+
759
+ If the messages are not found, or I18n is missing, WiceGrid will fall back to the hardcoded messages in
760
+ <tt>config/initializers/wice_grid_config.rb</tt>.
761
+
762
+ Currently supported languages are English, Dutch, French, and Russian.
763
+
764
+ === ERB mode
765
+
766
+ The view helper can function in two different modes. These are defined by its +erb_mode+ parameter.
767
+ By default (<tt>:erb_mode => false</tt>) the view helper is a simple helper surrounded by <tt><%=</tt> and <tt>%></tt>, like in all examples
768
+ above.
769
+
770
+
771
+ The second mode (<tt>:erb_mode => true</tt>) is called <em>ERB mode</em> and it allows to embed any ERB content inside blocks,
772
+ which is basically the style of the
773
+ <tt>form_for</tt> helper, only <tt>form_for</tt> takes one block, while inside the <tt>grid</tt> block there are other method calls taking
774
+ blocks as parameters:
775
+
776
+ <% grid(@countries_grid, :erb_mode => true) do |g| %>
777
+
778
+ <% g.column :column_name => 'Name', :attribute_name => 'name' do |country| %>
779
+ <b>Name: <%= link_to(country.name, country_path(country)) %></b>
780
+ <% end %>
781
+
782
+ <% g.column :column_name => 'Numeric Code', :attribute_name => 'numeric_code' do |country| %>
783
+ <i>Numeric Code: <%= country.numeric_code %></i>
784
+ <% end %>
785
+
786
+ <% end -%>
787
+
788
+ This mode can be usable if you like to have much HTML code inside cells.
789
+
790
+ Please remember that in this mode the helper opens with <tt><%</tt> instead of <tt><%=</tt>, similar to <tt>form_for</tt>.
791
+
792
+ The default value for <tt>:show_filters</tt> can be changed in <tt>config/initializers/wice_grid_config.rb</tt>.
793
+
794
+ === Action Column
795
+
796
+ It is easy to add a column with checkboxes for each record. This is useful for actions with multiple records,
797
+ for example, deleting selected records. Please note that +action_column+ only creates the checkboxes and the
798
+ 'Select All' and 'Deselect All' buttons, and the form itself as well as processing the parameters should be
799
+ taken care of by the application code.
800
+
801
+ <%= grid(@tasks_grid, :show_filters => :always) do |g|
802
+
803
+ ...
804
+
805
+ g.action_column
806
+
807
+ ...
808
+
809
+ end -%>
810
+
811
+ By default the name of the HTTP parameter follows pattern <tt>"#{grid_name}[#{param_name}][]"</tt>, thus
812
+ <tt>params[grid_name][param_name]</tt> will contain an array of object IDs.
813
+
814
+ See the documentation for more details.
815
+
816
+
817
+ === Integration of the grid with other forms on page
818
+
819
+ Imagine that the user should be able to change the behavior of the grid using some other control on the page, and not a grid filter.
820
+
821
+ For example, on a page showing tasks,
822
+ change between 'Show active tasks' to 'Show archived tasks' using a dropdown box.
823
+ WiceGrid allows to keep the status of the grid with all the filtering and sorting
824
+ using helper +dump_filter_parameters_as_hidden_fields+ which takes a grid object and dumps all current sorting and filtering parameters as
825
+ hidden fields. Just include <tt>dump_filter_parameters_as_hidden_fields(@grid)</tt> inside your form, and the newly rendered grid will
826
+ keep ordering and filtering.
827
+
828
+ <% form_tag('', :method => :get) do %>
829
+ <%= dump_filter_parameters_as_hidden_fields(@tasks_grid) %>
830
+ <%= select_tag 'archived',
831
+ options_for_select([['View active tasks', 0], ['View archived tasks', 1]], @archived ? 1 : 0),
832
+ :onchange => 'this.form.submit()' %>
833
+ <% end -%>
834
+
835
+
836
+ === Javascript Calendar for Date and DateTime Filters.
837
+
838
+ Standard Rails Date and DateTime helpers are a set of dropdown lists, and while this is practical,
839
+ displaying two Date or especially DateTime
840
+ helpers takes too much space on a page and is in general confusing.
841
+
842
+ To solve this, WiceGrid includes a second variant of Date/DateTime filters based on a Javascript calendar.
843
+
844
+ In the Prototype mode the plugin uses a forked[http://github.com/leikind/calendarview] version of Calendarview[http://github.com/jsmecham/calendarview]. It is bundled with the plugin and the generator task
845
+ +wice_grid_assets_prototype+ will install al necessary assets.
846
+
847
+ The jQuery version uses jQuery datepicker[http://jqueryui.com/demos/datepicker/]. Because this is part
848
+ of the standard jQuery codebase, it is not bundled together with the plugin, and it is the responsibility
849
+ of the programmer to include all necessary assets including localization files if the application is
850
+ multilingual.
851
+
852
+ It is always possible to fall back to simple dropdown lists that the standard date/datetime Rails helpers use.
853
+
854
+ JQUERY DATEPICKER IS ONLY USED FOR DATE FILTERS AT THE MOMENT. DATETIME FILTERS FALL BACK TO THE DEFAULT
855
+ RAILS DATE/DATETIME HELPERS.
856
+
857
+
858
+ Calendar based helpers are enabled by default, but it's possible to change it in
859
+ <tt>config/initializers/wice_grid_config.rb</tt>, variable HELPER_STYLE.
860
+
861
+ The flavor of the date filter can also be changed on per-column basis:
862
+
863
+ g.column :column_name => 'Due Date', :attribute_name => 'due_date', :helper_style => :calendar do |task|
864
+ task.due_date.to_s(:short) if task.due_date
865
+ end
866
+
867
+ g.column :column_name => 'Updated', :attribute_name => 'updated_at', :helper_style => :standard do |task|
868
+ task.created_at.to_s(:short)
869
+ end
870
+
871
+ Constants +DATE_FORMAT+ and +DATETIME_FORMAT+ define the format of dates the user will see, as well as the
872
+ format of the string sent in a HTTP parameter.
873
+
874
+ You can change the constants in
875
+ <tt>config/initializers/wice_grid_config.rb</tt>. Doing so, make sure that lamdbas defined
876
+ in +DATETIME_PARSER+ and +DATE_PARSER+ return valid DateTime and Date objects. The format
877
+ by default is <tt>%Y-%m-%d</tt> for the Date and the date part of DateTime, and <tt>Time.zone.parse</tt>
878
+ and <tt>Date.parse</tt> handle it.
879
+ Make sure it stays so.
880
+
881
+ jQuery +datepicker+ uses a different format flavor, therefore there is an additional constant
882
+ +DATE_FORMAT_JQUERY+. While +DATE_FORMAT_JQUERY+ is fed to +datepicker+, +DATE_FORMAT+ is still used
883
+ for presenting initial date values in filters, so make sure that +DATE_FORMAT_JQUERY+ and +DATE_FORMAT+
884
+ result in an identical date representation.
885
+
886
+
887
+ === Show All Records
888
+
889
+ It is possible to switch to the All Records mode clicking on link "show all" in the bottom right corner. This functionality should be used
890
+ with care. To turn this mode off for all grid instances, change constant +ALLOW_SHOWING_ALL_QUERIES+ in
891
+ <tt>config/initializers/wice_grid_config.rb</tt> to
892
+ +false+. To do so for a specific grid, use initializer parameter <tt>:allow_showing_all_records</tt>.
893
+
894
+ Configuration constant +START_SHOWING_WARNING_FROM+ sets the threshold number of all records after which clicking on the
895
+ link results in a javascript confirmation dialog.
896
+
897
+
898
+ === CSV Export
899
+
900
+ It is possible to export the data displayed on a grid to a CSV file. The dumped data is the current resultset with all the
901
+ current filters and sorting applied, only without the pagination constraint (i.e. all pages).
902
+
903
+ To enable CSV export add parameters +enable_export_to_csv+ and +csv_file_name+ to the initialization of the grid:
904
+
905
+ @projects_grid = initialize_grid(Project,
906
+ :include => [:customer, :supplier],
907
+ :name => 'g2',
908
+ :enable_export_to_csv => true,
909
+ :csv_file_name => 'projects'
910
+ )
911
+
912
+ +csv_file_name+ is the name of the downloaded file. This parameter is optional, if it is missing, the name of the grid is used instead.
913
+ The export icon will appear at the bottom right corner of the grid.
914
+
915
+ Next, each grid view helper should be placed in a partial of its own, requiring it from the master template for the usual flow. There must be no
916
+ HTML or ERB code in this partial except for the grid helper.
917
+
918
+ By convention the name of such a partial follows the following pattern:
919
+
920
+ _GRID_NAME_grid.html.erb
921
+
922
+ In other words, a grid named +tasks+ is expected to be found in a template called <tt>_tasks_grid.html.erb</tt> (remember that the default name of grids is '+grid+'.)
923
+
924
+ Next, method +export_grid_if_requested+ should be added to the end of each action containing grids with enabled CSV export.
925
+
926
+ +export_grid_if_requested+ intercepts CSV export requests and evaluates the partial with the required grid helper.
927
+
928
+ The naming convention for grid partials can be easily overridden by supplying a hash parameter to +export_grid_if_requested+
929
+ where each key is the name of a grid, and the value is the name of the template (like it is specified for +render+, i.e.
930
+ without '_' and extensions):
931
+
932
+
933
+ export_grid_if_requested('g1' => 'tasks_grid', 'g2' => 'projects_grid')
934
+
935
+ If the request is not a CSV export request, +export_grid_if_requested+ does nothing and returns +false+, if it is a CSV export request,
936
+ the method returns +true+.
937
+
938
+
939
+ If the action has no explicit +render+ call, it's OK to just place +export_grid_if_requested+ as the last line of the action:
940
+
941
+ def index
942
+
943
+ @tasks_grid = initialize_grid(Task,
944
+ :name => 'g1',
945
+ :enable_export_to_csv => true,
946
+ :csv_file_name => 'tasks'
947
+ )
948
+
949
+ @projects_grid = initialize_grid(Project,
950
+ :name => 'g2',
951
+ :enable_export_to_csv => true,
952
+ :csv_file_name => 'projects'
953
+ )
954
+
955
+ export_grid_if_requested
956
+ end
957
+
958
+
959
+ Otherwise, to avoid double rendering, use the return value of the method to conditionally call your +render+ :
960
+
961
+
962
+ def index
963
+
964
+ ...........
965
+
966
+ export_grid_if_requested || render(:action => 'my_template')
967
+ end
968
+
969
+
970
+ It's also possible to supply a block which will be called if no CSV export is requested:
971
+
972
+ def index
973
+
974
+ ...........
975
+
976
+ export_grid_if_requested do
977
+ render(:action => 'my_template')
978
+ end
979
+ end
980
+
981
+
982
+ If a column has to be excluded from the CSV export,
983
+ set +column+ parameter +in_csv+ to +false+:
984
+
985
+ g.column :in_csv => false do |task|
986
+ link_to('Edit', edit_task_path(task))
987
+ end
988
+
989
+ If a column must appear both in HTML and CSV, but with different output,
990
+ duplicate the column and use
991
+ parameters +in_csv+ and +in_html+ to include one of them to html output only, the other to CSV only:
992
+
993
+
994
+ # html version
995
+ g.column :column_name => 'Title', :attribute_name => 'title', :in_csv => false do |task|
996
+ link_to('Edit', edit_task_path(task.title))
997
+ end
998
+ # plain text version
999
+ g.column :column_name => 'Title', :in_html => false do |task|
1000
+ task.title
1001
+ end
1002
+
1003
+ The default field separator in generated CSV is a comma, but it's possible to override this submitting a
1004
+ string to the <tt>:enable_export_to_csv</tt> parameter:
1005
+
1006
+ @products_grid = initialize_grid(Product,
1007
+ :enable_export_to_csv => ';',
1008
+ :csv_file_name => 'products')
1009
+
1010
+
1011
+ === Detached Filters
1012
+
1013
+ It is possible to detach filters and place them anywhere on the page. To do so, use parameter <tt>:detach_with_id</tt> for a column whose filter needs to be detached, with an arbitrary string or a symbol value which will be used later to identify the filter. As soon as there is one column with <tt>:detach_with_id</tt>, the behavior of the +grid+ helper changes - it becomes an <i>initializer</i> of the grid and doesn't output any HTML code. To render the grid, use <tt>grid</tt> for the second time without the block. To render a detached output filter, use helper <tt>grid_filter(grid_object, detached_filter_key)</tt>:
1014
+
1015
+ <%= grid(@tasks_grid, :show_filters => :always) do |g|
1016
+
1017
+ g.column :column_name => 'Title', :attribute_name => 'title', :detach_with_id => :title_filter do |task|
1018
+ link_to('Edit', edit_task_path(task.title))
1019
+ end
1020
+
1021
+ g.column :column_name => 'Archived', :attribute_name => 'archived', :detach_with_id => :archived_filter do |task|
1022
+ task.archived? ? 'Yes' : 'No'
1023
+ end
1024
+
1025
+ g.column :column_name => 'Added', :attribute_name => 'created_at', :detach_with_id => :created_at_filter do |task|
1026
+ task.created_at.to_s(:short)
1027
+ end
1028
+
1029
+ end -%>
1030
+
1031
+ <% # rendering filter with key :title_filter %>
1032
+ <%= grid_filter @tasks_grid, :title_filter %>
1033
+
1034
+ <% # rendering filter with key :archived_filter %>
1035
+ <%= grid_filter @tasks_grid, :archived_filter %>
1036
+
1037
+ <% # rendering filter with key :created_at_filter %>
1038
+ <%= grid_filter @tasks_grid, :created_at_filter %>
1039
+
1040
+ <% # Rendering the grid body %>
1041
+ <%= grid(@tasks_grid) %>
1042
+
1043
+ It is important that the grid initializer goes first, the order of +grid_filter+ and the second call to +grid+ is of no importance.
1044
+
1045
+ Using custom submit and reset buttons together with <tt>:hide_submit_button => true</tt> and <tt>:hide_reset_button => true</tt>
1046
+ allows to completely get rid
1047
+ of the default filter row and the default icons (see section 'Submit/Reset Buttons').
1048
+
1049
+ For CSV export will continue functioning, just make sure the first call to +grid+ is still in the template of its own and
1050
+ is inside of <tt><%= %></tt>, because when CSV is requested, the first +grid+ works in the old fashioned way producing CSV formatted output.
1051
+
1052
+ This feature also works with <tt>:erb_mode => true</tt>.
1053
+
1054
+ If a column was declared with <tt>:detach_with_id</tt>, but never output with +grid_filter+, filtering the grid in development mode will result
1055
+ in an warning javascript message and the missing filter will be ignored. There is no such message in production.
1056
+
1057
+ <tt>grid</tt> parameter <tt>:show_filter</tt> must not be set to <tt>:no</tt> for detached filters to work.
1058
+
1059
+ ==== How Does It Work? (For the interested)
1060
+
1061
+ When there is at least one column with <tt>:detach_with_id</tt>, the generated HTML code is stored in a buffer, code for detached filters is stored
1062
+ in buffers of their own identified by the given IDs, and nothing is returned to the view.
1063
+ When the helper is called for the second time, the buffer outputs its content.
1064
+ In a similar fashion, the +grid_filter+ helper outputs buffers for filters.
1065
+
1066
+ === Compatibility with Rails Asset Caching
1067
+
1068
+ Helpers +names_of_wice_grid_stylesheets+ and +names_of_wice_grid_javascripts+ return names of stylesheet and javascript files and can be used with
1069
+ +stylesheet_link_tag+ and +javascript_include_tag+ with <tt>:cache => true</tt>.
1070
+ Using this trick you have to deal with the parameters correctly, mind that Prototype has to be loaded before WiceGrid javascripts:
1071
+
1072
+ <%= stylesheet_link_tag *(['core', 'modalbox'] + names_of_wice_grid_stylesheets + [ {:cache => true}]) %>
1073
+ <%= javascript_include_tag *([:defaults] + names_of_wice_grid_javascripts + [ 'ui', 'swfobject', {:cache => true}]) %>
1074
+
1075
+
1076
+ === Access to Records From Outside The Grid
1077
+
1078
+ There are two ways you can access the records outside the grid - using methods of the WiceGrid object and using callbacks.
1079
+
1080
+ ==== Accessing Records Via The WiceGrid Object
1081
+
1082
+ Method +current_page_records+ returns exactly the same list of objects displayed on page:
1083
+
1084
+ <%= grid(@tasks_grid) do |g|
1085
+ ...
1086
+ end -%>
1087
+
1088
+ <p>
1089
+ IDs of records on the current page:
1090
+ <%= @tasks_grid.current_page_records.map(&:id).to_sentence %>
1091
+ </p>
1092
+
1093
+ Method +all_pages_records+ returns a list of objects browsable through all pages with the current filters:
1094
+
1095
+ <%= grid(@tasks_grid) do |g|
1096
+ ...
1097
+ end -%>
1098
+
1099
+ <p>
1100
+ IDs of all records:
1101
+ <%= @tasks_grid.all_pages_records.map(&:id).to_sentence %>
1102
+ </p>
1103
+
1104
+ Mind that this helper results in an additional SQL query.
1105
+
1106
+
1107
+ Because of the current implementation of WiceGrid these helpers work only after the declaration of the grid in the view.
1108
+ This is due to the lazy nature of WiceGrid - the actual call to the database is made during the execution of
1109
+ the +grid+ helper, because to build the correct query columns declarations are required.
1110
+
1111
+ ==== Accessing Records Via Callbacks
1112
+
1113
+ It is possible to set up callbacks which are executed from within the plugin just after the call to the database.
1114
+ The callbacks are called before rendering the grid cells, so the results of this processing can be used in the grid.
1115
+ There are 3 ways you can set up such callbacks:
1116
+
1117
+ Via a lambda object:
1118
+
1119
+ def index
1120
+ @tasks_grid = initialize_grid(Task,
1121
+ :with_paginated_resultset => lambda do |records|
1122
+ ...
1123
+ end
1124
+ )
1125
+ end
1126
+
1127
+ Via a symbol which is the name of a controller method:
1128
+
1129
+ def index
1130
+ @tasks_grid = initialize_grid(Task,
1131
+ :with_paginated_resultset => :process_selection
1132
+ )
1133
+ end
1134
+
1135
+ def process_selection(records)
1136
+ ...
1137
+ end
1138
+
1139
+ Via a separate block:
1140
+
1141
+ def index
1142
+ @tasks_grid = initialize_grid(Task)
1143
+
1144
+ @tasks_grid.with_paginated_resultset do |records|
1145
+ ...
1146
+ end
1147
+ end
1148
+
1149
+ There are two callbacks:
1150
+
1151
+ * <tt>:with_paginated_resultset</tt> - used to process records of the current page
1152
+ * <tt>:with_resultset</tt> - used to process all records browsable through all pages with the current filters
1153
+
1154
+ While the <tt>:with_paginated_resultset</tt> callback just receives the list of records, <tt>:with_resultset</tt>
1155
+ receives a lambda object which, when called, returns the list of all records:
1156
+
1157
+
1158
+ def index
1159
+ @tasks_grid = initialize_grid(Task)
1160
+
1161
+ @tasks_grid.with_resultset do |wrapper|
1162
+ all_records = wrapper.call
1163
+ ...
1164
+ end
1165
+ end
1166
+
1167
+
1168
+ This lazy nature exists for performance reasons.
1169
+ Reading all records leads to an additional call, and there can be cases when processing all records should be triggered
1170
+ only under certain circumstances:
1171
+
1172
+ def index
1173
+ @tasks_grid = initialize_grid(Task)
1174
+
1175
+ @tasks_grid.with_resultset do |wrapper|
1176
+ if params[:process_all_records]
1177
+ all_records = wrapper.call
1178
+ ...
1179
+ end
1180
+ end
1181
+ end
1182
+
1183
+
1184
+
1185
+ == Icons
1186
+
1187
+ Icons used by the plugin are courtesy of Mark James, the creator of the SILK icon set -
1188
+ http://www.famfamfam.com/lab/icons/silk/.