trk_datatables 0.1.16 → 0.1.17
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 +16 -2
- data/lib/generators/templates/trk_datatable.rb +29 -0
- data/lib/generators/trk_datatables/trk_datatables_generator.rb +21 -0
- data/lib/trk_datatables/column_key_options.rb +8 -1
- data/lib/trk_datatables/render_html.rb +3 -2
- data/lib/trk_datatables/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 07c3787293eeff0d7de126d9f7396ab7e2b50b8115fb8ff7bdc082c5430729e5
|
4
|
+
data.tar.gz: b051feaec9ea1d1278fb34426c2641cc7073d9ba1a0428288661a833ca2fa66a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 72a519b5c19de4dad5412e4089fb07a87a1537c897012e3562b474bcb0ea73fa8744faa51ce3900a170b5c5f2b4ca85b15a671fc3e92dc0a8b37e3529e067c62
|
7
|
+
data.tar.gz: 84668274b21444a0eb75505250d0b8b192c65ac8b38f221cdf4b85aa5da5593dc0e82c5ed02a32a683b6f89fa674f424a7b5de6528d91d922c6050580a317750
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -499,6 +499,20 @@ class PostsDatatableTest < ActiveSupport::TestCase
|
|
499
499
|
end
|
500
500
|
```
|
501
501
|
|
502
|
+
# Exceptions
|
503
|
+
|
504
|
+
To catch errors from TrkDatables you can
|
505
|
+
|
506
|
+
```
|
507
|
+
# app/controllers/application_controller.rb
|
508
|
+
rescue_from TrkDatatables::Error do |exception|
|
509
|
+
respond_to do |format|
|
510
|
+
format.html { redirect_to root_path, alert: exception.message }
|
511
|
+
format.json { render json: { error_message: exception.message, error_status: :bad_request }, status: :bad_request }
|
512
|
+
end
|
513
|
+
end
|
514
|
+
```
|
515
|
+
|
502
516
|
## Debug
|
503
517
|
|
504
518
|
You can override some of the methos and put byebug, for example
|
@@ -537,13 +551,13 @@ You can also run `bin/console` for an interactive prompt that will allow you to
|
|
537
551
|
|
538
552
|
To install this gem onto your local machine, run `bundle exec rake install`. To
|
539
553
|
release a new version, update the version number in
|
540
|
-
`lib/trk_datatables/version.rb`, and then
|
554
|
+
`lib/trk_datatables/version.rb`, and then publish with
|
541
555
|
|
542
556
|
```
|
543
557
|
bundle exec rake release
|
544
558
|
```
|
545
559
|
|
546
|
-
which will create a git tag for the version, push git commits and tags, and
|
560
|
+
which will create a git tag for the version, push git commits and tags, and deploy the
|
547
561
|
`.gem` file to [rubygems.org](https://rubygems.org).
|
548
562
|
|
549
563
|
Instead of installing you can point directly to your path
|
@@ -0,0 +1,29 @@
|
|
1
|
+
class <%= class_name.pluralize %>Datatable < TrkDatatables::ActiveRecord
|
2
|
+
def columns
|
3
|
+
{
|
4
|
+
<% class_name.constantize.columns.each do |column| -%>
|
5
|
+
<% next if %w[created_at updated_at].include? column.name -%>
|
6
|
+
'<%= table_name %>.<%= column.name %>': {},
|
7
|
+
<% end -%>
|
8
|
+
}
|
9
|
+
end
|
10
|
+
|
11
|
+
def all_items
|
12
|
+
<%= class_name %>.all
|
13
|
+
end
|
14
|
+
|
15
|
+
def rows(filtered)
|
16
|
+
filtered.map do |<%= singular_table_name %>|
|
17
|
+
[
|
18
|
+
<% class_name.constantize.columns.each do |column| -%>
|
19
|
+
<% next if %w[created_at updated_at].include? column.name -%>
|
20
|
+
<% if column.name == 'id' -%>
|
21
|
+
@view.link_to(<%= singular_table_name %>.id, <%= singular_table_name %>),
|
22
|
+
<% else -%>
|
23
|
+
<%= singular_table_name %>.<%= column.name %>,
|
24
|
+
<% end -%>
|
25
|
+
<% end -%>
|
26
|
+
]
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module TrkDatatables
|
2
|
+
module Generators
|
3
|
+
class TrkDatatablesGenerator < Rails::Generators::NamedBase
|
4
|
+
MissingModelError = Class.new(Thor::Error)
|
5
|
+
# we can call with `rails g trk_datatables` instead of: `rails g trk_datatables:trk_datatables`
|
6
|
+
namespace 'trk_datatables'
|
7
|
+
source_root File.expand_path('../templates', __dir__)
|
8
|
+
|
9
|
+
desc 'Generates datatables file for a give NAME'
|
10
|
+
def create
|
11
|
+
begin
|
12
|
+
class_name.constantize
|
13
|
+
rescue NameError => e
|
14
|
+
raise MissingModelError, e.message
|
15
|
+
end
|
16
|
+
|
17
|
+
template 'trk_datatable.rb', "app/datatables/#{plural_name}_datatable.rb"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -100,7 +100,7 @@ module TrkDatatables
|
|
100
100
|
column_name: column_name,
|
101
101
|
column_type_in_db: column_type_in_db,
|
102
102
|
# the following are used for RenderHtml
|
103
|
-
title: column_options[TITLE_OPTION] || column_name
|
103
|
+
title: column_options[TITLE_OPTION] || _determine_column_name(table_class, column_name),
|
104
104
|
html_options: html_options(column_options, column_type_in_db),
|
105
105
|
}
|
106
106
|
end
|
@@ -145,6 +145,13 @@ module TrkDatatables
|
|
145
145
|
end
|
146
146
|
end
|
147
147
|
|
148
|
+
def _determine_column_name(table_class, column_name)
|
149
|
+
return column_name.humanize if table_class.blank?
|
150
|
+
|
151
|
+
# maybe we should check if human_attribute_name exists
|
152
|
+
table_class.human_attribute_name column_name
|
153
|
+
end
|
154
|
+
|
148
155
|
def searchable
|
149
156
|
@data.reject do |column_key_option|
|
150
157
|
column_key_option[:column_options][SEARCH_OPTION] == false
|
@@ -2,7 +2,7 @@ module TrkDatatables
|
|
2
2
|
# rubocop:disable Rails/OutputSafety
|
3
3
|
class RenderHtml
|
4
4
|
@indent = 0
|
5
|
-
def initialize(search_link, datatable, html_options)
|
5
|
+
def initialize(search_link, datatable, html_options = {})
|
6
6
|
@search_link = search_link
|
7
7
|
@datatable = datatable
|
8
8
|
@html_options = html_options
|
@@ -36,6 +36,7 @@ module TrkDatatables
|
|
36
36
|
# _content_tag :div, class: 'background' do
|
37
37
|
def _content_tag(tag, options = {}, content = nil)
|
38
38
|
if !options.is_a?(Hash)
|
39
|
+
# content is first argument, do not pass hash as content
|
39
40
|
inline = true
|
40
41
|
content = options
|
41
42
|
options = {}
|
@@ -120,7 +121,7 @@ module TrkDatatables
|
|
120
121
|
safe_join(@datatable.rows(@datatable.ordered_paginated_filtered_items).map do |row|
|
121
122
|
_content_tag :tr do
|
122
123
|
safe_join(row.map do |col|
|
123
|
-
_content_tag :td, col
|
124
|
+
_content_tag :td, col.to_s
|
124
125
|
end)
|
125
126
|
end
|
126
127
|
end, "\n".html_safe)
|
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.17
|
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-
|
11
|
+
date: 2019-10-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -168,6 +168,8 @@ files:
|
|
168
168
|
- Rakefile
|
169
169
|
- bin/console
|
170
170
|
- bin/setup
|
171
|
+
- lib/generators/templates/trk_datatable.rb
|
172
|
+
- lib/generators/trk_datatables/trk_datatables_generator.rb
|
171
173
|
- lib/trk_datatables.rb
|
172
174
|
- lib/trk_datatables/active_record.rb
|
173
175
|
- lib/trk_datatables/base.rb
|