trk_datatables 0.2.14 → 0.2.15

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0f8339f6cc77f6372dff2350851b738f1c02bc157496a23fe371795df5c7e379
4
- data.tar.gz: a6f7b37a8fb9c7b5a21f3fda2edd5a3a01f4f34a3c209ceb324ea7cdd8c15be2
3
+ metadata.gz: 0d7a8767e7c4b77bbdd9d47b478de7b6b3dd456c449e83e50a70c4b674121fa7
4
+ data.tar.gz: aedc1d098b6ddd648a7b18fbf99cc88dede84f63a63c7ececa704fcb8bb0d018
5
5
  SHA512:
6
- metadata.gz: a6c9d856519bf89090cdb4fb5b6856b3eb59568f816ffeed838425e23f9bb75a57c94be9afb0155b0064a2c4fa69568d426c949cf5847fb5bdc35577c148e824
7
- data.tar.gz: 88432304b3a19c6ac6cb7a683d6ed01919494f61f9b45050b6c3ee7102ef3f610a15852a47f8ce1282fca2d1c24d4192b895e6c66f2b1c92ceb9b4abd706c03f
6
+ metadata.gz: 890f228e839a1e25ab96db11cf92c2b9a328be57f49e202ad77d2071b2c4ccdec65c31bcb57f5743c3fabbca00cb0f941c9df15364025413a73616e23b906095
7
+ data.tar.gz: 9a6744711a901ea792c54633f4eac570a36c9547f8e86f9d112f4ed175d5f7838f33ccb2b33b529cde15f82bd7184edaec881056afb88f183b1df203b8139fa9
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- trk_datatables (0.2.14)
4
+ trk_datatables (0.2.15)
5
5
  activesupport
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -418,14 +418,14 @@ generated based on other columns):
418
418
 
419
419
  Simple calculations and subqueries works fine, you can search and order by them.
420
420
  Note that when you use join with other table, than you should group by all
421
- columns that you have used as columns, for example
421
+ columns that you have used as columns which can be ordered by, for example
422
422
  ```
423
423
  # app/datatables/member_profiles_datatable.rb
424
424
  def columns
425
425
  {
426
426
  'member_profiles.full_name': {},
427
427
  'users.email': {},
428
- 'users.current_sign_in_at': { },
428
+ 'users.current_sign_in_at': {},
429
429
  }
430
430
  end
431
431
  def all_items
@@ -543,7 +543,7 @@ class MostLikedPostsDatatable < TrkDatatables::ActiveRecord
543
543
  Post.select(%(
544
544
  posts.*,
545
545
  #{title_and_body} AS title_and_body,
546
- (#{comments_count}) AS comments_count
546
+ #{comments_count} AS comments_count
547
547
  ))
548
548
  end
549
549
 
@@ -557,8 +557,10 @@ class MostLikedPostsDatatable < TrkDatatables::ActiveRecord
557
557
  # you have { search: false }
558
558
  def comments_count
559
559
  <<~SQL
560
+ (
560
561
  SELECT COUNT(*) FROM comments
561
562
  WHERE comments.post_id = posts.id
563
+ )
562
564
  SQL
563
565
  end
564
566
 
@@ -865,6 +867,15 @@ You can set filters on datatable even params are blank, for example
865
867
 
866
868
  Inside datatable you can access params using `@view.params`
867
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
+
868
879
  ### Saved Preferences (optional)
869
880
 
870
881
  You can save column order and page length in User.preferences field so
@@ -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
@@ -1,3 +1,3 @@
1
1
  module TrkDatatables
2
- VERSION = '0.2.14'.freeze
2
+ VERSION = '0.2.15'.freeze
3
3
  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.14
4
+ version: 0.2.15
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dusan Orlovic
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-02-08 00:00:00.000000000 Z
11
+ date: 2023-06-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport