rich_table_component 0.0.1

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.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rich_table_component.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 ralibi g470
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,470 @@
1
+ # RichTableComponent
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'rich_table_component'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install rich_table_component
18
+
19
+ ## Usage
20
+
21
+
22
+ penggunaan:
23
+
24
+ CONTROLLER
25
+
26
+ rich_table_component(relation = {}, _sort_column = {}, _sort_direction = nil, pagination = true)
27
+
28
+ Parameter
29
+ relation (required)
30
+ value: ActiveRecord::Relation
31
+ description:
32
+
33
+ sort_column (optional)
34
+ value: string
35
+ default: `created_at`
36
+ description:
37
+ default kolom sorting yang diinginkan saat pertama kali ditampilkan
38
+
39
+ sort_direction (optional)
40
+ value: string
41
+ default: 'DESC'
42
+ description:
43
+ mode urutan pada sorting dari kolom default/sort_column
44
+
45
+ pagination (optional)
46
+ value: boolean
47
+ default: true
48
+ description:
49
+ Menyertakan fitur pagination jika true
50
+
51
+ contoh penggunaan pada controller:
52
+ @posts = rich_table_component Post
53
+ @posts = rich_table_component Post, sort_column: 'title', sort_direction: 'DESC'
54
+ @posts = rich_table_component Post, sort_column: 'title', sort_direction: 'DESC', pagination: false
55
+ @posts = rich_table_component Post, pagination: false
56
+ @posts = rich_table_component @q.result
57
+ @posts = rich_table_component @q.result, sort_column: 'title', sort_direction: 'DESC'
58
+ @posts = rich_table_component @q.result, sort_column: 'title', sort_direction: 'DESC', pagination: false
59
+ @posts = rich_table_component @q.result, pagination: false
60
+ @posts = rich_table_component @user.posts
61
+
62
+ instance name must plural e.g. @posts not @post
63
+
64
+ right: @posts = rich_table_component Post
65
+ WRONG: @post = rich_table_component Post
66
+
67
+
68
+
69
+
70
+
71
+
72
+ VIEW
73
+ = render 'shared/rtc/component'
74
+
75
+ Merender file shared/rtc/_component.html.haml
76
+
77
+
78
+ ##################################
79
+ PARAMETER:
80
+ ##################################
81
+
82
+ rtc_controller_name
83
+ value: string
84
+ default: controller_name atau string dari controller yang menghandle request
85
+ description:
86
+ Menentukan instance ActiveRecord::Relation yang ingin ditampilkan. Jika ingin menampilkan koleksi post melalui `PostsController#index`, maka default valuenya adalah 'posts'. Jika request bukan `controller#index` maka rtc_controller_name perlu dituliskan. Misalnya ingin menampilkan koleksi post pada halaman user pengupload atau `UsersController#show`, maka pada method `#show` perlu didefinisikan instance `@posts` dan parameter rtc_controller_name perlu dituliskan pada `render 'shared/rtc/component'` dengan value 'posts'.
87
+
88
+
89
+
90
+ rtc_partial
91
+ value: string
92
+ default: rtc_controller_name.singularize (jika default, merender file partial pada directory yang memanggil)
93
+
94
+
95
+ rtc_title
96
+ value: boolean, Hash{ title: 'Judul', wrapper: 'h1'/'h2'/'h3'/'h4' }, or string
97
+ default: true
98
+ description:
99
+
100
+
101
+
102
+ rtc_header
103
+ value: boolean
104
+ default: true
105
+ description:
106
+ Menampilkan kotak header tabel jika true, dan tidak ditampilkan jika false
107
+
108
+
109
+ rtc_column_header
110
+ value: boolean
111
+ default: true
112
+ description:
113
+ Menampilkan kotak header kolom header jika true, dan tidak ditampilkan jika false
114
+
115
+
116
+ rtc_footer
117
+ value: boolean
118
+ default: true
119
+ description:
120
+ Menampilkan kotak footer tabel jika true, dan tidak ditampilkan jika false
121
+
122
+
123
+ headers (required)
124
+ value: Array of Object
125
+ description:
126
+ Mendefinisikan atribut/kolom yang akan ditampilkan sebagai kolom header, berupa atribut pada model atau asosiasi model
127
+ example:
128
+ [:nip, :name, 'department', :nidn, :certification_number, 'user.email']
129
+
130
+
131
+ columns_width
132
+ value: Array of Integer
133
+ default: [] / empty array
134
+ description:
135
+ Mengatur ukuran tiap kolom pada tabel. Nilai array merupakan rasio ukuran, misal: [1, 2, 2] berarti [20%, 40%, 40%]
136
+
137
+
138
+ search_constraint
139
+ value: symbol
140
+ default: nil
141
+ description:
142
+ Mendefinisikan pencarian yang disediakan untuk single search pada kanan atas. Menggunakan syntax ransack
143
+ example:
144
+ :title_or_body_or_author_name_or_author_address_street_cont
145
+
146
+
147
+
148
+ advanced_search_attributes
149
+ value: Array of Object. Object can be string or symbol or hash {input: ..., params: simple_form input params}
150
+ default: nil
151
+ description:
152
+ Menampilkan element advanced search pada rtc
153
+ example:
154
+ advanced_search_attributes: [ :nidn,
155
+ :name,
156
+ :nip,
157
+ :ktp_number,
158
+ {input: 'department_id', params: department_params},
159
+ {input: :employment_status, params: {as: :select, label: 'employment_status', collection: LECTURER_EMPLOYMENT_STATUS.collect(&:reverse), prompt: :true}},
160
+ {input: :activity_status, params: {as: :select, label: 'active_status', collection: LECTURER_ACTIVE_STATUS.collect(&:reverse), prompt: :true}},
161
+ :birth_date,
162
+ 'user.email',
163
+ :birth_place,
164
+ {input: :gender, params: {as: :select, label: 'gender', collection: GENDER.collect(&:reverse), prompt: :true}},
165
+ :certification_number ]
166
+
167
+
168
+ export_attributes
169
+ value: Array of Object. Object can be string or symbol
170
+ default: nil,
171
+ description:
172
+ Menyertakan fitur export pdf dan excel dengan menspesifikasikan attribut yang ingin di-sertakan
173
+ example:
174
+ [:nip, :name, 'department', :nidn, :certification_number, 'user.email']
175
+
176
+
177
+ add_form_remote
178
+ value: boolean
179
+ default: false
180
+ description:
181
+ Menghidupkan form ajax pada button tambah jika true
182
+
183
+
184
+ rtc_empty_data_message
185
+ value: string
186
+ default: sanitize "Data #{t(rtc_controller_name)} kosong"
187
+ description:
188
+ text yang ditampilkan pada tabel jika data kosong. text dapat dalam format html
189
+
190
+
191
+ search_key
192
+ value: string
193
+ default: 'q'
194
+ description:
195
+ Param key yang dibutuhkan pada fitur pencarian menggunakan gem ransack. Jika tidak menggunakan ransack, pada controller tidak perlu memiliki instance @q
196
+
197
+
198
+ rtc_button_new
199
+ value: boolean
200
+ default: true
201
+ description:
202
+ Render default button new if true
203
+
204
+ rtc_actions
205
+ value: Element or Array of Element
206
+ default: nil
207
+ description:
208
+ Render element(s) side by side with button new
209
+
210
+
211
+ toggle_view
212
+ value: boolean
213
+ default: false
214
+ description:
215
+ render toggle rtc view. thumbnail/list
216
+
217
+
218
+ table_title
219
+ value: string
220
+ default: controller_name
221
+ description:
222
+ render table title
223
+
224
+
225
+
226
+ - rtc_selection_checkbox (To be developed)
227
+ - recapitulation_matrix (removed from rtc component to dedicated component)
228
+
229
+
230
+
231
+
232
+
233
+
234
+
235
+
236
+ CONTOH KASUS:
237
+
238
+ ===============================================================================================================================
239
+ Kasus normal ==================================================================================================================
240
+ ===============================================================================================================================
241
+
242
+ Menampilkan rtc berupa list `page` pada halaman `index` controller `PagesController` atau `PagesController#index`. RTC yang ingin ditampilkan secara lengkap (memiliki fitur advanced search dan export pdf & xls)
243
+
244
+ Yang perlu dilakukan:
245
+ 1. Controller: Pada `PagesController` pages_controller.rb dalam method `index` harus memiliki instance @q dan @pages repond dengan respond_to_remote :index, @pages
246
+
247
+ def index
248
+ ...
249
+ @q = Page.search(params[:q])
250
+ @pages = rich_table_component @q.result
251
+ respond_to_remote :index, @pages
252
+ end
253
+
254
+
255
+ 2. View: Pada view/pages/index.html.haml
256
+
257
+ = render 'shared/rtc/component',
258
+ headers: [:title, :body, ['author.name', 'author'], 'author.email', nil],
259
+ columns_width: [2, 6, 1, 1, 1],
260
+ search_constraint: :title_or_body_or_author_name_or_author_email_cont,
261
+ add_form_remote: true,
262
+ export_attributes: [:title, :body, ['author.name', 'author'], 'author.email'],
263
+ advanced_search_attributes: [ :title,
264
+ :body,
265
+ 'author.name',
266
+ 'author.email',
267
+ 'author.address.city.name']
268
+
269
+
270
+ 3. View: Pada view/pages/_page.html.haml
271
+ // Jumlah td disesuaikan dengan jumlah element pada headers, untuk kasus ini berjumlah 5
272
+ %tr
273
+ %td
274
+ = page.title
275
+ %td
276
+ = page.body
277
+ %td
278
+ = page.author.name
279
+ %td
280
+ = page.author.email
281
+ %td
282
+ = link_to delete .....
283
+
284
+
285
+ ===============================================================================================================================
286
+ Kasus me-render partial custom yang berbeda ===================================================================================
287
+ ===============================================================================================================================
288
+ Secara default baris yang dirender adalah _model.html.haml, misal _page.html.haml
289
+ untuk merender partial yang berbeda untuk model page misalnya: _subscriber_page.html.haml dapat dilakukan dengan mendefinisikan parameter `rtc_partial` dengan lokasi file partial tersebut `pages/subscriber_page`
290
+
291
+ = render 'shared/rtc/component',
292
+ headers: [:title, :body, ['author.name', 'author'], 'author.email', nil],
293
+ columns_width: [2, 6, 1, 1, 1],
294
+ search_constraint: :title_or_body_or_author_name_or_author_email_cont,
295
+ rtc_partial: 'pages/subscriber_page'
296
+
297
+ Jika pada rtc terdapat button tambah, edit, atau apapun yang mengharuskan merender kembali partial custom, maka tambahkan params rtc_partial pada:
298
+ form:
299
+ hidden_field_tag 'rtc_partial', 'home/lecturer_academic_term'
300
+
301
+ atau pada
302
+ controller:
303
+ params[:rtc_partial] = 'home/lecturer_academic_term'
304
+
305
+
306
+
307
+ ===============================================================================================================================
308
+ Kasus mengacu controller yang berbeda =========================================================================================
309
+ ===============================================================================================================================
310
+ Misal menampilkan koleksi post pada home
311
+
312
+ Controller: home#index
313
+ ...
314
+ @q = Post.search(params[:q])
315
+ @posts = rich_table_component @q.result
316
+ ...
317
+
318
+ View:
319
+ = render 'shared/rtc/component',
320
+ headers: [:title, :body, ['author.name', 'author'], 'author.email', nil],
321
+ columns_width: [2, 6, 1, 1, 1],
322
+ search_constraint: :title_or_body_or_author_name_or_author_email_cont,
323
+ rtc_controller_name: 'posts'
324
+
325
+
326
+
327
+
328
+ ===============================================================================================================================
329
+ Kasus dua atau lebih table dalam halaman yang sama ===========================================================================
330
+ ===============================================================================================================================
331
+
332
+
333
+ Misal menampilkan koleksi post pada home
334
+
335
+ Controller: home#index
336
+ ...
337
+ @q = Post.search(params[:q])
338
+ @posts = rich_table_component @q.result
339
+
340
+ @p = Post.search(params[:p], search_key: :p)
341
+ @second_posts = rich_table_component @p.result
342
+
343
+ @r = Post.search(params[:r], search_key: :r)
344
+ @third_posts = rich_table_component @p.result
345
+ ...
346
+
347
+ View:
348
+ = render 'shared/rtc/component',
349
+ headers: [:title, :body, ['author.name', 'author'], 'author.email', nil],
350
+ columns_width: [2, 6, 1, 1, 1],
351
+ search_constraint: :title_or_body_or_author_name_or_author_email_cont,
352
+ rtc_controller_name: 'posts'
353
+
354
+ = render 'shared/rtc/component',
355
+ headers: [:title, :body, ['author.name', 'author'], 'author.email', nil],
356
+ columns_width: [2, 6, 1, 1, 1],
357
+ search_constraint: :title_or_body_or_author_name_or_author_email_cont,
358
+ rtc_controller_name: 'second_posts',
359
+ rtc_partial: 'posts/second_post',
360
+ search_key: 'p'
361
+
362
+ = render 'shared/rtc/component',
363
+ headers: [:title, :body, ['author.name', 'author'], 'author.email', nil],
364
+ columns_width: [2, 6, 1, 1, 1],
365
+ search_constraint: :title_or_body_or_author_name_or_author_email_cont,
366
+ rtc_controller_name: 'third_posts',
367
+ rtc_partial: 'posts/third_post',
368
+ search_key: 'r'
369
+
370
+
371
+
372
+ ===============================================================================================================================
373
+ Kasus updating tapi bukan dari tombol edit standar (misal: approve/reject) ====================================================
374
+ ===============================================================================================================================
375
+ tambahkan class html '.edit' pada button custom
376
+ pada method terakhir yang terpanggil sebelum baris terupdate, harus respond remote to :update,
377
+ dan harus memiliki params[:rtc_partial] yang diassign di controller ATAU dilempar dari form ATAU button edit
378
+ - assign di controller: params[:rtc_partial] = 'admin/departments/student'
379
+ - assign pada form (jika melewati modal form, seperti edit pada umumnya)
380
+ simple_form_for ... do |f|
381
+ = hidden_field_tag 'rtc_partial', 'admin/departments/student'
382
+ ...
383
+ end
384
+ - assign dari button (jika tidak melewati modal)
385
+ link_to 'approve', approve_post_path(post, rtc_partial: 'admin/departments/student'), remote: :true, class: 'btn edit'
386
+
387
+ Jika model yang diupdate berbeda dengan model controllernya, misal mengupdate `post` pada halaman `UsersController#show`,
388
+ sertakan rtc_controller_name berdampingan dengan rtc_partial,
389
+ contoh:
390
+ link_to 'approve', approve_post_path(post, rtc_controller_name: 'admin/departments/student', rtc_partial: 'admin/departments/student'), remote: :true, class: 'btn edit'
391
+
392
+ =====================================
393
+ a) Aksi tidak memunculkan modal, tetapi langsung update. Dengan custom method, model yang diupdate sesuai dengan controller
394
+
395
+ View:
396
+ tambahkan class html '.edit' pada button custom
397
+
398
+ ...
399
+ = link_to 'approve', approve_post_path(post), remote: :true, class: 'btn edit'
400
+ ...
401
+
402
+ Controller
403
+
404
+ ...
405
+ def approve
406
+ # updating post
407
+ @post = Post.find(params[:id])
408
+ ...
409
+ respond_to do |format|
410
+ if @post.update_attributes(params[:post])
411
+ format_remote format, :update, @post
412
+ else
413
+ format_remote format, :edit, @post
414
+ end
415
+ end
416
+ end
417
+ ...
418
+
419
+
420
+ b) Aksi tidak memunculkan modal, tetapi langsung update. Dengan custom method, model yang diupdate BERBEDA dengan controller.
421
+ rtc_controller_name & rtc_partial langsung ditambahkan pada link
422
+
423
+ View:
424
+ tambahkan class html '.edit' pada button custom
425
+
426
+ ...
427
+ = link_to 'approve', approve_post_path(post, rtc_controller_name: 'posts', rtc_partial: 'users/post'), remote: :true, class: 'btn edit'
428
+ ...
429
+
430
+ Controller
431
+
432
+ ...
433
+ def approve
434
+ # updating post
435
+ @post = Post.find(params[:id])
436
+ ...
437
+ respond_to do |format|
438
+ if @post.update_attributes(params[:post])
439
+ format_remote format, :update, @post
440
+ else
441
+ format_remote format, :edit, @post
442
+ end
443
+ end
444
+ end
445
+ ...
446
+
447
+
448
+
449
+
450
+
451
+
452
+
453
+
454
+
455
+ register mime xls
456
+ create ability.rb
457
+ define current_user in application_controller if not using devise, then set as helper_method
458
+ define method is? in user.rb
459
+
460
+ rtc_button_new hanya true, false
461
+
462
+
463
+
464
+ ## Contributing
465
+
466
+ 1. Fork it
467
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
468
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
469
+ 4. Push to the branch (`git push origin my-new-feature`)
470
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,3 @@
1
+ module RichTableComponent
2
+ VERSION = "0.0.1"
3
+ end