ez_datatable 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/MIT-LICENSE +20 -0
- data/README.md +109 -0
- data/Rakefile +27 -0
- data/lib/ez_datatable.rb +6 -0
- data/lib/ez_datatable/controller.rb +26 -0
- data/lib/ez_datatable/model.rb +47 -0
- data/lib/ez_datatable/railtie.rb +4 -0
- data/lib/ez_datatable/version.rb +3 -0
- data/lib/generators/ez_datatable/locales/ez_datatable.en.yml +4 -0
- data/lib/generators/ez_datatable/templates/_table.html.erb +89 -0
- data/lib/generators/ez_datatable/view_generator.rb +10 -0
- data/lib/tasks/ez_datatable_tasks.rake +4 -0
- metadata +86 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0632a6d479ebcbd822f99ad5543927e27096607f
|
4
|
+
data.tar.gz: 6e3cb6d929c0e8eeaf322d55d3292384203b2a32
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3f809d88e08241094ff2f1c0191bd19dc6215b8d3aeee2664a8e9302556e4da66d535b459fb98a61906353f51e24fdd2f0b35f1e7e740b3a8e46dc4baaf9bf53
|
7
|
+
data.tar.gz: 971f0a187125d0558a6e22e3714a1406a852a4010ced1326926aae68bd31204f5b58918f2a220f08d01e586f70ae556a4493e3deb16bea31863305bc186cf1ae
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2019 Mic
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
# EzDatatable
|
2
|
+
|
3
|
+
Easy and Customizable datatable for Ruby on Rails. Usually in admin panel you gonna need to list data from your
|
4
|
+
model and have a search bar to filter the data and buttons for View, Edit, Delete model.
|
5
|
+
So This is just another gem that help you handle those kinds of boring jobs.
|
6
|
+
|
7
|
+
*** Currently support only MySQL.
|
8
|
+
|
9
|
+
### Usage
|
10
|
+
|
11
|
+
1 - Add to your Gemfile
|
12
|
+
|
13
|
+
```
|
14
|
+
gem 'ez_datatable'
|
15
|
+
```
|
16
|
+
|
17
|
+
2 - Install the gem
|
18
|
+
|
19
|
+
```
|
20
|
+
bundle install
|
21
|
+
```
|
22
|
+
|
23
|
+
3 - Run the install generator (This will generate view files "views/ez_datatable/_table.html.erb")
|
24
|
+
|
25
|
+
```
|
26
|
+
rails generate ez_datatable:view
|
27
|
+
```
|
28
|
+
|
29
|
+
4 - Include EzDatatable module in controller (yo)
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
include EzDatatable::Controller
|
33
|
+
```
|
34
|
+
|
35
|
+
Example (You can put it one place at "ApplicationController" if you like):
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
class BooksController < ApplicationController
|
39
|
+
include EzDatatable::Controller
|
40
|
+
end
|
41
|
+
```
|
42
|
+
|
43
|
+
5 - Include EzDatatable module in model
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
include EzDatatable::Model
|
47
|
+
```
|
48
|
+
|
49
|
+
Example (You can put it one place at "ApplicationController" if you like):
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
class Book < ApplicationRecord
|
53
|
+
include EzDatatable::Model
|
54
|
+
end
|
55
|
+
```
|
56
|
+
|
57
|
+
6 - **Create class method and instance method in model. see example in the link below (/test/dummy/app/models/book.rb).**
|
58
|
+
|
59
|
+
*** Please read the comments in the example model.
|
60
|
+
|
61
|
+
[Click Here to See Example](test/dummy/app/models/book.rb)
|
62
|
+
|
63
|
+
7 - Setup action in controller (You can use with will_paginate)
|
64
|
+
|
65
|
+
7.1 - You must pass params[:filter] and params[:operator] to "search" method
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
@books = Book.search(params[:filter], params[:operator]).paginate(per_page: 10, page: params[:page])
|
69
|
+
```
|
70
|
+
|
71
|
+
7.2 - You must pass collections and datatable configuration to "_table" method
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
@table = _table(@books, Book.datatable_config)
|
75
|
+
```
|
76
|
+
|
77
|
+
Example:
|
78
|
+
|
79
|
+
```ruby
|
80
|
+
def index
|
81
|
+
@books = Book.search(params[:filter], params[:operator]).paginate(per_page: 10, page: params[:page])
|
82
|
+
@table = _table(@books, Book.datatable_config)
|
83
|
+
end
|
84
|
+
```
|
85
|
+
|
86
|
+
8. Display Datatable in view (example: in index.html.erb)
|
87
|
+
|
88
|
+
```ruby
|
89
|
+
<%= @table %>
|
90
|
+
```
|
91
|
+
|
92
|
+
### Screenshots
|
93
|
+
|
94
|
+
##### Table
|
95
|
+
|
96
|
+

|
97
|
+
|
98
|
+
##### After Search
|
99
|
+
|
100
|
+

|
101
|
+
|
102
|
+
### Todos
|
103
|
+
|
104
|
+
- support PostgreSQL
|
105
|
+
|
106
|
+
### Notes
|
107
|
+
|
108
|
+
I normally use this datatable in every projects but as a services class. So everytime i start a new project have to copy and paste those related files to new projects.
|
109
|
+
Now i decide to build it as a gem so me and my colleagues can use this a lot easier :)
|
data/Rakefile
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'EzDatatable'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.md')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
require 'bundler/gem_tasks'
|
18
|
+
|
19
|
+
require 'rake/testtask'
|
20
|
+
|
21
|
+
Rake::TestTask.new(:test) do |t|
|
22
|
+
t.libs << 'test'
|
23
|
+
t.pattern = 'test/**/*_test.rb'
|
24
|
+
t.verbose = false
|
25
|
+
end
|
26
|
+
|
27
|
+
task default: :test
|
data/lib/ez_datatable.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
module EzDatatable
|
2
|
+
module Controller
|
3
|
+
def _table(collections, config)
|
4
|
+
context = Rails.configuration.paths['app/views']
|
5
|
+
view = ActionView::Base.new(context)
|
6
|
+
view.class.include Rails.application.routes.url_helpers
|
7
|
+
view.render partial: '/ez_datatable/table', locals: { collections: collections, config: config, params: params, request: request }
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.send(collection, attr)
|
11
|
+
val = collection
|
12
|
+
attrs = attr.split('.')
|
13
|
+
if attrs.count > 1
|
14
|
+
attrs.each_with_index do |attr, i|
|
15
|
+
if i == 0
|
16
|
+
attr = attr.singularize
|
17
|
+
end
|
18
|
+
val = val.public_send(attr) if val
|
19
|
+
end
|
20
|
+
else
|
21
|
+
val = val.public_send(attr)
|
22
|
+
end
|
23
|
+
val
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module EzDatatable
|
2
|
+
module Model
|
3
|
+
def self.included(klass)
|
4
|
+
klass.include Rails.application.routes.url_helpers
|
5
|
+
klass.scope :search, -> filters, operator = nil do
|
6
|
+
conditions = []
|
7
|
+
if filters
|
8
|
+
config = self.datatable_config
|
9
|
+
filters.each do |key, val|
|
10
|
+
if config[:search]
|
11
|
+
current_config = config[:search].select { |hash| hash[:attr] == key }.first
|
12
|
+
|
13
|
+
# data_type
|
14
|
+
if current_config && current_config[:data_type]
|
15
|
+
if current_config[:data_type] == 'integer'
|
16
|
+
val = val.to_i
|
17
|
+
else
|
18
|
+
val = val.to_s
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
operator = current_config[:operator] ? current_config[:operator] : ''
|
23
|
+
|
24
|
+
unless val.blank? || val == 0
|
25
|
+
if operator == 'equal'
|
26
|
+
conditions << ["#{key} = '#{val}'"]
|
27
|
+
elsif operator == 'start_between'
|
28
|
+
conditions << ["#{key} >= '#{val}'"]
|
29
|
+
elsif operator == 'end_between'
|
30
|
+
conditions << ["#{key} <= '#{val}'"]
|
31
|
+
elsif operator == 'between'
|
32
|
+
d_start = Date.strptime("#{val.split(' - ')[0]}", '%m/%d/%Y').to_formatted_s(:db)
|
33
|
+
d_end = Date.strptime("#{val.split(' - ')[1]}", '%m/%d/%Y').to_formatted_s(:db)
|
34
|
+
conditions << ["#{key} between '#{d_start}' AND '#{d_end}'"]
|
35
|
+
else
|
36
|
+
conditions << ["#{key} LIKE '%#{val}%'"]
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
where(conditions.join(" AND "))
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
<div class="datatable">
|
2
|
+
<% if config[:search] %>
|
3
|
+
<div class="search">
|
4
|
+
<form method="get" action="" class="search-form row">
|
5
|
+
<input type="hidden" name="search" value="true" />
|
6
|
+
|
7
|
+
<% config[:search].each_with_index do |col, index| %>
|
8
|
+
<div class="form-group <%= config[:num_cols] ? "col-md-#{12 / config[:num_cols]}" : "col-md-6" %>">
|
9
|
+
<label for="search_<%= col[:attr] %>" class="control-label" style="text-align: left;"><%= col[:label] %></label>
|
10
|
+
<% if col[:field] == 'select' %>
|
11
|
+
<select
|
12
|
+
col_<%= col[:attr] %>"
|
13
|
+
name="filter[<%= col[:attr] %>]"
|
14
|
+
class="form-control<%= col[:class_name] ? " #{col[:class_name]}" : "" %>"
|
15
|
+
<%= col[:data] ? (col[:data] || {}).map { |key, val| "data-#{key}=#{val}" }.join(" ") : "" %>
|
16
|
+
>
|
17
|
+
<% col[:options].each do |option| %>
|
18
|
+
<% if params[:filter] && params[:filter][col[:attr].to_sym].to_s == option.last.to_s %>
|
19
|
+
<option value="<%= option.last %>" selected="selected"><%= option.first %></option>
|
20
|
+
<% else %>
|
21
|
+
<option value="<%= option.last %>"><%= option.first %></option>
|
22
|
+
<% end %>
|
23
|
+
<% end %>
|
24
|
+
</select>
|
25
|
+
<% else %>
|
26
|
+
<input
|
27
|
+
type="text"
|
28
|
+
id="col_<%= col[:attr] %>"
|
29
|
+
name="filter[<%= col[:attr] %>]"
|
30
|
+
class="form-control<%= col[:class_name] ? " #{col[:class_name]}" : "" %>"
|
31
|
+
<%= col[:data] ? (col[:data] || {}).map { |key, val| "data-#{key}=#{val}" }.join(" ") : "" %>
|
32
|
+
value="<%= params[:filter][col[:attr].to_sym] if params[:filter] %>"
|
33
|
+
/>
|
34
|
+
<% end %>
|
35
|
+
</div>
|
36
|
+
<% end %>
|
37
|
+
|
38
|
+
<% if !config[:search].empty? %>
|
39
|
+
<div class="col-md-12">
|
40
|
+
<input type="submit" value="<%= config[:search_btn] && config[:search_btn][:label] ? config[:search_btn][:label] : t('ez_datatable.search') %>" class="btn btn-sm <%= config[:search_btn] && config[:search_btn][:class_name] ? config[:search_btn][:class_name] : 'btn-sm btn-primary' %>" />
|
41
|
+
<% if !config.key?(:hide_reset) || config.key?(:hide_reset) && config[:hide_reset] != true %>
|
42
|
+
<a href="<%= request.path %>" class="btn btn-sm btn-primary reset"><%= t('ez_datatable.reset') %></a>
|
43
|
+
<% end %>
|
44
|
+
</div>
|
45
|
+
<% end %>
|
46
|
+
</form>
|
47
|
+
</div>
|
48
|
+
<% end %>
|
49
|
+
|
50
|
+
<div class="table-responsive m-t-20">
|
51
|
+
<table class="table table-hover">
|
52
|
+
<thead>
|
53
|
+
<tr>
|
54
|
+
<% config[:cols].each do |col| %>
|
55
|
+
<th><%= col[:label] ? col[:label] : '' %></th>
|
56
|
+
<% end %>
|
57
|
+
</tr>
|
58
|
+
</thead>
|
59
|
+
|
60
|
+
<tbody>
|
61
|
+
<% collections.each do |collection| %>
|
62
|
+
<tr>
|
63
|
+
<% config[:cols].each do |col| %>
|
64
|
+
<% if col[:attr] %>
|
65
|
+
<% if col[:link] %>
|
66
|
+
<td>
|
67
|
+
<a href="<%= collection.send("datatable_link")[col[:link].to_sym] %>" <%= col[:target] ? "target=_blank" : nil %>><%= collection[col[:attr].to_sym] %></a>
|
68
|
+
</td>
|
69
|
+
<% else %>
|
70
|
+
<td><%= EzDatatable::Controller.send(collection, col[:attr]) %></td>
|
71
|
+
<% end %>
|
72
|
+
<% elsif col[:actions] %>
|
73
|
+
<td class="actions">
|
74
|
+
<% col[:actions].each do |action| %>
|
75
|
+
<% if collection.send("datatable_link")[action[:link].to_sym] %>
|
76
|
+
<a href="<%= collection.send("datatable_link")[action[:link].to_sym] %>" <%= action[:target] ? "target=_blank" : nil %> class="<%= action[:class] %> btn-sm"><%= action[:label] %></a>
|
77
|
+
<% end %>
|
78
|
+
<% end %>
|
79
|
+
</td>
|
80
|
+
<% else %>
|
81
|
+
<td>-</td>
|
82
|
+
<% end %>
|
83
|
+
<% end %>
|
84
|
+
</tr>
|
85
|
+
<% end %>
|
86
|
+
</tbody>
|
87
|
+
</table>
|
88
|
+
</div>
|
89
|
+
</div>
|
@@ -0,0 +1,10 @@
|
|
1
|
+
module EzDatatable
|
2
|
+
class ViewGenerator < Rails::Generators::Base
|
3
|
+
source_root File.expand_path('templates', __dir__)
|
4
|
+
|
5
|
+
def create_initializer_file
|
6
|
+
copy_file "_table.html.erb", "app/views/ez_datatable/_table.html.erb"
|
7
|
+
copy_file "../locales/ez_datatable.en.yml", "config/locales/ez_datatable.en.yml"
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ez_datatable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mic
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-02-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 5.2.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 5.2.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: sqlite3
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: Easy and Customizable datatable for Ruby on Rails (Currently support
|
42
|
+
only MySQL).
|
43
|
+
email:
|
44
|
+
- kittiphong.mic@gmail.com
|
45
|
+
executables: []
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- MIT-LICENSE
|
50
|
+
- README.md
|
51
|
+
- Rakefile
|
52
|
+
- lib/ez_datatable.rb
|
53
|
+
- lib/ez_datatable/controller.rb
|
54
|
+
- lib/ez_datatable/model.rb
|
55
|
+
- lib/ez_datatable/railtie.rb
|
56
|
+
- lib/ez_datatable/version.rb
|
57
|
+
- lib/generators/ez_datatable/locales/ez_datatable.en.yml
|
58
|
+
- lib/generators/ez_datatable/templates/_table.html.erb
|
59
|
+
- lib/generators/ez_datatable/view_generator.rb
|
60
|
+
- lib/tasks/ez_datatable_tasks.rake
|
61
|
+
homepage: https://github.com/michelloworld
|
62
|
+
licenses:
|
63
|
+
- MIT
|
64
|
+
metadata: {}
|
65
|
+
post_install_message:
|
66
|
+
rdoc_options: []
|
67
|
+
require_paths:
|
68
|
+
- lib
|
69
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: '0'
|
79
|
+
requirements: []
|
80
|
+
rubyforge_project:
|
81
|
+
rubygems_version: 2.5.1
|
82
|
+
signing_key:
|
83
|
+
specification_version: 4
|
84
|
+
summary: Easy and Customizable datatable for Ruby on Rails (Currently support only
|
85
|
+
MySQL).
|
86
|
+
test_files: []
|