trk_datatables 0.1.14 → 0.1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8c62f6190d44143f7d76ef88fce49357c06a2e693bc96164cb678f0f5935f817
4
- data.tar.gz: 3a5ab2c6e062acd588484e3907e135875ab688c5ff88b4c075f0e42f785a00b8
3
+ metadata.gz: 13f7157e06eae31d336fcd8c561a1e681922f3b940677821012443fb6f4fd504
4
+ data.tar.gz: 7648806094e45b2be4b2838180150284d8e05aa78764456267ac539fc7ff59d4
5
5
  SHA512:
6
- metadata.gz: 22dc186b88537bab98e1b36981fcce153e325252935e873b8007a176943e7e48caf5c90868e2bdffe6055bdc171a06f9ed9367fe821eb07bbd801b9e0a9e48a2
7
- data.tar.gz: c6d7304344a2f174444cc5d6de704d68e4ef6af4ea6a611cd1d5aca7ded896abdd279721245ef9ff2fbe8268af88c02ecdc97a26e79348d96e57e8da2242ed52
6
+ metadata.gz: 04fde516dc05e4833d70bce836a91d6ce46aee65072065c22ab801f8878ab4665bf5af3d22b679aa6452750150328d246ee33cd900ccbac9daf246b3c95ffdea
7
+ data.tar.gz: 6b72c1393c14ce9d567c349cc2a56e2d816478dfc768a39df4bf3c91f5f33347f8b4457e61bd0fd69c4b79325db8963b69c7e0b8a04a936ab037fdde8f5711b7
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- trk_datatables (0.1.14)
4
+ trk_datatables (0.1.15)
5
5
  activesupport
6
6
 
7
7
  GEM
@@ -23,7 +23,7 @@ GEM
23
23
  database_cleaner (1.7.0)
24
24
  i18n (1.6.0)
25
25
  concurrent-ruby (~> 1.0)
26
- minitest (5.11.3)
26
+ minitest (5.12.0)
27
27
  minitest-color (0.0.2)
28
28
  minitest (~> 5)
29
29
  pg (1.1.4)
@@ -32,7 +32,7 @@ GEM
32
32
  thread_safe (0.3.6)
33
33
  tzinfo (1.2.5)
34
34
  thread_safe (~> 0.1)
35
- zeitwerk (2.1.9)
35
+ zeitwerk (2.1.10)
36
36
 
37
37
  PLATFORMS
38
38
  ruby
data/README.md CHANGED
@@ -66,6 +66,10 @@ class PostsDatatable < TrkDatatables::ActiveRecord
66
66
  }
67
67
  end
68
68
 
69
+ def all_items
70
+ Post.left_joins(:user)
71
+ end
72
+
69
73
  def rows(filtered)
70
74
  filtered.map do |post|
71
75
  [
@@ -74,10 +78,6 @@ class PostsDatatable < TrkDatatables::ActiveRecord
74
78
  ]
75
79
  end
76
80
  end
77
-
78
- def all_items
79
- Post.left_joins(:user)
80
- end
81
81
  end
82
82
  ```
83
83
 
@@ -343,18 +343,30 @@ end
343
343
  ### Params
344
344
 
345
345
  To set parameters that you can use for links to set column search value, use
346
- this `PostsDatatable.param_set` for example
346
+ this `PostsDatatable.param_set 'users.email', 'my@email.com'` for example
347
347
 
348
348
  ```
349
- link_to 'Active posts for my@email.com', \
350
- posts_path(
351
- PostsDatatable.param_set('users.email', 'my@email.com')
352
- .deep_merge(PostsDatatable.param_set('posts.status', Post.statuses.values_at(:published, :promoted))
353
- .deep_merge(user_id: 1)
354
- )
349
+ <%= link_to 'Active posts for my@email.com', \
350
+ posts_path(
351
+ PostsDatatable.param_set('users.email', 'my@email.com')
352
+ .deep_merge(PostsDatatable.param_set('posts.status', Post.statuses.values_at(:published, :promoted))
353
+ .deep_merge(user_id: 1)
354
+ )
355
+ %>
355
356
  ```
356
357
 
357
- This will fill proper column search values so you do not need to do it manually (`post_path(:columns=>{"3"=>{:search=>{:value=>"my@email.com"}}, "2"=>{:search=>{:value=>"1|2"}}}, :user_id=>1)`)
358
+ This will fill proper column search values so you do not need to do it manually
359
+ (`post_path(:columns=>{"3"=>{:search=>{:value=>"my@email.com"}},
360
+ "2"=>{:search=>{:value=>"1|2"}}}, :user_id=>1)`)
361
+
362
+ For form fields you can use similar helper `PostsDatatable.form_field_name
363
+ 'users.email'`
364
+ ```
365
+ <%= form_tag url: posts_path, method: :get do |f| %>
366
+ <%= f.text_field PostsDatatable.form_field_name('users.email'), 'my@email.com' %>
367
+ <%= f.submit 'Search' %>
368
+ <% end %>
369
+ ```
358
370
 
359
371
  If you need, you can fetch params with this helper
360
372
 
@@ -486,7 +498,18 @@ side rendering and more advance listing
486
498
 
487
499
  ## Development
488
500
 
489
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
501
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run all tests.
502
+
503
+ ```
504
+ # all
505
+ rake
506
+ # specific file
507
+ ruby -I test test/trk_datatables/base_test.rb
508
+ # only mathing
509
+ ruby -I test test/trk_datatables/base_test.rb -n /additional/
510
+ ```
511
+
512
+ You can also run `bin/console` for an interactive prompt that will allow you to experiment.
490
513
 
491
514
  To install this gem onto your local machine, run `bundle exec rake install`. To
492
515
  release a new version, update the version number in
@@ -8,6 +8,8 @@ module TrkDatatables
8
8
  end
9
9
 
10
10
  class Base
11
+ extend TrkDatatables::BaseHelpers
12
+
11
13
  attr_accessor :column_key_options
12
14
 
13
15
  # In tests you can use `spy(:view)` when you want to initialize without
@@ -95,6 +97,8 @@ module TrkDatatables
95
97
  raise 'order_and_paginate_items_is_defined_in_specific_orm'
96
98
  end
97
99
 
100
+ # rubocop:disable Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity
101
+
98
102
  # Returns dt_orders or default as array of index and direction
99
103
  # https://datatables.net/reference/option/order
100
104
  # @return
@@ -104,7 +108,9 @@ module TrkDatatables
104
108
  def dt_orders_or_default_index_and_direction
105
109
  return @dt_orders_or_default if defined? @dt_orders_or_default
106
110
 
107
- if @dt_params.dt_orders.present?
111
+ if columns.blank?
112
+ @dt_orders_or_default = []
113
+ elsif @dt_params.dt_orders.present?
108
114
  @dt_orders_or_default = @dt_params.dt_orders
109
115
  @preferences.set :order, @dt_params.dt_orders
110
116
  else
@@ -113,6 +119,7 @@ module TrkDatatables
113
119
  end
114
120
  @dt_orders_or_default
115
121
  end
122
+ # rubocop:enable Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity
116
123
 
117
124
  def default_order
118
125
  [[0, :desc]].freeze
@@ -134,26 +141,6 @@ module TrkDatatables
134
141
  end
135
142
  end
136
143
 
137
- # Set params for columns. This is class method so you do not need datatable
138
- # instance.
139
- #
140
- # @example
141
- # link_to 'Published posts for my@email.com',
142
- # posts_path(PostsDatatable.params('posts.status': :published,
143
- # 'users.email: 'my@email.com')
144
- #
145
- # You can always use your params for filtering outside of datatable and
146
- # merge to params
147
- # @example
148
- # link_to 'Published posts for user1',
149
- # posts_path(PostsDatatable.param_set('posts.status', :published).merge(user_id: user1.id))
150
- def self.param_set(column_key, value)
151
- datatable = new OpenStruct.new(params: {})
152
- value = value.join MULTIPLE_OPTION_SEPARATOR if value.is_a? Array
153
- column_index = datatable.index_by_column_key column_key
154
- DtParams.param_set column_index, value
155
- end
156
-
157
144
  # We need this method publicly available since we use it for class method
158
145
  # param_set
159
146
  def index_by_column_key(column_key)
@@ -0,0 +1,44 @@
1
+ module TrkDatatables
2
+ module BaseHelpers
3
+ # Set params for column search. This is class method so you do not need
4
+ # datatable instance.
5
+ #
6
+ # @example
7
+ # link_to 'Published posts for my@email.com',
8
+ # posts_path(PostsDatatable.params('posts.status': :published,
9
+ # 'users.email: 'my@email.com')
10
+ #
11
+ # You can always use your params for filtering outside of datatable and
12
+ # merge to params
13
+ # @example
14
+ # link_to 'Published posts for user1',
15
+ # posts_path(PostsDatatable.param_set('posts.status', :published).merge(user_id: user1.id))
16
+ def param_set(column_key, value)
17
+ datatable = new OpenStruct.new(params: {})
18
+ value = value.join MULTIPLE_OPTION_SEPARATOR if value.is_a? Array
19
+ column_index = datatable.index_by_column_key column_key
20
+ DtParams.param_set column_index, value
21
+ end
22
+
23
+ # Get the form field name for column. This is class method so you do not
24
+ # need datatable instance.
25
+ #
26
+ # @example
27
+ # form_tag url: posts_path, method: :get do |f|
28
+ # f.text_field PostsDatatable.form_field_name('users.email'), 'my@email.com'
29
+ def form_field_name(column_key)
30
+ datatable = new OpenStruct.new(params: {})
31
+ column_index = datatable.index_by_column_key column_key
32
+ DtParams.form_field_name column_index
33
+ end
34
+
35
+ # For range you can this helper to insert BETWEEN_SEPARATOR
36
+ def range_string(range)
37
+ raise ArgumentError, "#{range} is not a Range" unless range.is_a? Range
38
+
39
+ from = range.min
40
+ to = range.max
41
+ "#{from} #{BETWEEN_SEPARATOR} #{to}"
42
+ end
43
+ end
44
+ end
@@ -108,6 +108,10 @@ module TrkDatatables
108
108
  { columns: { column_index.to_s => { search: { value: value } } } }
109
109
  end
110
110
 
111
+ def self.form_field_name(column_index)
112
+ "columns[#{column_index}][search][value]"
113
+ end
114
+
111
115
  def param_get(column_index)
112
116
  @params.dig :columns, column_index.to_s, :search, :value
113
117
  end
@@ -1,3 +1,3 @@
1
1
  module TrkDatatables
2
- VERSION = '0.1.14'.freeze
2
+ VERSION = '0.1.15'.freeze
3
3
  end
@@ -1,7 +1,7 @@
1
1
  require 'trk_datatables/version'
2
2
  # modules
3
3
  require 'trk_datatables/preferences.rb'
4
-
4
+ require 'trk_datatables/base_helpers'
5
5
  require 'trk_datatables/base'
6
6
  require 'trk_datatables/active_record'
7
7
  require 'trk_datatables/neo4j'
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.1.14
4
+ version: 0.1.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: 2019-09-19 00:00:00.000000000 Z
11
+ date: 2019-09-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -171,6 +171,7 @@ files:
171
171
  - lib/trk_datatables.rb
172
172
  - lib/trk_datatables/active_record.rb
173
173
  - lib/trk_datatables/base.rb
174
+ - lib/trk_datatables/base_helpers.rb
174
175
  - lib/trk_datatables/column_key_options.rb
175
176
  - lib/trk_datatables/dt_params.rb
176
177
  - lib/trk_datatables/neo4j.rb