ajax-datatables-rails-coveralls 0.0.1
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 +17 -0
- data/.rspec +1 -0
- data/.rvmrc +48 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +142 -0
- data/Rakefile +6 -0
- data/ajax-datatables-rails.gemspec +19 -0
- data/lib/ajax-datatables-rails.rb +79 -0
- data/lib/generators/ajaxdatatable/USAGE +2 -0
- data/lib/generators/ajaxdatatable/ajaxdatatable_generator.rb +8 -0
- data/lib/generators/ajaxdatatable/templates/datatable.rb +33 -0
- data/lib/generators/filterdatatable/USAGE +2 -0
- data/lib/generators/filterdatatable/templates/filter.rb +8 -0
- data/spec/ajax-datatables-rails/ajax_datatables_rails_spec.rb +7 -0
- data/spec/spec_helper.rb +1 -0
- metadata +78 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0ff475314e5279552f9dd3661de1d9b46703234c
|
4
|
+
data.tar.gz: 19c310b583dba22cfc3f0785186b280aabac68cc
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b3672cfae84d784dcdb7b513fbcb356873a5196a5de25103d738d60a83911dea2f339120c32882f0e53f1a09a1ce1d5c8f33f5af58469689f717e4f40f195a16
|
7
|
+
data.tar.gz: 0dbec84b3df0b5a493f5800273958ddcd1b696b206fea5f9efb1222a46af78e83682f1a461929756b71083c6f2c3c5ca95897345d2768d1a7a3e539d1f948738
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/.rvmrc
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
#!/usr/bin/env bash
|
2
|
+
|
3
|
+
# This is an RVM Project .rvmrc file, used to automatically load the ruby
|
4
|
+
# development environment upon cd'ing into the directory
|
5
|
+
|
6
|
+
# First we specify our desired <ruby>[@<gemset>], the @gemset name is optional,
|
7
|
+
# Only full ruby name is supported here, for short names use:
|
8
|
+
# echo "rvm use 1.9.3" > .rvmrc
|
9
|
+
environment_id="ruby-1.9.3-p125@ajax-datatables-rails"
|
10
|
+
|
11
|
+
# Uncomment the following lines if you want to verify rvm version per project
|
12
|
+
# rvmrc_rvm_version="1.11.0-pre" # 1.10.1 seams as a safe start
|
13
|
+
# eval "$(echo ${rvm_version}.${rvmrc_rvm_version} | awk -F. '{print "[[ "$1*65536+$2*256+$3" -ge "$4*65536+$5*256+$6" ]]"}' )" || {
|
14
|
+
# echo "This .rvmrc file requires at least RVM ${rvmrc_rvm_version}, aborting loading."
|
15
|
+
# return 1
|
16
|
+
# }
|
17
|
+
|
18
|
+
# First we attempt to load the desired environment directly from the environment
|
19
|
+
# file. This is very fast and efficient compared to running through the entire
|
20
|
+
# CLI and selector. If you want feedback on which environment was used then
|
21
|
+
# insert the word 'use' after --create as this triggers verbose mode.
|
22
|
+
if [[ -d "${rvm_path:-$HOME/.rvm}/environments"
|
23
|
+
&& -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
|
24
|
+
then
|
25
|
+
\. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
|
26
|
+
[[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]] &&
|
27
|
+
\. "${rvm_path:-$HOME/.rvm}/hooks/after_use" || true
|
28
|
+
else
|
29
|
+
# If the environment file has not yet been created, use the RVM CLI to select.
|
30
|
+
rvm --create "$environment_id" || {
|
31
|
+
echo "Failed to create RVM environment '${environment_id}'."
|
32
|
+
return 1
|
33
|
+
}
|
34
|
+
fi
|
35
|
+
|
36
|
+
# If you use bundler, this might be useful to you:
|
37
|
+
# if [[ -s Gemfile ]] && {
|
38
|
+
# ! builtin command -v bundle >/dev/null ||
|
39
|
+
# builtin command -v bundle | grep $rvm_path/bin/bundle >/dev/null
|
40
|
+
# }
|
41
|
+
# then
|
42
|
+
# printf "%b" "The rubygem 'bundler' is not installed. Installing it now.\n"
|
43
|
+
# gem install bundler
|
44
|
+
# fi
|
45
|
+
# if [[ -s Gemfile ]] && builtin command -v bundle >/dev/null
|
46
|
+
# then
|
47
|
+
# bundle install | grep -vE '^Using|Your bundle is complete'
|
48
|
+
# fi
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Joel Quenneville
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,142 @@
|
|
1
|
+
# ajax-datatables-rails
|
2
|
+
|
3
|
+
Datatables is a nifty jquery plugin that adds the ability to paginate, sort, and search your html tables. When dealing with large tables (more than a couple hundred rows) however, we run into performance issues. These can be fixed by using server-side pagination, but this breaks some datatables functionality.
|
4
|
+
|
5
|
+
`ajax-datatables-rails` is a wrapper around datatable's ajax methods that allow synchronization with server-side pagination in a rails app. It was inspired by this [railscast](http://railscasts.com/episodes/340-datatables). I needed to implement a similar solution in a couple projects I was working on so I extracted it out into a gem.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add these lines to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'jquery-datatables-rails'
|
12
|
+
gem 'ajax-datatables-rails'
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
## Usage
|
19
|
+
*The following examples assume that we are setting up ajax-datatables-rails for an index of users from a `User` model*
|
20
|
+
### Model
|
21
|
+
Run the following command:
|
22
|
+
|
23
|
+
$ rails generate ajaxdatatable User
|
24
|
+
|
25
|
+
This will generate a file named `users_datatable.rb` in `app/datatables`. Open the file and customize in the functions as directed by the comments
|
26
|
+
|
27
|
+
#### Initializer
|
28
|
+
```ruby
|
29
|
+
def initialize(view)
|
30
|
+
@model_name = User
|
31
|
+
@columns = # insert array of column names here
|
32
|
+
@searchable_columns = #insert array of columns that will be searched
|
33
|
+
super(view)
|
34
|
+
end
|
35
|
+
```
|
36
|
+
|
37
|
+
* For `@columns`, assign an array of the database columns that correspond to the columns in our view table. For example `[users.f_name, users.l_name, users.bio]`. This array is used for sorting by various columns
|
38
|
+
|
39
|
+
* For `@searchable_columns`, assign an array of the database columns that you want searchable by datatables. For example `[users.f_name, users.l_name]`
|
40
|
+
|
41
|
+
This gives us:
|
42
|
+
```ruby
|
43
|
+
def initialize(view)
|
44
|
+
@model_name = User
|
45
|
+
@columns = [users.f_name, users.l_name, users.bio]
|
46
|
+
@searchable_columns = [users.f_name, users.l_name]
|
47
|
+
super(view)
|
48
|
+
end
|
49
|
+
```
|
50
|
+
|
51
|
+
#### Data
|
52
|
+
```ruby
|
53
|
+
def data
|
54
|
+
users.map do |user|
|
55
|
+
[
|
56
|
+
# comma separated list of the values for each cell of a table row
|
57
|
+
]
|
58
|
+
end
|
59
|
+
end
|
60
|
+
```
|
61
|
+
|
62
|
+
This method builds a 2d array that is used by datatables to construct the html table. Insert the values you want on each column.
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
def data
|
66
|
+
users.map do |user|
|
67
|
+
[
|
68
|
+
user.f_name,
|
69
|
+
user.l_name,
|
70
|
+
user.bio
|
71
|
+
]
|
72
|
+
end
|
73
|
+
end
|
74
|
+
```
|
75
|
+
|
76
|
+
#### Get Raw Records
|
77
|
+
```ruby
|
78
|
+
def get_raw_records
|
79
|
+
# insert query here
|
80
|
+
end
|
81
|
+
```
|
82
|
+
|
83
|
+
This is where your query goes.
|
84
|
+
|
85
|
+
```ruby
|
86
|
+
def get_raw_records
|
87
|
+
User.all
|
88
|
+
end
|
89
|
+
```
|
90
|
+
|
91
|
+
### Controller
|
92
|
+
Set up the controller to respond to JSON
|
93
|
+
|
94
|
+
```ruby
|
95
|
+
def index
|
96
|
+
respond_to do |format|
|
97
|
+
format.html
|
98
|
+
format.json { render json: UsersDatatable.new(view_context) }
|
99
|
+
end
|
100
|
+
end
|
101
|
+
```
|
102
|
+
|
103
|
+
### View
|
104
|
+
* Set up an html `<table>` with a `<thead>` and `<tbody>`
|
105
|
+
* Add in your table headers if desired
|
106
|
+
* Don't add any rows to the body of the table, datatables does this automatically
|
107
|
+
* Add a data attribute to the `<table>` tag with the url of the JSON feed
|
108
|
+
|
109
|
+
The resulting view may look like this:
|
110
|
+
|
111
|
+
```erb
|
112
|
+
<table id="user-table", data-source="<%= users_path(format: :json) %>">
|
113
|
+
<thead>
|
114
|
+
<tr>
|
115
|
+
<th>First Name</th>
|
116
|
+
<th>Last Name</th>
|
117
|
+
<th>Brief Bio</th>
|
118
|
+
</tr>
|
119
|
+
</thead>
|
120
|
+
<tbody>
|
121
|
+
</tbody>
|
122
|
+
</table>
|
123
|
+
```
|
124
|
+
|
125
|
+
### Javascript
|
126
|
+
Finally, the javascript to tie this all together. In the appropriate `js.coffee` file:
|
127
|
+
|
128
|
+
```coffeescript
|
129
|
+
$ ->
|
130
|
+
$('#users-table').dataTable
|
131
|
+
bProcessing: true
|
132
|
+
bServerSide: true
|
133
|
+
sAjaxSource: $('#users-table').data('source')
|
134
|
+
```
|
135
|
+
|
136
|
+
## Contributing
|
137
|
+
|
138
|
+
1. Fork it
|
139
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
140
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
141
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
142
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/ajax-datatables-rails', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Joel Quenneville", "Nick Merwin"]
|
6
|
+
gem.email = ["joel.quenneville@collegeplus.org", "n@mer.io"]
|
7
|
+
gem.description = %q{Fork for Coveralls app}
|
8
|
+
gem.summary = %q{A wrapper around datatable's ajax methods that allow synchronization with server-side pagination in a rails app}
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "ajax-datatables-rails-coveralls"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = AjaxDatatablesRails::VERSION
|
17
|
+
|
18
|
+
gem.add_development_dependency "rspec"
|
19
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
# require 'rails'
|
2
|
+
|
3
|
+
class AjaxDatatablesRails
|
4
|
+
|
5
|
+
class MethodError < StandardError; end
|
6
|
+
|
7
|
+
VERSION = '0.0.1'
|
8
|
+
|
9
|
+
attr_reader :columns, :model_name, :searchable_columns
|
10
|
+
|
11
|
+
def initialize(view)
|
12
|
+
@view = view
|
13
|
+
end
|
14
|
+
|
15
|
+
def method_missing(meth, *args, &block)
|
16
|
+
@view.send(meth, *args, &block)
|
17
|
+
end
|
18
|
+
|
19
|
+
def as_json(options = {})
|
20
|
+
{
|
21
|
+
sEcho: params[:sEcho].to_i,
|
22
|
+
iTotalRecords: get_raw_record_count,
|
23
|
+
iTotalDisplayRecords: filtered_record_count,
|
24
|
+
aaData: data
|
25
|
+
}
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def data
|
31
|
+
raise MethodError, "The method `data' is not defined."
|
32
|
+
end
|
33
|
+
|
34
|
+
def get_raw_records
|
35
|
+
raise MethodError, "The method `get_raw_records' is not defined."
|
36
|
+
end
|
37
|
+
|
38
|
+
def filtered_record_count
|
39
|
+
search_records(get_raw_records).count
|
40
|
+
end
|
41
|
+
|
42
|
+
def fetch_records
|
43
|
+
search_records(sort_records(paginate_records(get_raw_records)))
|
44
|
+
end
|
45
|
+
|
46
|
+
def paginate_records(records)
|
47
|
+
records.offset((page - 1) * per_page).limit(per_page)
|
48
|
+
end
|
49
|
+
|
50
|
+
def sort_records(records)
|
51
|
+
records.order("#{sort_column} #{sort_direction}")
|
52
|
+
end
|
53
|
+
|
54
|
+
def search_records(records)
|
55
|
+
if params[:sSearch].present?
|
56
|
+
query = @searchable_columns.map do |column|
|
57
|
+
"#{column} LIKE :search"
|
58
|
+
end.join(" OR ")
|
59
|
+
records = records.where(query, search: "%#{params[:sSearch]}%")
|
60
|
+
end
|
61
|
+
return records
|
62
|
+
end
|
63
|
+
|
64
|
+
def page
|
65
|
+
params[:iDisplayStart].to_i/per_page + 1
|
66
|
+
end
|
67
|
+
|
68
|
+
def per_page
|
69
|
+
params[:iDisplayLength].to_i > 0 ? params[:iDisplayLength].to_i : 10
|
70
|
+
end
|
71
|
+
|
72
|
+
def sort_column
|
73
|
+
@columns[params[:iSortCol_0].to_i]
|
74
|
+
end
|
75
|
+
|
76
|
+
def sort_direction
|
77
|
+
params[:sSortDir_0] == "desc" ? "DESC" : "ASC"
|
78
|
+
end
|
79
|
+
end
|
@@ -0,0 +1,8 @@
|
|
1
|
+
class AjaxdatatableGenerator < Rails::Generators::Base
|
2
|
+
source_root File.expand_path('../templates', __FILE__)
|
3
|
+
argument :model, type: :string
|
4
|
+
|
5
|
+
def generate_ajaxdatatable
|
6
|
+
template 'datatable.rb', File.join('app/datatables', "#{model.tableize}_datatable.rb")
|
7
|
+
end
|
8
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
class <%= model.classify.pluralize %>Datatable < AjaxDatatablesRails
|
2
|
+
|
3
|
+
def initialize(view)
|
4
|
+
@model_name = <%= model.classify %>
|
5
|
+
@columns = # insert array of column names here
|
6
|
+
@searchable_columns = #insert array of columns that will be searched
|
7
|
+
super(view)
|
8
|
+
end
|
9
|
+
|
10
|
+
private
|
11
|
+
|
12
|
+
def data
|
13
|
+
<%= model.tableize %>.map do |<%= model.tableize.singularize %>|
|
14
|
+
[
|
15
|
+
# comma separated list of the values for each cell of a table row
|
16
|
+
]
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def <%= model.tableize %>
|
21
|
+
@<%= model.tableize %> ||= fetch_records
|
22
|
+
end
|
23
|
+
|
24
|
+
def get_raw_records
|
25
|
+
# insert query here
|
26
|
+
end
|
27
|
+
|
28
|
+
def get_raw_record_count
|
29
|
+
search_records(get_raw_records).count
|
30
|
+
end
|
31
|
+
|
32
|
+
# ==== Insert 'presenter'-like methods below if necessary
|
33
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'ajax-datatables-rails'
|
metadata
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ajax-datatables-rails-coveralls
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Joel Quenneville
|
8
|
+
- Nick Merwin
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2015-05-18 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - ">="
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '0'
|
21
|
+
type: :development
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '0'
|
28
|
+
description: Fork for Coveralls app
|
29
|
+
email:
|
30
|
+
- joel.quenneville@collegeplus.org
|
31
|
+
- n@mer.io
|
32
|
+
executables: []
|
33
|
+
extensions: []
|
34
|
+
extra_rdoc_files: []
|
35
|
+
files:
|
36
|
+
- ".gitignore"
|
37
|
+
- ".rspec"
|
38
|
+
- ".rvmrc"
|
39
|
+
- Gemfile
|
40
|
+
- LICENSE
|
41
|
+
- README.md
|
42
|
+
- Rakefile
|
43
|
+
- ajax-datatables-rails.gemspec
|
44
|
+
- lib/ajax-datatables-rails.rb
|
45
|
+
- lib/generators/ajaxdatatable/USAGE
|
46
|
+
- lib/generators/ajaxdatatable/ajaxdatatable_generator.rb
|
47
|
+
- lib/generators/ajaxdatatable/templates/datatable.rb
|
48
|
+
- lib/generators/filterdatatable/USAGE
|
49
|
+
- lib/generators/filterdatatable/templates/filter.rb
|
50
|
+
- spec/ajax-datatables-rails/ajax_datatables_rails_spec.rb
|
51
|
+
- spec/spec_helper.rb
|
52
|
+
homepage: ''
|
53
|
+
licenses: []
|
54
|
+
metadata: {}
|
55
|
+
post_install_message:
|
56
|
+
rdoc_options: []
|
57
|
+
require_paths:
|
58
|
+
- lib
|
59
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '0'
|
64
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
requirements: []
|
70
|
+
rubyforge_project:
|
71
|
+
rubygems_version: 2.2.2
|
72
|
+
signing_key:
|
73
|
+
specification_version: 4
|
74
|
+
summary: A wrapper around datatable's ajax methods that allow synchronization with
|
75
|
+
server-side pagination in a rails app
|
76
|
+
test_files:
|
77
|
+
- spec/ajax-datatables-rails/ajax_datatables_rails_spec.rb
|
78
|
+
- spec/spec_helper.rb
|