trk_datatables 0.1.0
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 +7 -0
- data/.gitignore +9 -0
- data/.travis.yml +7 -0
- data/CHANGELOG.md +3 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +56 -0
- data/LICENSE.txt +21 -0
- data/README.md +267 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/trk_datatables.rb +15 -0
- data/lib/trk_datatables/active_record.rb +110 -0
- data/lib/trk_datatables/base.rb +189 -0
- data/lib/trk_datatables/column_key_options.rb +165 -0
- data/lib/trk_datatables/dt_params.rb +156 -0
- data/lib/trk_datatables/preferences.rb +35 -0
- data/lib/trk_datatables/render_html.rb +133 -0
- data/lib/trk_datatables/version.rb +3 -0
- data/trk_datatables.gemspec +53 -0
- data/yarn.lock +4 -0
- metadata +210 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: bd503b00264e92d4ef4c55c30507288ea014180615b594e3403afa07e1e84aee
|
4
|
+
data.tar.gz: bc7883edb1bcb2c828dda4c7d3f7094cfb4a1ab462e70cf912ac03c1e80f0199
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1b8c68037f5ade06d24b01f25fb2159a39f2e57bacbbb6d8f34ce5b2447c3e33223bad2ef613b36aa452fdcb816f9d3a6de2ad31ace78e26c476a9bd64113acc
|
7
|
+
data.tar.gz: caee91e2838d5eea03798d383faccc057ff147f2f86e3c62064bd1f97504ff0efdab44edb1cfa030760c93353e2be04eeb3d95418dc900bd4e25d1c81ba3a75c
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at duleorlovic@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
trk_datatables (0.1.0)
|
5
|
+
activesupport
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activemodel (6.0.0)
|
11
|
+
activesupport (= 6.0.0)
|
12
|
+
activerecord (6.0.0)
|
13
|
+
activemodel (= 6.0.0)
|
14
|
+
activesupport (= 6.0.0)
|
15
|
+
activesupport (6.0.0)
|
16
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
17
|
+
i18n (>= 0.7, < 2)
|
18
|
+
minitest (~> 5.1)
|
19
|
+
tzinfo (~> 1.1)
|
20
|
+
zeitwerk (~> 2.1, >= 2.1.8)
|
21
|
+
byebug (11.0.1)
|
22
|
+
concurrent-ruby (1.1.5)
|
23
|
+
database_cleaner (1.7.0)
|
24
|
+
i18n (1.6.0)
|
25
|
+
concurrent-ruby (~> 1.0)
|
26
|
+
minitest (5.11.3)
|
27
|
+
minitest-around (0.5.0)
|
28
|
+
minitest (~> 5.0)
|
29
|
+
minitest-color (0.0.2)
|
30
|
+
minitest (~> 5)
|
31
|
+
pg (1.1.4)
|
32
|
+
rake (10.5.0)
|
33
|
+
sqlite3 (1.4.1)
|
34
|
+
thread_safe (0.3.6)
|
35
|
+
tzinfo (1.2.5)
|
36
|
+
thread_safe (~> 0.1)
|
37
|
+
zeitwerk (2.1.9)
|
38
|
+
|
39
|
+
PLATFORMS
|
40
|
+
ruby
|
41
|
+
|
42
|
+
DEPENDENCIES
|
43
|
+
activerecord (~> 6.0)
|
44
|
+
bundler (~> 2.0)
|
45
|
+
byebug
|
46
|
+
database_cleaner
|
47
|
+
minitest (~> 5.0)
|
48
|
+
minitest-around
|
49
|
+
minitest-color
|
50
|
+
pg
|
51
|
+
rake (~> 10.0)
|
52
|
+
sqlite3
|
53
|
+
trk_datatables!
|
54
|
+
|
55
|
+
BUNDLED WITH
|
56
|
+
2.0.2
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 Dusan Orlovic
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,267 @@
|
|
1
|
+
# Trk Datatables
|
2
|
+
|
3
|
+
This is a [trk_datatables gem](https://github.com/trkin/trk_datatables) that you
|
4
|
+
can use with trk_datatables npm package for easier usage of [Datatables js library](https://datatables.net)
|
5
|
+
|
6
|
+
It gives you one liner command to generate first page in html (so non-js
|
7
|
+
crawlers can see it), global search, filtering and sorting by one or more
|
8
|
+
columns, adding map and other complex listing based on GET params.
|
9
|
+
|
10
|
+
There are alternatives, for example:
|
11
|
+
* [jbox-web/ajax-datatables-rails](https://github.com/jbox-web/ajax-datatables-rails)
|
12
|
+
excellent gem but it's scope is only to produce JSON. I wanted to have server
|
13
|
+
side rendering and more advance listing
|
14
|
+
|
15
|
+
## Installation
|
16
|
+
|
17
|
+
Add this line to your application's Gemfile:
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
# Gemfile
|
21
|
+
gem 'trk_datatables'
|
22
|
+
```
|
23
|
+
|
24
|
+
You need to create a class that inherits from `TrkDatatables::ActiveRecord` (you
|
25
|
+
can use Rails generator)
|
26
|
+
|
27
|
+
```
|
28
|
+
```
|
29
|
+
|
30
|
+
## Usage example in Ruby on Rails
|
31
|
+
|
32
|
+
For a table you need to define `rows` and `columns` (well that is obvious 😌).
|
33
|
+
In datatable class you also need to define `all_items` method which will be
|
34
|
+
used to populate `rows` with paginated, sorted and filtered items (we will call
|
35
|
+
them `filtered`)
|
36
|
+
|
37
|
+
```
|
38
|
+
# app/datatables/posts_datatable.rb
|
39
|
+
class PostsDatatable < TrkDatatables::ActiveRecord
|
40
|
+
def all_items
|
41
|
+
Post.left_joins(:user)
|
42
|
+
end
|
43
|
+
|
44
|
+
def columns
|
45
|
+
{
|
46
|
+
'posts.title': {},
|
47
|
+
'users.email': {},
|
48
|
+
}
|
49
|
+
end
|
50
|
+
|
51
|
+
def rows(filtered)
|
52
|
+
filtered.map do |post|
|
53
|
+
[
|
54
|
+
@view.link_to(post.title, post),
|
55
|
+
post.user&.email,
|
56
|
+
]
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
```
|
61
|
+
|
62
|
+
In controller you need to initialize with `view_context`
|
63
|
+
|
64
|
+
```
|
65
|
+
# app/controllers/posts_controller.rb
|
66
|
+
class PostsController < ApplicationController
|
67
|
+
def index
|
68
|
+
@datatable = PostsDatatable.new view_context
|
69
|
+
end
|
70
|
+
|
71
|
+
def search
|
72
|
+
render json: PostsDatatable.new(view_context).as_json
|
73
|
+
end
|
74
|
+
end
|
75
|
+
```
|
76
|
+
|
77
|
+
In controller add a route to `:search`
|
78
|
+
|
79
|
+
```
|
80
|
+
# config/routes.rb
|
81
|
+
Rails.application.routes.draw do
|
82
|
+
resources :posts do
|
83
|
+
colletion do
|
84
|
+
get :search
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
```
|
89
|
+
|
90
|
+
And finally in a view, use `render_html` to have first page show up prerendered
|
91
|
+
|
92
|
+
```
|
93
|
+
# app/views/posts/index.html
|
94
|
+
<h1>Posts</h1>
|
95
|
+
<%= @datatable.render_html search_posts_path(format: :json) %>
|
96
|
+
```
|
97
|
+
|
98
|
+
## Configuration
|
99
|
+
|
100
|
+
### Global search
|
101
|
+
|
102
|
+
There are two types of search: global (search all columns) and column search
|
103
|
+
(search is done for specific columns).
|
104
|
+
|
105
|
+
For global search any type of a column is casted to a string and we use `ILIKE`
|
106
|
+
(ie `.matches`).
|
107
|
+
|
108
|
+
You can add more columns to global search by overriding `global_search_columns`
|
109
|
+
method.
|
110
|
+
|
111
|
+
```
|
112
|
+
class PostsDatatable < TrkDatatables::ActiveRecord
|
113
|
+
def global_search_columns
|
114
|
+
# those fields will be used only to match global search
|
115
|
+
%w[posts.body users.name]
|
116
|
+
end
|
117
|
+
end
|
118
|
+
```
|
119
|
+
|
120
|
+
### Column 'ILIKE' and 'BETWEEN' search
|
121
|
+
|
122
|
+
For column search when search string does not contain BETWEEN_SEPARATOR (` - `) than
|
123
|
+
all columns are casted to string and `ILIKE` is perfomed.
|
124
|
+
|
125
|
+
When column_type_in_db as one of the: `:date`, `:datetime`, `:integer` and
|
126
|
+
`:float` than `BETWEEN` is perfomed (when search contains BETWEEN_SEPARATOR,
|
127
|
+
otherwise it is `ILIKE`).
|
128
|
+
|
129
|
+
For columns `:date` and `:datetime` there will be `data-datatable-range='true'`
|
130
|
+
attribute so bootstrap datepicker will be automatically loaded.
|
131
|
+
|
132
|
+
### Column 'IN' search
|
133
|
+
|
134
|
+
You can use column_option `select_options: [['name1', 'value1']]` so select box
|
135
|
+
will be loaded and match if `col IN (value1|value2)`.
|
136
|
+
|
137
|
+
```
|
138
|
+
def columns
|
139
|
+
{
|
140
|
+
'posts.title': {},
|
141
|
+
'posts.status': { select_options: Post.statuses },
|
142
|
+
}
|
143
|
+
end
|
144
|
+
|
145
|
+
# in view
|
146
|
+
link_to 'Active', search_posts_path(PostsDatatable.params_set('posts.status':
|
147
|
+
Post.statues.values_at(:published, :promoted)))
|
148
|
+
```
|
149
|
+
|
150
|
+
You can use column_option `search: :checkbox` so for column_type_in_db `:boolean`
|
151
|
+
it will provide checkbox. For other column_type_in_db it will match if value is
|
152
|
+
NULL or NOT NULL.
|
153
|
+
|
154
|
+
## Action column
|
155
|
+
|
156
|
+
You can use one column for actions (so it is not related to any db column) just
|
157
|
+
use empty column_key
|
158
|
+
|
159
|
+
```
|
160
|
+
def columns
|
161
|
+
{
|
162
|
+
'posts.title': {},
|
163
|
+
'': { title: "<a href='#'>Check all</a>" },
|
164
|
+
}
|
165
|
+
end
|
166
|
+
|
167
|
+
def rows(filtered)
|
168
|
+
filtered.each do |post|
|
169
|
+
[
|
170
|
+
post.title,
|
171
|
+
"@view"
|
172
|
+
]
|
173
|
+
end
|
174
|
+
end
|
175
|
+
```
|
176
|
+
|
177
|
+
## Params
|
178
|
+
|
179
|
+
To set parameters that you can use for links to set column search value, use
|
180
|
+
this `PostsDatatable.param_set` for example
|
181
|
+
|
182
|
+
```
|
183
|
+
link_to 'Posts for my@email.com and my_title', \
|
184
|
+
posts_path(
|
185
|
+
PostsDatatable.params_set('users.email': 'my@email.com', 'posts.title': 'my_title')
|
186
|
+
.merge(user_id: 1)
|
187
|
+
)
|
188
|
+
# will generate
|
189
|
+
```
|
190
|
+
|
191
|
+
If you need, you can fetch params with this helper
|
192
|
+
|
193
|
+
```
|
194
|
+
PostsDatatable.param_get('users.email', params)
|
195
|
+
```
|
196
|
+
|
197
|
+
## Saved Preferences (optional)
|
198
|
+
|
199
|
+
You can save column order and page length in User.preferences field so
|
200
|
+
next time user navigate to same page will see the same order and page length. It
|
201
|
+
can be `string` or `text`, or some advance `hstore` or `jsonb`.
|
202
|
+
|
203
|
+
```
|
204
|
+
rails g migration add_preferences_to_users preferences:string
|
205
|
+
|
206
|
+
# app/models/user.rb
|
207
|
+
class User
|
208
|
+
# no need to serialize if it is hstore or jsonb
|
209
|
+
serialize :preferences, Hash
|
210
|
+
end
|
211
|
+
|
212
|
+
# app/datatables/posts_datatable.rb
|
213
|
+
class PostsDatatable
|
214
|
+
def preferences_holder
|
215
|
+
@view.current_user
|
216
|
+
end
|
217
|
+
|
218
|
+
def preferences_field
|
219
|
+
# this is default so do not need to define unless you use different field
|
220
|
+
:preferences
|
221
|
+
end
|
222
|
+
end
|
223
|
+
```
|
224
|
+
|
225
|
+
It will store order and page lenght inside `dt_preferences` on
|
226
|
+
`user.preferences`.
|
227
|
+
|
228
|
+
## Debug
|
229
|
+
|
230
|
+
You can override some of the methos and put byebug, for example
|
231
|
+
```
|
232
|
+
# app/datatables/posts_datatable.rb
|
233
|
+
class PostsDatatable < TrkDatatables::ActiveRecord
|
234
|
+
def as_json(_ = nil)
|
235
|
+
byebug
|
236
|
+
super
|
237
|
+
end
|
238
|
+
end
|
239
|
+
|
240
|
+
```
|
241
|
+
|
242
|
+
## Development
|
243
|
+
|
244
|
+
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.
|
245
|
+
|
246
|
+
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).
|
247
|
+
|
248
|
+
To generate docs you can run
|
249
|
+
|
250
|
+
```
|
251
|
+
yard server
|
252
|
+
|
253
|
+
# clear cache
|
254
|
+
rm -rf .yardoc/
|
255
|
+
```
|
256
|
+
|
257
|
+
## Contributing
|
258
|
+
|
259
|
+
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.
|
260
|
+
|
261
|
+
## License
|
262
|
+
|
263
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
264
|
+
|
265
|
+
## Code of Conduct
|
266
|
+
|
267
|
+
Everyone interacting in the TrkDatatables project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/trkin/trk_datatables/blob/master/CODE_OF_CONDUCT.md).
|