trk_datatables 0.1.11 → 0.1.12
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +61 -10
- data/lib/trk_datatables/base.rb +20 -15
- data/lib/trk_datatables/column_key_options.rb +3 -1
- data/lib/trk_datatables/version.rb +1 -1
- metadata +3 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 689f7693808d3e9a2401c9b6fdbb6481b8d2d642e31064eb2f38e704ed95c5a2
|
4
|
+
data.tar.gz: b5f6ba211d7fcec99e9025a51bd0f6fc83d69ee3e10eeb6e1df10f2c41ddf3e2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 566e9b5010b7b75bb0a1cc4ee3adf937a9cdfde97826007665a4e4b5edf80d4a970a1705700e66bf30a3c629da5bc3da66a069858e3670343284f66075b06d90
|
7
|
+
data.tar.gz: '08e33268ee12bb569476fafb899c8938ef7b272d3ea7272e4c851113ffde0b6fba94630f6363dd61e874f465622ab94c9fff638b7ab48c186c8c21a96c127fcc'
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -141,20 +141,39 @@ end
|
|
141
141
|
|
142
142
|
### Column 'ILIKE' search
|
143
143
|
|
144
|
-
All columns are by default
|
145
|
-
|
146
|
-
|
147
|
-
|
144
|
+
All columns are by default casted to string and `ILIKE` is perfomed.
|
145
|
+
|
146
|
+
If you do not need any specific column configuration,
|
147
|
+
for example custom `title`, than instead of defining columns as key/value
|
148
|
+
pairs, you can define them in one line as array of column_key.
|
148
149
|
|
149
150
|
```
|
150
151
|
# app/datatables/posts_datatable.rb
|
151
152
|
class PostsDatatable < TrkDatatables::ActiveRecord
|
152
153
|
def columns
|
153
|
-
#
|
154
|
+
# instead of
|
155
|
+
# {
|
156
|
+
# id: {},
|
157
|
+
# title: {},
|
158
|
+
# body: {},
|
159
|
+
# }.each_with_object({}) { |(key, value), obj| obj["posts.#{key}"] = value }
|
160
|
+
# you can use one liner
|
161
|
+
# %w[posts.id posts.title posts.body]
|
162
|
+
# or even shorter using map
|
154
163
|
%i[id title body].map { |col| "posts.#{col}" }
|
155
164
|
end
|
156
165
|
end
|
157
166
|
```
|
167
|
+
|
168
|
+
For specific columns you can use following keys
|
169
|
+
|
170
|
+
* `title: 'My Title'` set up column header
|
171
|
+
* `search: false` disable searching for this column
|
172
|
+
* `order: false` disable ordering for this column
|
173
|
+
* `predefined_ranges: {}` add ranges for datetime fields
|
174
|
+
* `select_options: Post.statuses` generate select box
|
175
|
+
* `hide: true` hide column
|
176
|
+
|
158
177
|
### Column 'BETWEEN' search
|
159
178
|
|
160
179
|
For column search when search string contains BETWEEN_SEPARATOR (` - `) and
|
@@ -240,7 +259,7 @@ def columns
|
|
240
259
|
end
|
241
260
|
|
242
261
|
# in view
|
243
|
-
link_to 'Active', search_posts_path(PostsDatatable.
|
262
|
+
link_to 'Active', search_posts_path(PostsDatatable.param_set('posts.status':
|
244
263
|
Post.statues.values_at(:published, :promoted)))
|
245
264
|
```
|
246
265
|
|
@@ -272,20 +291,43 @@ use empty column_key
|
|
272
291
|
end
|
273
292
|
```
|
274
293
|
|
294
|
+
### Default order and page length
|
295
|
+
|
296
|
+
You can override default order (index and direction) and default page length so
|
297
|
+
if user did not request some order or page length (and if it is not found in
|
298
|
+
save preferences) and this default values will be used
|
299
|
+
|
300
|
+
```
|
301
|
+
# app/datatables/posts_datatable.rb
|
302
|
+
class PostsDatatable
|
303
|
+
# when we show some invoice_no on first column, and that is reset every year
|
304
|
+
# on first april, thatn it is better is to use date column ordering
|
305
|
+
def default_order
|
306
|
+
[[3, :desc]]
|
307
|
+
end
|
308
|
+
|
309
|
+
def default_page_length
|
310
|
+
20
|
311
|
+
end
|
312
|
+
end
|
313
|
+
```
|
314
|
+
|
275
315
|
### Params
|
276
316
|
|
277
317
|
To set parameters that you can use for links to set column search value, use
|
278
318
|
this `PostsDatatable.param_set` for example
|
279
319
|
|
280
320
|
```
|
281
|
-
link_to '
|
321
|
+
link_to 'Active posts for my@email.com', \
|
282
322
|
posts_path(
|
283
|
-
PostsDatatable.
|
284
|
-
.
|
323
|
+
PostsDatatable.param_set('users.email', 'my@email.com')
|
324
|
+
.deep_merge(PostsDatatable.param_set('posts.status', Post.statuses.values_at(:published, :promoted))
|
325
|
+
.deep_merge(user_id: 1)
|
285
326
|
)
|
286
|
-
# will generate
|
287
327
|
```
|
288
328
|
|
329
|
+
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)`)
|
330
|
+
|
289
331
|
If you need, you can fetch params with this helper
|
290
332
|
|
291
333
|
```
|
@@ -420,6 +462,15 @@ After checking out the repo, run `bin/setup` to install dependencies. Then, run
|
|
420
462
|
|
421
463
|
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
422
464
|
|
465
|
+
Instead of installing you can point directly to your path
|
466
|
+
```
|
467
|
+
# Gemfile
|
468
|
+
# for index, ordering, pagination and searching
|
469
|
+
# gem 'trk_datatables', '~>0.1'
|
470
|
+
# no need to `bundle update trk_datatables` when switch to local
|
471
|
+
gem 'trk_datatables', path: '~/gems/trk_datatables'
|
472
|
+
```
|
473
|
+
|
423
474
|
To generate docs you can run
|
424
475
|
|
425
476
|
```
|
data/lib/trk_datatables/base.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
module TrkDatatables
|
2
|
+
# TODO: extract those to configuration options
|
2
3
|
BETWEEN_SEPARATOR = ' - '.freeze
|
3
4
|
MULTIPLE_OPTION_SEPARATOR = '|'.freeze
|
4
|
-
|
5
|
-
DEFAULT_PAGE_LENGTH = 10
|
5
|
+
# maximum page length = 100 (we should not believe params)
|
6
6
|
|
7
7
|
class Error < StandardError
|
8
8
|
end
|
@@ -109,11 +109,19 @@ module TrkDatatables
|
|
109
109
|
@preferences.set :order, @dt_params.dt_orders
|
110
110
|
else
|
111
111
|
check_value = ->(r) { r.is_a?(Array) && r[0].is_a?(Array) && r[0][0].is_a?(Integer) }
|
112
|
-
@dt_orders_or_default = @preferences.get(:order, check_value) ||
|
112
|
+
@dt_orders_or_default = @preferences.get(:order, check_value) || default_order
|
113
113
|
end
|
114
114
|
@dt_orders_or_default
|
115
115
|
end
|
116
116
|
|
117
|
+
def default_order
|
118
|
+
[[0, :desc]].freeze
|
119
|
+
end
|
120
|
+
|
121
|
+
def default_page_length
|
122
|
+
10
|
123
|
+
end
|
124
|
+
|
117
125
|
def dt_per_page_or_default
|
118
126
|
return @dt_per_page_or_default if defined? @dt_per_page_or_default
|
119
127
|
|
@@ -122,7 +130,7 @@ module TrkDatatables
|
|
122
130
|
@preferences.set :per_page, @dt_params.dt_per_page
|
123
131
|
@dt_params.dt_per_page
|
124
132
|
else
|
125
|
-
@preferences.get(:per_page) ||
|
133
|
+
@preferences.get(:per_page) || default_page_length
|
126
134
|
end
|
127
135
|
end
|
128
136
|
|
@@ -134,23 +142,20 @@ module TrkDatatables
|
|
134
142
|
# posts_path(PostsDatatable.params('posts.status': :published,
|
135
143
|
# 'users.email: 'my@email.com')
|
136
144
|
#
|
137
|
-
# You can always use your params for filtering outside of datatable
|
145
|
+
# You can always use your params for filtering outside of datatable and
|
146
|
+
# merge to params
|
138
147
|
# @example
|
139
148
|
# link_to 'Published posts for user1',
|
140
|
-
# posts_path(PostsDatatable.
|
141
|
-
def self.
|
149
|
+
# posts_path(PostsDatatable.param_set('posts.status', :published).merge(user_id: user1.id))
|
150
|
+
def self.param_set(column_key, value)
|
142
151
|
datatable = new OpenStruct.new(params: {})
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
column_index = datatable.index_by_column_key column_key
|
147
|
-
result = result.deep_merge DtParams.param_set column_index, value
|
148
|
-
end
|
149
|
-
result
|
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
|
150
155
|
end
|
151
156
|
|
152
157
|
# We need this method publicly available since we use it for class method
|
153
|
-
#
|
158
|
+
# param_set
|
154
159
|
def index_by_column_key(column_key)
|
155
160
|
@column_key_options.index_by_column_key column_key
|
156
161
|
end
|
@@ -17,10 +17,11 @@ module TrkDatatables
|
|
17
17
|
TITLE_OPTION = :title
|
18
18
|
SELECT_OPTIONS = :select_options
|
19
19
|
PREDEFINED_RANGES = :predefined_ranges
|
20
|
+
HIDE_OPTION = :hide
|
20
21
|
# this will load date picker
|
21
22
|
# SEARCH_OPTION_DATE_VALUE = :date
|
22
23
|
# SEARCH_OPTION_DATETIME_VALUE = :datetime
|
23
|
-
COLUMN_OPTIONS = [SEARCH_OPTION, ORDER_OPTION, TITLE_OPTION, SELECT_OPTIONS, PREDEFINED_RANGES].freeze
|
24
|
+
COLUMN_OPTIONS = [SEARCH_OPTION, ORDER_OPTION, TITLE_OPTION, SELECT_OPTIONS, PREDEFINED_RANGES, HIDE_OPTION].freeze
|
24
25
|
|
25
26
|
STRING_TYPE_CAST_POSTGRES = 'VARCHAR'.freeze
|
26
27
|
STRING_TYPE_CAST_MYSQL = 'CHAR'.freeze
|
@@ -170,6 +171,7 @@ module TrkDatatables
|
|
170
171
|
res = {}
|
171
172
|
res['data-searchable'] = false if column_options[SEARCH_OPTION] == false
|
172
173
|
res['data-orderable'] = false if column_options[ORDER_OPTION] == false
|
174
|
+
res['data-datatable-hide-column'] = true if column_options[HIDE_OPTION] == true
|
173
175
|
if %i[date datetime].include? column_type_in_db
|
174
176
|
res['data-datatable-range'] = column_type_in_db == :datetime ? :datetime : true
|
175
177
|
if column_options[PREDEFINED_RANGES].present? ||
|
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.
|
4
|
+
version: 0.1.12
|
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-
|
11
|
+
date: 2019-09-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -203,8 +203,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
203
203
|
- !ruby/object:Gem::Version
|
204
204
|
version: '0'
|
205
205
|
requirements: []
|
206
|
-
|
207
|
-
rubygems_version: 2.7.6
|
206
|
+
rubygems_version: 3.0.4
|
208
207
|
signing_key:
|
209
208
|
specification_version: 4
|
210
209
|
summary: Gem that simplify using datatables with Ruby on Rails and Sinatra.
|