trk_datatables 0.2.13 → 0.2.15
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.
- checksums.yaml +4 -4
- data/Gemfile.lock +29 -23
- data/README.md +160 -24
- data/lib/trk_datatables/active_record.rb +5 -1
- data/lib/trk_datatables/base_helpers.rb +12 -0
- data/lib/trk_datatables/dt_params.rb +4 -0
- data/lib/trk_datatables/version.rb +1 -1
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0d7a8767e7c4b77bbdd9d47b478de7b6b3dd456c449e83e50a70c4b674121fa7
|
4
|
+
data.tar.gz: aedc1d098b6ddd648a7b18fbf99cc88dede84f63a63c7ececa704fcb8bb0d018
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 890f228e839a1e25ab96db11cf92c2b9a328be57f49e202ad77d2071b2c4ccdec65c31bcb57f5743c3fabbca00cb0f941c9df15364025413a73616e23b906095
|
7
|
+
data.tar.gz: 9a6744711a901ea792c54633f4eac570a36c9547f8e86f9d112f4ed175d5f7838f33ccb2b33b529cde15f82bd7184edaec881056afb88f183b1df203b8139fa9
|
data/Gemfile.lock
CHANGED
@@ -1,39 +1,45 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
trk_datatables (0.2.
|
4
|
+
trk_datatables (0.2.15)
|
5
5
|
activesupport
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: https://rubygems.org/
|
9
9
|
specs:
|
10
|
-
activemodel (6.
|
11
|
-
activesupport (= 6.
|
12
|
-
activerecord (6.
|
13
|
-
activemodel (= 6.
|
14
|
-
activesupport (= 6.
|
15
|
-
activesupport (6.
|
10
|
+
activemodel (6.1.7.2)
|
11
|
+
activesupport (= 6.1.7.2)
|
12
|
+
activerecord (6.1.7.2)
|
13
|
+
activemodel (= 6.1.7.2)
|
14
|
+
activesupport (= 6.1.7.2)
|
15
|
+
activesupport (6.1.7.2)
|
16
16
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
17
|
-
i18n (>=
|
18
|
-
minitest (
|
19
|
-
tzinfo (~>
|
20
|
-
zeitwerk (~> 2.
|
21
|
-
byebug (11.
|
22
|
-
concurrent-ruby (1.
|
23
|
-
database_cleaner (
|
24
|
-
|
17
|
+
i18n (>= 1.6, < 2)
|
18
|
+
minitest (>= 5.1)
|
19
|
+
tzinfo (~> 2.0)
|
20
|
+
zeitwerk (~> 2.3)
|
21
|
+
byebug (11.1.3)
|
22
|
+
concurrent-ruby (1.2.0)
|
23
|
+
database_cleaner (2.0.1)
|
24
|
+
database_cleaner-active_record (~> 2.0.0)
|
25
|
+
database_cleaner-active_record (2.0.1)
|
26
|
+
activerecord (>= 5.a)
|
27
|
+
database_cleaner-core (~> 2.0.0)
|
28
|
+
database_cleaner-core (2.0.1)
|
29
|
+
i18n (1.12.0)
|
25
30
|
concurrent-ruby (~> 1.0)
|
26
|
-
|
31
|
+
mini_portile2 (2.8.1)
|
32
|
+
minitest (5.17.0)
|
27
33
|
minitest-color (0.0.2)
|
28
34
|
minitest (~> 5)
|
29
|
-
pg (1.
|
35
|
+
pg (1.4.5)
|
30
36
|
rake (10.5.0)
|
31
|
-
sqlite3 (1.
|
32
|
-
|
33
|
-
timecop (0.9.
|
34
|
-
tzinfo (
|
35
|
-
|
36
|
-
zeitwerk (2.
|
37
|
+
sqlite3 (1.6.0)
|
38
|
+
mini_portile2 (~> 2.8.0)
|
39
|
+
timecop (0.9.6)
|
40
|
+
tzinfo (2.0.6)
|
41
|
+
concurrent-ruby (~> 1.0)
|
42
|
+
zeitwerk (2.6.6)
|
37
43
|
|
38
44
|
PLATFORMS
|
39
45
|
ruby
|
data/README.md
CHANGED
@@ -96,7 +96,7 @@ module.exports = environment
|
|
96
96
|
<%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
|
97
97
|
<%# we need stylesheet for production server, locally it could work without stylesheet_pack_tag even in production mode %>
|
98
98
|
<%= stylesheet_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
|
99
|
-
<%# we use jQuery from wepbacker so asset pipeline should be included later %>
|
99
|
+
<%# we use jQuery from wepbacker so asset pipeline should be included later if you use asset pipeline %>
|
100
100
|
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
|
101
101
|
```
|
102
102
|
|
@@ -106,7 +106,7 @@ Than add a gem and sample PostsDatatable
|
|
106
106
|
# Gemfile
|
107
107
|
gem 'trk_datatables'
|
108
108
|
|
109
|
-
# in console
|
109
|
+
# in console you can use rails generator to generate app/datatables/xxx_datatable.rb
|
110
110
|
bundle
|
111
111
|
rails g trk_datatables post
|
112
112
|
vi app/datatables/posts_datatable.rb
|
@@ -153,7 +153,7 @@ class PostsDatatable < TrkDatatables::ActiveRecord
|
|
153
153
|
Post.left_joins(:user)
|
154
154
|
end
|
155
155
|
|
156
|
-
def rows(filtered)
|
156
|
+
def rows(filtered) # rubocop:disable Metrics/MethodLength
|
157
157
|
filtered.map do |post|
|
158
158
|
[
|
159
159
|
@view.link_to(post.title, post),
|
@@ -217,6 +217,9 @@ method.
|
|
217
217
|
class PostsDatatable < TrkDatatables::ActiveRecord
|
218
218
|
def global_search_columns
|
219
219
|
# in addition to columns those fields will be used to match global search
|
220
|
+
# instead Post.all you should use Post.joins(:user) or
|
221
|
+
# Post.left_joins(:user) if user is optional. For has_many relations you
|
222
|
+
# need to join them since you will get multiple table rows
|
220
223
|
%w[posts.body users.name]
|
221
224
|
end
|
222
225
|
end
|
@@ -256,7 +259,9 @@ For specific columns you can use following keys
|
|
256
259
|
* `order: false` disable ordering for this column
|
257
260
|
* `select_options: Post.statuses` generate select box instead of text input
|
258
261
|
* `predefined_ranges: {}` for datetime fiels add ranges to pick up from
|
259
|
-
* `hide: true` hide column with display none, for example `{ hide:
|
262
|
+
* `hide: true` hide column with display none, for example `{ hide:
|
263
|
+
@view.params[:user_id].present? }`, note that you should send that column data
|
264
|
+
anyway, just it will not be visible
|
260
265
|
* `class_name: 'Admin::User'` use different class name than
|
261
266
|
`table_name.classify` (in this case of `admin_users` will be `AdminUser`)
|
262
267
|
* `column_type_in_db` one of the: `:string`, `:integer`, `:date`, `:datetime`,
|
@@ -391,6 +396,15 @@ nor in Ruby code.
|
|
391
396
|
end
|
392
397
|
```
|
393
398
|
|
399
|
+
You can access filtered items for example
|
400
|
+
```
|
401
|
+
def totals
|
402
|
+
filter_by_columns all_items
|
403
|
+
filter_by_search_all filter_by_columns all_items # this is actually filtered_items
|
404
|
+
order_and_paginate_items filter_by_search_all filter_by_columns all_items # this is ordered_paginated_filtered_items
|
405
|
+
end
|
406
|
+
```
|
407
|
+
|
394
408
|
### Values calculated in database
|
395
409
|
|
396
410
|
There are three types of calculated values (new custom fields that are
|
@@ -402,22 +416,64 @@ generated based on other columns):
|
|
402
416
|
posts_count FROM "users" LEFT OUTER JOIN "posts" ON "posts"."user_id" =
|
403
417
|
"users"."id" GROUP BY users.id`
|
404
418
|
|
419
|
+
Simple calculations and subqueries works fine, you can search and order by them.
|
420
|
+
Note that when you use join with other table, than you should group by all
|
421
|
+
columns that you have used as columns which can be ordered by, for example
|
422
|
+
```
|
423
|
+
# app/datatables/member_profiles_datatable.rb
|
424
|
+
def columns
|
425
|
+
{
|
426
|
+
'member_profiles.full_name': {},
|
427
|
+
'users.email': {},
|
428
|
+
'users.current_sign_in_at': {},
|
429
|
+
}
|
430
|
+
end
|
431
|
+
def all_items
|
432
|
+
MemberProfile
|
433
|
+
.joins(:user)
|
434
|
+
.group("member_profiles.id")
|
435
|
+
.group("users.email")
|
436
|
+
.group("users.current_sign_in_at")
|
437
|
+
end
|
438
|
+
```
|
439
|
+
otherwise the error like
|
440
|
+
`PG::GroupingError: ERROR: column "users.current_sign_in_at" must appear in the
|
441
|
+
GROUP BY clause or be used in an aggregate function`
|
442
|
+
You should test by searching and selecting each column as sortable column.
|
443
|
+
```
|
444
|
+
# test/controllers/admin/member_profiles_controller_test.rb
|
445
|
+
test "#search when order by is by each defined column" do
|
446
|
+
columns = MemberProfilesDatatable.new(OpenStruct.new(params: {})).columns
|
447
|
+
|
448
|
+
columns.each_with_index do |_column_key_option, index|
|
449
|
+
post search_superadmin_member_profiles_path(search: { value: "foo" }, order: { "0": { column: index, dir: "asc" } })
|
450
|
+
assert_response :success
|
451
|
+
end
|
452
|
+
end
|
453
|
+
```
|
454
|
+
|
405
455
|
Since in SQL you can not use aggregate functions in WHERE (we should repeat
|
406
456
|
calculation and subqueries), currently TrkDatatables does not support using
|
407
|
-
aggregate functions since it requires implementation of `HAVING`
|
408
|
-
disable search and order for those fields with
|
409
|
-
`'users.posts_count': { search: false, order: false }`).
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
|
457
|
+
aggregate functions since it requires implementation of `HAVING` so when you use
|
458
|
+
aggregate functions you need to disable search and order for those fields with
|
459
|
+
aggregate functions `'users.posts_count': { search: false, order: false }`).
|
460
|
+
|
461
|
+
You can use concatenation aggregate function: in postgres
|
462
|
+
`STRING_AGG(comments.body, ' ') comments_body`, in mysql `GROUP_CONCAT` so in
|
463
|
+
this case we search on real columns. For example let's we have
|
464
|
+
```
|
465
|
+
Post
|
466
|
+
.select(%(posts.*, GROUP_CONCAT(comments.body) AS comments_body))
|
467
|
+
.left_outer_joins(:comments)
|
468
|
+
.group('posts.id')
|
469
|
+
```
|
470
|
+
and that we have a row `postName, comment1, comment2` than when we searh for
|
471
|
+
`comment2` we will get a row `postName, comment2`.
|
416
472
|
|
417
473
|
Simple calculations and subqueries works fine, just you have to use public
|
418
474
|
method to define calculation (that method is also used in filtering). Name of
|
419
|
-
method is the same as column name `title_and_body` or `comments_count`. For
|
420
|
-
name you should use one of:
|
475
|
+
method is the same as column name `title_and_body` or `comments_count`. For
|
476
|
+
table name you should use one of:
|
421
477
|
`:string_calculated_in_db`, `:integer_calculated_in_db`,
|
422
478
|
`:date_calculated_in_db`, `:datetime_calculated_in_db` or
|
423
479
|
`:boolean_calculated_in_db`.
|
@@ -487,7 +543,7 @@ class MostLikedPostsDatatable < TrkDatatables::ActiveRecord
|
|
487
543
|
Post.select(%(
|
488
544
|
posts.*,
|
489
545
|
#{title_and_body} AS title_and_body,
|
490
|
-
|
546
|
+
#{comments_count} AS comments_count
|
491
547
|
))
|
492
548
|
end
|
493
549
|
|
@@ -501,8 +557,10 @@ class MostLikedPostsDatatable < TrkDatatables::ActiveRecord
|
|
501
557
|
# you have { search: false }
|
502
558
|
def comments_count
|
503
559
|
<<~SQL
|
504
|
-
|
505
|
-
|
560
|
+
(
|
561
|
+
SELECT COUNT(*) FROM comments
|
562
|
+
WHERE comments.post_id = posts.id
|
563
|
+
)
|
506
564
|
SQL
|
507
565
|
end
|
508
566
|
|
@@ -525,8 +583,11 @@ class MostLikedPostsDatatable < TrkDatatables::ActiveRecord
|
|
525
583
|
end
|
526
584
|
end
|
527
585
|
|
586
|
+
# You can use this in config/initializers/trk_datatables.rb
|
587
|
+
# class TrkDatatables::ActiveRecord; def default_order;
|
588
|
+
# or put in specific datatable class like here
|
528
589
|
def default_order
|
529
|
-
[[
|
590
|
+
[[columns.size - 1, :desc]]
|
530
591
|
end
|
531
592
|
end
|
532
593
|
```
|
@@ -619,6 +680,43 @@ calculated_ columns is used than it can not find translation so better is to:
|
|
619
680
|
'string_calculated_in_db.column_value_translated': {search: false, title: @view.t('activerecord.attributes.translation.column_value_translated')},
|
620
681
|
```
|
621
682
|
|
683
|
+
Note that when you have two associations to the same class, for example
|
684
|
+
`Interest` has `:from_member_profile` and `:to_member_profile` you can use
|
685
|
+
search for the first column, but for the second you should use
|
686
|
+
`string_calculated_in_db` and inside method use table name that is generated in
|
687
|
+
joins like `to_member_profiles_interests` (trk_datatable can not determine which
|
688
|
+
class is this, in order to find its column)
|
689
|
+
```
|
690
|
+
# app/models/interest.rb
|
691
|
+
class Interest < ApplicationRecord
|
692
|
+
belongs_to :from_member_profile, class_name: 'MemberProfile'
|
693
|
+
belongs_to :to_member_profile, class_name: 'MemberProfile'
|
694
|
+
end
|
695
|
+
|
696
|
+
# app/datatables/interests_datatable.rb
|
697
|
+
class InterestsDatatable < TrkDatatables::ActiveRecord
|
698
|
+
def columns
|
699
|
+
{
|
700
|
+
'member_profiles.full_name': {},
|
701
|
+
'string_calculated_in_db.to_member_profiles_interests_full_name': {},
|
702
|
+
# if we want to search by this column than we can not use
|
703
|
+
# "to_member_profiles_interests.full_name": {},
|
704
|
+
# since we can not find that class and its arel_table
|
705
|
+
}
|
706
|
+
end
|
707
|
+
|
708
|
+
def to_member_profiles_interests_full_name
|
709
|
+
"to_member_profiles_interests.full_name"
|
710
|
+
end
|
711
|
+
|
712
|
+
def all_items
|
713
|
+
Interest
|
714
|
+
.joins(:from_member_profile) # this will be "member_profiles" table in sql
|
715
|
+
.joins(:to_member_profile) # this will be "to_member_profiles_interests"
|
716
|
+
end
|
717
|
+
end
|
718
|
+
```
|
719
|
+
|
622
720
|
### Default order and page length
|
623
721
|
|
624
722
|
You can override default order (index and direction) and default page length so
|
@@ -628,10 +726,11 @@ save preferences) and this default values will be used
|
|
628
726
|
```
|
629
727
|
# app/datatables/posts_datatable.rb
|
630
728
|
class PostsDatatable
|
631
|
-
# when we show
|
632
|
-
# on first april,
|
729
|
+
# when we show invoice_no on first column, and that is reset every year
|
730
|
+
# on first april, than it is better is to use date column ordering
|
731
|
+
# column starts from zero 0, 1, 2, 3
|
633
732
|
def default_order
|
634
|
-
[[
|
733
|
+
[[columns.size - 1, :desc]]
|
635
734
|
end
|
636
735
|
|
637
736
|
def default_page_length
|
@@ -768,6 +867,15 @@ You can set filters on datatable even params are blank, for example
|
|
768
867
|
|
769
868
|
Inside datatable you can access params using `@view.params`
|
770
869
|
|
870
|
+
To set the order (custom sort) in a link you can use:
|
871
|
+
```
|
872
|
+
<%= link_to 'Sort by email', \
|
873
|
+
posts_path(
|
874
|
+
PostsDatatable.order_set('users.email', :desc)
|
875
|
+
)
|
876
|
+
%>
|
877
|
+
```
|
878
|
+
|
771
879
|
### Saved Preferences (optional)
|
772
880
|
|
773
881
|
You can save column order and page length in User.preferences field so
|
@@ -796,7 +904,7 @@ class PostsDatatable
|
|
796
904
|
end
|
797
905
|
```
|
798
906
|
|
799
|
-
It will store order and page
|
907
|
+
It will store order and page length inside `dt_preferences` on
|
800
908
|
`user.preferences`.
|
801
909
|
|
802
910
|
### Additional data to json response
|
@@ -892,7 +1000,7 @@ link_to 'Active', search_posts_path(PostsDatatable.param_set('posts.status',
|
|
892
1000
|
Here is example how you can test
|
893
1001
|
|
894
1002
|
```
|
895
|
-
# test/datatables/
|
1003
|
+
# test/datatables/posts_datatable_test.rb
|
896
1004
|
require 'test_helper'
|
897
1005
|
|
898
1006
|
class PostsDatatableTest < ActiveSupport::TestCase
|
@@ -911,6 +1019,27 @@ class PostsDatatableTest < ActiveSupport::TestCase
|
|
911
1019
|
end
|
912
1020
|
```
|
913
1021
|
|
1022
|
+
You can also write controller test
|
1023
|
+
```
|
1024
|
+
# test/controllers/posts_controller_test.rb
|
1025
|
+
require 'test_helper'
|
1026
|
+
|
1027
|
+
class PostsControllerTest < ActiveSupport::TestCase
|
1028
|
+
test "#index" do
|
1029
|
+
get posts_path
|
1030
|
+
assert_response :success
|
1031
|
+
end
|
1032
|
+
|
1033
|
+
test "#search" do
|
1034
|
+
post = posts(:post)
|
1035
|
+
post search_posts_path(search: { value: post.title })
|
1036
|
+
assert_response :success
|
1037
|
+
response_json = JSON.parse response.body
|
1038
|
+
assert_equal 1, response_json["data"].size
|
1039
|
+
end
|
1040
|
+
end
|
1041
|
+
```
|
1042
|
+
|
914
1043
|
## Exceptions
|
915
1044
|
|
916
1045
|
To catch errors from TrkDatables you can
|
@@ -1026,6 +1155,13 @@ rm -rf .yardoc/
|
|
1026
1155
|
|
1027
1156
|
Bug reports and pull requests are welcome on GitHub at https://github.com/trkin/trk_datatables. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
1028
1157
|
|
1158
|
+
## TODOs
|
1159
|
+
|
1160
|
+
Column filtering with dropdowns https://datatables.net/extensions/searchpanes/examples/advanced/columnFilter.html
|
1161
|
+
Adding graphs https://datatables.net/forums/discussion/comment/123621/#Comment_123621
|
1162
|
+
https://datatables.net/examples/api/highcharts.html
|
1163
|
+
|
1164
|
+
|
1029
1165
|
## License
|
1030
1166
|
|
1031
1167
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
@@ -121,7 +121,11 @@ module TrkDatatables
|
|
121
121
|
column_key_option = @column_key_options[index]
|
122
122
|
next if column_key_option[:column_options][ColumnKeyOptions::ORDER_OPTION] == false
|
123
123
|
|
124
|
-
queries <<
|
124
|
+
queries << if column_key_option[:table_class] < TrkDatatables::CalculatedInDb
|
125
|
+
"#{send(column_key_option[:column_key])} #{direction}"
|
126
|
+
else
|
127
|
+
"#{column_key_option[:column_key]} #{direction}"
|
128
|
+
end
|
125
129
|
end
|
126
130
|
filtered.order(Arel.sql(order_by.join(', ')))
|
127
131
|
end
|
@@ -21,6 +21,18 @@ module TrkDatatables
|
|
21
21
|
DtParams.param_set column_index, value
|
22
22
|
end
|
23
23
|
|
24
|
+
# Set sort for column. This is class method so you do not need
|
25
|
+
# datatable instance.
|
26
|
+
#
|
27
|
+
# @example
|
28
|
+
# link_to 'Sort by email',
|
29
|
+
# posts_path(PostsDatatable.order_set('users.email', :desc)
|
30
|
+
def order_set(column_key, direction = :asc, view = nil)
|
31
|
+
datatable = new view || OpenStruct.new(params: {})
|
32
|
+
column_index = datatable.index_by_column_key column_key
|
33
|
+
DtParams.order_set column_index, direction
|
34
|
+
end
|
35
|
+
|
24
36
|
# Get the form field name for column. This is class method so you do not
|
25
37
|
# need datatable instance. It returns something like
|
26
38
|
# 'column[3][search][value]`. For global search you can use
|
@@ -110,6 +110,10 @@ module TrkDatatables
|
|
110
110
|
{columns: {column_index.to_s => {search: {value: value}}}}
|
111
111
|
end
|
112
112
|
|
113
|
+
def self.order_set(column_index, direction)
|
114
|
+
{order: {'0': {column: column_index, dir: direction}}}
|
115
|
+
end
|
116
|
+
|
113
117
|
def self.form_field_name(column_index)
|
114
118
|
"columns[#{column_index}][search][value]"
|
115
119
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: trk_datatables
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.15
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dusan Orlovic
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-06-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -205,7 +205,7 @@ metadata:
|
|
205
205
|
source_code_uri: https://github.com/trkin/trk_datatables
|
206
206
|
changelog_uri: https://github.com/trkin/trk_datatables/blob/master/CHANGELOG.md
|
207
207
|
yard.run: yri
|
208
|
-
post_install_message:
|
208
|
+
post_install_message:
|
209
209
|
rdoc_options: []
|
210
210
|
require_paths:
|
211
211
|
- lib
|
@@ -221,7 +221,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
221
221
|
version: '0'
|
222
222
|
requirements: []
|
223
223
|
rubygems_version: 3.2.15
|
224
|
-
signing_key:
|
224
|
+
signing_key:
|
225
225
|
specification_version: 4
|
226
226
|
summary: Gem that simplify using datatables with Ruby on Rails and Sinatra.
|
227
227
|
test_files: []
|