trk_datatables 0.1.17 → 0.1.18
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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 206ca38dbabdc5c5efc6977db4c34ee6300336e7162479a7c7b8a94f3fb74b8c
|
4
|
+
data.tar.gz: db45ebc154b0da6a653c785eee388128bfc3d982750fd3dd296e77c120932740
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 15ecc3cc4c9c0fb978bb76cdc9f136d7907956d6c4c7c464f5d4d5d6298147be91b00ed6e9314ef9b32318fd2772600067003da937c634585f687b0e27b3e3c4
|
7
|
+
data.tar.gz: ccf49ae9953ca13b9c0f850b67eb38f03cd7429a780ed46c15b078eea1028116a1ee19a29115474e3954cafb021c685e2ccf214e169f1c7b447fce34770e43a0
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -102,7 +102,7 @@ In controller add a route to `:search`
|
|
102
102
|
# config/routes.rb
|
103
103
|
Rails.application.routes.draw do
|
104
104
|
resources :posts do
|
105
|
-
|
105
|
+
collection do
|
106
106
|
post :search
|
107
107
|
end
|
108
108
|
end
|
@@ -343,12 +343,15 @@ 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 'users.email', 'my@email.com'
|
346
|
+
this `PostsDatatable.param_set 'users.email', 'my@email.com'`. For between
|
347
|
+
search you can use range `Time.zone.today..(Time.zone.today + 1.year)` and for
|
348
|
+
in multiple values use array `[Post.statuses[:draft]]`.
|
347
349
|
|
348
350
|
```
|
349
351
|
<%= link_to 'Active posts for my@email.com', \
|
350
352
|
posts_path(
|
351
353
|
PostsDatatable.param_set('users.email', 'my@email.com')
|
354
|
+
.deep_merge(PostsDatatable.param_set('posts.published_date', Date.parse('2019-10-20')..Date.parse('2019-10-22')))
|
352
355
|
.deep_merge(PostsDatatable.param_set('posts.status', Post.statuses.values_at(:published, :promoted))
|
353
356
|
.deep_merge(user_id: 1)
|
354
357
|
)
|
@@ -374,6 +377,15 @@ If you need, you can fetch params with this helper
|
|
374
377
|
PostsDatatable.param_get('users.email', params)
|
375
378
|
```
|
376
379
|
|
380
|
+
You can set filters on datatable even params are blank, for example
|
381
|
+
|
382
|
+
```
|
383
|
+
def index
|
384
|
+
view_context.params.merge! PostsDatatable.param_set 'posts.start_date', Time.zone.today..(Time.zone.today + 1.year)
|
385
|
+
@datatable = PostsDatatable.new view_context
|
386
|
+
end
|
387
|
+
```
|
388
|
+
|
377
389
|
### Saved Preferences (optional)
|
378
390
|
|
379
391
|
You can save column order and page length in User.preferences field so
|
@@ -550,10 +562,11 @@ ruby -I test test/trk_datatables/base_test.rb -n /additional/
|
|
550
562
|
You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
551
563
|
|
552
564
|
To install this gem onto your local machine, run `bundle exec rake install`. To
|
553
|
-
release a new version, update the version number
|
554
|
-
`lib/trk_datatables/version.rb`, and then publish with
|
565
|
+
release a new version, update the version number and then publish with
|
555
566
|
|
556
567
|
```
|
568
|
+
vi lib/trk_datatables/version.rb
|
569
|
+
bundle
|
557
570
|
bundle exec rake release
|
558
571
|
```
|
559
572
|
|
@@ -1,7 +1,7 @@
|
|
1
|
-
class <%=
|
1
|
+
class <%= @trk_class_name %> < TrkDatatables::ActiveRecord
|
2
2
|
def columns
|
3
3
|
{
|
4
|
-
<% class_name.constantize.columns.each do |column| -%>
|
4
|
+
<% @skip_model || class_name.constantize.columns.each do |column| -%>
|
5
5
|
<% next if %w[created_at updated_at].include? column.name -%>
|
6
6
|
'<%= table_name %>.<%= column.name %>': {},
|
7
7
|
<% end -%>
|
@@ -9,13 +9,15 @@ class <%= class_name.pluralize %>Datatable < TrkDatatables::ActiveRecord
|
|
9
9
|
end
|
10
10
|
|
11
11
|
def all_items
|
12
|
+
# you can use @view.params
|
12
13
|
<%= class_name %>.all
|
13
14
|
end
|
14
15
|
|
15
16
|
def rows(filtered)
|
17
|
+
# you can use @view.link_to and other helpers
|
16
18
|
filtered.map do |<%= singular_table_name %>|
|
17
19
|
[
|
18
|
-
<% class_name.constantize.columns.each do |column| -%>
|
20
|
+
<% @skip_model || class_name.constantize.columns.each do |column| -%>
|
19
21
|
<% next if %w[created_at updated_at].include? column.name -%>
|
20
22
|
<% if column.name == 'id' -%>
|
21
23
|
@view.link_to(<%= singular_table_name %>.id, <%= singular_table_name %>),
|
@@ -1,7 +1,6 @@
|
|
1
1
|
module TrkDatatables
|
2
2
|
module Generators
|
3
3
|
class TrkDatatablesGenerator < Rails::Generators::NamedBase
|
4
|
-
MissingModelError = Class.new(Thor::Error)
|
5
4
|
# we can call with `rails g trk_datatables` instead of: `rails g trk_datatables:trk_datatables`
|
6
5
|
namespace 'trk_datatables'
|
7
6
|
source_root File.expand_path('../templates', __dir__)
|
@@ -10,11 +9,49 @@ module TrkDatatables
|
|
10
9
|
def create
|
11
10
|
begin
|
12
11
|
class_name.constantize
|
12
|
+
@trk_class_name = "#{class_name.pluralize}Datatable"
|
13
|
+
@trk_file_name = "#{plural_name}_datatable"
|
13
14
|
rescue NameError => e
|
14
|
-
|
15
|
+
Rails.logger.info e.message
|
16
|
+
@skip_model = true
|
17
|
+
@trk_class_name = "#{class_name}Datatable"
|
18
|
+
@trk_file_name = "#{singular_name}_datatable"
|
15
19
|
end
|
16
20
|
|
17
|
-
template 'trk_datatable.rb', "app/datatables/#{
|
21
|
+
template 'trk_datatable.rb', "app/datatables/#{@trk_file_name}.rb"
|
22
|
+
|
23
|
+
say <<~TEXT
|
24
|
+
======================================================================
|
25
|
+
You can use in your controller
|
26
|
+
|
27
|
+
# app/controllers/#{plural_name}_controller.rb
|
28
|
+
class #{class_name}Controller < ApplicationController
|
29
|
+
def index
|
30
|
+
@datatable = #{@trk_class_name}.new view_context
|
31
|
+
end
|
32
|
+
|
33
|
+
def search
|
34
|
+
render json: #{@trk_class_name}.new(view_context)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
In your views mkdir app/views/#{plural_name}
|
39
|
+
# app/views/#{plural_name}/index.html
|
40
|
+
<h1>#{class_name.pluralize}</h1>
|
41
|
+
<%= @datatable.render_html search_#{plural_name}_path(format: :json) %>
|
42
|
+
|
43
|
+
And in routes
|
44
|
+
|
45
|
+
# config/routes.rb
|
46
|
+
Rails.application.routes.draw do
|
47
|
+
resources :#{plural_name} do
|
48
|
+
collection do
|
49
|
+
post :search
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
======================================================================
|
54
|
+
TEXT
|
18
55
|
end
|
19
56
|
end
|
20
57
|
end
|
@@ -16,6 +16,7 @@ module TrkDatatables
|
|
16
16
|
def param_set(column_key, value)
|
17
17
|
datatable = new OpenStruct.new(params: {})
|
18
18
|
value = value.join MULTIPLE_OPTION_SEPARATOR if value.is_a? Array
|
19
|
+
value = [value.first, value.last].join BETWEEN_SEPARATOR if value.is_a? Range
|
19
20
|
column_index = datatable.index_by_column_key column_key
|
20
21
|
DtParams.param_set column_index, value
|
21
22
|
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.1.
|
4
|
+
version: 0.1.18
|
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-10-
|
11
|
+
date: 2019-10-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|