datatables_server 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 +22 -0
- data/.travis.yml +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +153 -0
- data/Rakefile +9 -0
- data/datatables_server.gemspec +31 -0
- data/lib/datatables_server.rb +4 -0
- data/lib/datatables_server/active_record_repository.rb +38 -0
- data/lib/datatables_server/base.rb +78 -0
- data/lib/datatables_server/custom_errors.rb +4 -0
- data/lib/datatables_server/repository.rb +16 -0
- data/lib/datatables_server/repository_factory.rb +16 -0
- data/lib/datatables_server/version.rb +3 -0
- data/test/minitest_helper.rb +7 -0
- data/test/test_active_record_repository.rb +168 -0
- data/test/test_datatables_server.rb +69 -0
- data/test/test_repository_factory.rb +24 -0
- metadata +167 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d1b5dbfd6068d8f3c8be2c0f75dec05be475a6c9
|
4
|
+
data.tar.gz: 34396a21222811c9e5cd8c052f59f7d563b7b32d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 91ad2a0e5612dd120e98901004f83e65734369fd396b0ac801d2178a396388ad05247f0c1be9047330dacb3654c3a01573a8b1f89b308eba3e47124dbeba8300
|
7
|
+
data.tar.gz: d4a7c28158c1e9c3ea160339eeb17d5290872d2d5287049009f6785bcf33b923e7d6b88d59bd93930765f3eae1ca22a1fea64fb23a53d1da847d82f1281ce2be
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Diego Mónaco
|
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,153 @@
|
|
1
|
+
# DatatablesServer
|
2
|
+
|
3
|
+
[](https://travis-ci.org/dfmonaco/datatables_server)
|
4
|
+
[](https://coveralls.io/r/dfmonaco/datatables_server)
|
5
|
+
|
6
|
+
DatatablesServer will receive a number of variables from a DataTables client and
|
7
|
+
it will perform all the required processing (i.e. when paging, sorting, filtering etc),
|
8
|
+
and then return the data in the format required by DataTables.
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
gem 'datatables_server'
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
|
22
|
+
$ gem install datatables_server
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
### Basic usage
|
26
|
+
To use DatatablesServer you just need to inherit from `DatatablesServer::Base` and implement two methods: `#data`, wich must return
|
27
|
+
an `ActiveRecord::Relation` and `#columns` wich must return the columns used in the client table in the right order and represented
|
28
|
+
as an array of strings in the form `'table_name.column_name'`.
|
29
|
+
|
30
|
+
Datatables does NOT depend on Rails, it can be used perfectly with any framework, right now the only dependency it's ActiveRecord,
|
31
|
+
but it's very easy to implement an adapter for other ORMs.
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
# product.rb
|
35
|
+
|
36
|
+
class Product < ActiveRecord::Base
|
37
|
+
end
|
38
|
+
```
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
#product_datatables
|
42
|
+
|
43
|
+
class ProductDatatables < DatatablesServer::Base
|
44
|
+
|
45
|
+
def data
|
46
|
+
Product.all
|
47
|
+
end
|
48
|
+
|
49
|
+
def columns
|
50
|
+
%w(products.name products.price products.description)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
```
|
54
|
+
And that's it!, DatatablesServer will handle paging, sorting and filtering by returning the right JSON document to the client,
|
55
|
+
you just have to instantiate it with the params sent by DataTables, and call the `#as_json` mehtod.
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
ProductDatatables.new(params).as_json # => JSON document to be consumed by the client
|
59
|
+
```
|
60
|
+
|
61
|
+
### Processing raw data
|
62
|
+
If you want to process the raw data that's coming from the datatabase you can implement a method with the same name as the column
|
63
|
+
and do whatever you want with the data.
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
class ProductDatatables < DatatablesServer::Base
|
67
|
+
....
|
68
|
+
|
69
|
+
def name(raw_name)
|
70
|
+
raw_name.capitalize
|
71
|
+
end
|
72
|
+
|
73
|
+
def price(raw_price)
|
74
|
+
"$ #{raw_price}"
|
75
|
+
end
|
76
|
+
end
|
77
|
+
```
|
78
|
+
|
79
|
+
### Wroking with joins
|
80
|
+
To work with joins you don't have to do anything special, just define the required methods as before.
|
81
|
+
|
82
|
+
```ruby
|
83
|
+
#product_datatables
|
84
|
+
|
85
|
+
class ProductDatatables < DatatablesServer::Base
|
86
|
+
|
87
|
+
# Product belongs_to :supplier
|
88
|
+
def data
|
89
|
+
Product.select('products.name', 'suppliers.email').joins(:supplier)
|
90
|
+
end
|
91
|
+
|
92
|
+
def columns
|
93
|
+
%w(products.name suppliers.email)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
```
|
97
|
+
### Rails example
|
98
|
+
As I said DatatablesServer does not depend on Rails, this is just an example of a possible implementation.
|
99
|
+
|
100
|
+
```ruby
|
101
|
+
# app/controllers/products_controller.rb
|
102
|
+
|
103
|
+
class ProductsController < ApplicationController
|
104
|
+
def index
|
105
|
+
respond_to do |format|
|
106
|
+
format.html
|
107
|
+
# you can pass the view_context if you want to use helper methods
|
108
|
+
format.json {render json: ProductDatatables.new(view_context)}
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
```
|
113
|
+
```ruby
|
114
|
+
# app/datatables/product.rb
|
115
|
+
|
116
|
+
class ProductDatatables < DatatablesServer::Base
|
117
|
+
|
118
|
+
attr_reader :h
|
119
|
+
|
120
|
+
def initialize(view_context)
|
121
|
+
super(view_context.params)
|
122
|
+
@h = view_context
|
123
|
+
end
|
124
|
+
|
125
|
+
def data
|
126
|
+
Product.in_stock
|
127
|
+
end
|
128
|
+
|
129
|
+
def columns
|
130
|
+
%w(products.name products.price products.description)
|
131
|
+
end
|
132
|
+
|
133
|
+
def price(raw_price)
|
134
|
+
h.number_to_currency(price)
|
135
|
+
end
|
136
|
+
end
|
137
|
+
```
|
138
|
+
|
139
|
+
## Compatibility
|
140
|
+
|
141
|
+
For any given version, check `.travis.yml` to see what Ruby versions are being tested for compatibility.
|
142
|
+
|
143
|
+
## Contributing
|
144
|
+
|
145
|
+
1. Fork it ( https://github.com/[my-github-username]/datatables_server/fork )
|
146
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
147
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
148
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
149
|
+
5. Create a new Pull Request
|
150
|
+
|
151
|
+
## License
|
152
|
+
|
153
|
+
__MIT License__. *Copyright 2014 Diego Mónaco*
|
data/Rakefile
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'datatables_server/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "datatables_server"
|
8
|
+
spec.version = DatatablesServer::VERSION
|
9
|
+
spec.authors = ["Diego Mónaco"]
|
10
|
+
spec.email = ["dfmonaco@gmail.com"]
|
11
|
+
spec.summary = %q{Server-side processing for DataTables in Ruby}
|
12
|
+
spec.description = %q{DatatablesServer will receive a number of variables from a Datatables client and
|
13
|
+
it will perform all the required processing (i.e. when paging, sorting, filtering etc),
|
14
|
+
and then return the data in the format required by DataTables}
|
15
|
+
spec.homepage = "https://github.com/dfmonaco/datatables_server"
|
16
|
+
spec.license = "MIT"
|
17
|
+
|
18
|
+
spec.files = `git ls-files -z`.split("\x0")
|
19
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
20
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.required_ruby_version = '>= 1.9.2'
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
25
|
+
spec.add_development_dependency "rake"
|
26
|
+
spec.add_development_dependency "pry-debugger"
|
27
|
+
spec.add_development_dependency "sqlite3"
|
28
|
+
spec.add_development_dependency "database_cleaner"
|
29
|
+
spec.add_development_dependency "activerecord"
|
30
|
+
spec.add_development_dependency "coveralls"
|
31
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module DatatablesServer
|
2
|
+
class ActiveRecordRepository
|
3
|
+
def initialize(data, columns, options)
|
4
|
+
@data = data
|
5
|
+
@columns = columns
|
6
|
+
@options = options
|
7
|
+
end
|
8
|
+
|
9
|
+
def count_all
|
10
|
+
data.count
|
11
|
+
end
|
12
|
+
|
13
|
+
def count_filtered
|
14
|
+
filtered_data.count
|
15
|
+
end
|
16
|
+
|
17
|
+
def paginated_data
|
18
|
+
ordered_data.limit(options.page_size).offset(options.page_start)
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
attr_reader :data, :options, :columns
|
24
|
+
|
25
|
+
def ordered_data
|
26
|
+
filtered_data.order("#{options.sort_column} #{options.sort_direction}")
|
27
|
+
end
|
28
|
+
|
29
|
+
def filtered_data
|
30
|
+
return data if options.search_term.empty?
|
31
|
+
data.where(conditions, search: "%#{options.search_term}%")
|
32
|
+
end
|
33
|
+
|
34
|
+
def conditions
|
35
|
+
columns.join(' LIKE :search OR ') << ' LIKE :search'
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
require 'datatables_server/version'
|
2
|
+
require 'datatables_server/custom_errors'
|
3
|
+
require 'datatables_server/repository_factory'
|
4
|
+
|
5
|
+
module DatatablesServer
|
6
|
+
class Base
|
7
|
+
def initialize(params)
|
8
|
+
@params = params
|
9
|
+
end
|
10
|
+
|
11
|
+
def as_json(options = {})
|
12
|
+
{
|
13
|
+
sEcho: s_echo,
|
14
|
+
iTotalRecords: total_records_count,
|
15
|
+
iTotalDisplayRecords: filtered_records_count,
|
16
|
+
aaData: aa_data
|
17
|
+
}
|
18
|
+
end
|
19
|
+
|
20
|
+
def data
|
21
|
+
raise MethodNotImplementedError
|
22
|
+
end
|
23
|
+
|
24
|
+
def columns
|
25
|
+
raise MethodNotImplementedError
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
attr_reader :params
|
31
|
+
|
32
|
+
def s_echo
|
33
|
+
params[:sEcho].to_i
|
34
|
+
end
|
35
|
+
|
36
|
+
def total_records_count
|
37
|
+
repository.count_all
|
38
|
+
end
|
39
|
+
|
40
|
+
def filtered_records_count
|
41
|
+
repository.count_filtered
|
42
|
+
end
|
43
|
+
|
44
|
+
def aa_data
|
45
|
+
repository.paginated_data.map do |datum|
|
46
|
+
attributes.inject([]) do |array, column|
|
47
|
+
raw_value = datum.public_send(column)
|
48
|
+
if respond_to?(column)
|
49
|
+
array << public_send(column, raw_value)
|
50
|
+
else
|
51
|
+
array << raw_value
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def attributes
|
58
|
+
@attributes ||= columns.map do |column|
|
59
|
+
column.split('.').last
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def options
|
64
|
+
OpenStruct.new.tap do |o|
|
65
|
+
o.page_start = params[:iDisplayStart].to_i
|
66
|
+
o.page_size = params[:iDisplayLength].to_i
|
67
|
+
o.sort_column = columns[params[:iSortCol_0].to_i]
|
68
|
+
o.sort_direction = (params[:sSortDir_0] == 'desc' ? 'DESC' : 'ASC')
|
69
|
+
o.search_term = params[:sSearch]
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def repository
|
74
|
+
@repository ||= RepositoryFactory.create(data, columns, options)
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'datatables_server/active_record_repository'
|
2
|
+
|
3
|
+
module DatatablesServer
|
4
|
+
class RepositoryFactory
|
5
|
+
def self.create(data, columns, options)
|
6
|
+
case
|
7
|
+
when defined?(ActiveRecord::Relation) && data.is_a?(ActiveRecord::Relation)
|
8
|
+
ActiveRecordRepository.new(data, columns, options)
|
9
|
+
when defined?(Sequel::Model) && data.is_a?(Sequel::Model)
|
10
|
+
raise RepositoryNotImplementedError
|
11
|
+
else
|
12
|
+
raise RepositoryNotImplementedError
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'datatables_server/active_record_repository'
|
2
|
+
|
3
|
+
module DatatablesServer
|
4
|
+
class RepositoryFactory
|
5
|
+
def self.create(data, columns, options)
|
6
|
+
case
|
7
|
+
when defined?(ActiveRecord::Relation) && data.is_a?(ActiveRecord::Relation)
|
8
|
+
ActiveRecordRepository.new(data, columns, options)
|
9
|
+
when defined?(Sequel::Model) && data.is_a?(Sequel::Model)
|
10
|
+
raise RepositoryNotImplementedError
|
11
|
+
else
|
12
|
+
raise RepositoryNotImplementedError
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,168 @@
|
|
1
|
+
require 'minitest_helper'
|
2
|
+
|
3
|
+
require 'sqlite3'
|
4
|
+
require 'active_record'
|
5
|
+
require 'database_cleaner'
|
6
|
+
|
7
|
+
unless File.exists? 'test.db'
|
8
|
+
SQLite3::Database.new "test.db"
|
9
|
+
|
10
|
+
ActiveRecord::Base.establish_connection(adapter: :sqlite3, database: "test.db")
|
11
|
+
|
12
|
+
ActiveRecord::Schema.define do
|
13
|
+
create_table :contacts do |t|
|
14
|
+
t.column :name, :string
|
15
|
+
t.column :age, :integer
|
16
|
+
end
|
17
|
+
|
18
|
+
create_table :addresses do |t|
|
19
|
+
t.column :street, :string
|
20
|
+
t.column :contact_id, :integer
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
ActiveRecord::Base.establish_connection(adapter: :sqlite3, database: "test.db")
|
26
|
+
|
27
|
+
class Contact < ActiveRecord::Base
|
28
|
+
has_one :address
|
29
|
+
end
|
30
|
+
|
31
|
+
class Address < ActiveRecord::Base
|
32
|
+
belongs_to :contact
|
33
|
+
end
|
34
|
+
|
35
|
+
module DatatablesServer
|
36
|
+
describe ActiveRecordRepository do
|
37
|
+
before do
|
38
|
+
DatabaseCleaner.start
|
39
|
+
end
|
40
|
+
|
41
|
+
after do
|
42
|
+
DatabaseCleaner.clean
|
43
|
+
end
|
44
|
+
|
45
|
+
describe '#count_all' do
|
46
|
+
it 'counts all the records from the relation' do
|
47
|
+
3.times { Contact.create }
|
48
|
+
data = Contact.all
|
49
|
+
columns = []
|
50
|
+
options = OpenStruct.new
|
51
|
+
|
52
|
+
repository = ActiveRecordRepository.new(data, columns, options)
|
53
|
+
|
54
|
+
assert_equal 3, repository.count_all
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
describe '#count_filtered' do
|
59
|
+
it 'counts the filtered records from the relation' do
|
60
|
+
3.times {|n| Contact.create(name: "name_#{n}", age: n)}
|
61
|
+
data = Contact.all
|
62
|
+
columns = %w(contacts.name contacts.age)
|
63
|
+
options = OpenStruct.new(search_term: '2')
|
64
|
+
|
65
|
+
repository = ActiveRecordRepository.new(data, columns, options)
|
66
|
+
|
67
|
+
assert_equal 1, repository.count_filtered
|
68
|
+
end
|
69
|
+
|
70
|
+
it 'counts all the records if search term is blank' do
|
71
|
+
3.times {|n| Contact.create(name: "name_#{n}", age: n)}
|
72
|
+
data = Contact.all
|
73
|
+
columns = %w(contacts.name contacts.age)
|
74
|
+
options = OpenStruct.new(search_term: '')
|
75
|
+
|
76
|
+
repository = ActiveRecordRepository.new(data, columns, options)
|
77
|
+
|
78
|
+
assert_equal 3, repository.count_filtered
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
describe '#paginated_data' do
|
83
|
+
it 'returns the data paginated' do
|
84
|
+
10.times {|n| Contact.create(name: "name_#{n}", age: n)}
|
85
|
+
data = Contact.all
|
86
|
+
columns = %w(contacts.name contacts.age)
|
87
|
+
options = OpenStruct.new.tap do |o|
|
88
|
+
o.page_start = 4
|
89
|
+
o.page_size = 2
|
90
|
+
o.sort_column = 'contacts.name'
|
91
|
+
o.sort_direction = 'ASC'
|
92
|
+
o.search_term = ''
|
93
|
+
end
|
94
|
+
repository = ActiveRecordRepository.new(data, columns, options)
|
95
|
+
|
96
|
+
paginated_data = repository.paginated_data
|
97
|
+
|
98
|
+
assert_equal ['name_4', 'name_5'], paginated_data.map(&:name)
|
99
|
+
end
|
100
|
+
|
101
|
+
it 'returns the data in the right order' do
|
102
|
+
Contact.create(age: 32)
|
103
|
+
Contact.create(age: 48)
|
104
|
+
Contact.create(age: 5)
|
105
|
+
data = Contact.all
|
106
|
+
columns = %w(contacts.name contacts.age)
|
107
|
+
options = OpenStruct.new.tap do |o|
|
108
|
+
o.page_start = 0
|
109
|
+
o.page_size = 3
|
110
|
+
o.sort_column = 'contacts.age'
|
111
|
+
o.sort_direction = 'DESC'
|
112
|
+
o.search_term = ''
|
113
|
+
end
|
114
|
+
repository = ActiveRecordRepository.new(data, columns, options)
|
115
|
+
|
116
|
+
paginated_data = repository.paginated_data
|
117
|
+
|
118
|
+
assert_equal [48, 32, 5], paginated_data.map(&:age)
|
119
|
+
end
|
120
|
+
|
121
|
+
it 'returns the data filtered' do
|
122
|
+
Contact.create(age: 32, name: 'foo')
|
123
|
+
Contact.create(age: 48, name: 'bar')
|
124
|
+
Contact.create(age: 5, name: 'foo')
|
125
|
+
data = Contact.all
|
126
|
+
columns = %w(contacts.name contacts.age)
|
127
|
+
options = OpenStruct.new.tap do |o|
|
128
|
+
o.page_start = 0
|
129
|
+
o.page_size = 3
|
130
|
+
o.sort_column = 'contacts.age'
|
131
|
+
o.sort_direction = 'ASC'
|
132
|
+
o.search_term = 'fo'
|
133
|
+
end
|
134
|
+
repository = ActiveRecordRepository.new(data, columns, options)
|
135
|
+
|
136
|
+
paginated_data = repository.paginated_data
|
137
|
+
|
138
|
+
assert_equal [5, 32], paginated_data.map(&:age)
|
139
|
+
end
|
140
|
+
|
141
|
+
it 'works with joins' do
|
142
|
+
c1 = Contact.create(name: 'bar', age: 32)
|
143
|
+
c2 = Contact.create(name: 'foo', age: 23)
|
144
|
+
c3 = Contact.create(name: 'bar', age: 78)
|
145
|
+
Address.create(street: 'street 1', contact: c1)
|
146
|
+
Address.create(street: 'street 2', contact: c2)
|
147
|
+
Address.create(street: 'street 3', contact: c3)
|
148
|
+
data = Contact.select('contacts.name', 'addresses.street').joins(:address)
|
149
|
+
columns = %w(contacts.name addresses.street)
|
150
|
+
options = OpenStruct.new.tap do |o|
|
151
|
+
o.page_start = 0
|
152
|
+
o.page_size = 3
|
153
|
+
o.sort_column = 'addresses.street'
|
154
|
+
o.sort_direction = 'DESC'
|
155
|
+
o.search_term = 'ar'
|
156
|
+
end
|
157
|
+
repository = ActiveRecordRepository.new(data, columns, options)
|
158
|
+
|
159
|
+
expected_data = [['bar', 'street 3'], ['bar', 'street 1']]
|
160
|
+
actual_data = repository.paginated_data.map {|d| [d.name, d.street]}
|
161
|
+
|
162
|
+
assert_equal expected_data, actual_data
|
163
|
+
end
|
164
|
+
end
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'minitest_helper'
|
2
|
+
require 'ostruct'
|
3
|
+
|
4
|
+
describe DatatablesServer::Base do
|
5
|
+
describe 'raising MethodNotImplementedError' do
|
6
|
+
before do
|
7
|
+
class Foo < DatatablesServer::Base
|
8
|
+
end
|
9
|
+
|
10
|
+
@foo = Foo.new({})
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'raises an exception if #data is not implemented' do
|
14
|
+
assert_raises(DatatablesServer::MethodNotImplementedError) do
|
15
|
+
@foo.data
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'raises an exception if #columns is not implemented' do
|
20
|
+
assert_raises(DatatablesServer::MethodNotImplementedError) do
|
21
|
+
@foo.columns
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe '#as_json' do
|
27
|
+
before do
|
28
|
+
class ContactsDatatatables < DatatablesServer::Base
|
29
|
+
def data
|
30
|
+
end
|
31
|
+
|
32
|
+
def columns
|
33
|
+
%w(contacts.name contacts.age)
|
34
|
+
end
|
35
|
+
|
36
|
+
def name(value)
|
37
|
+
value.upcase
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
# Mock repository, we don't need a mock framework, thanks Ruby!
|
42
|
+
# repository is a collaborator so we can mock the expected interface
|
43
|
+
def repository
|
44
|
+
OpenStruct.new.tap do |r|
|
45
|
+
r.count_all = 57
|
46
|
+
r.count_filtered = 32
|
47
|
+
r.paginated_data = [OpenStruct.new(name: 'foo', age: 21),
|
48
|
+
OpenStruct.new(name: 'bar', age: 12)]
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'returns a hash with the right keys and values' do
|
55
|
+
params = { sEcho: '1' }
|
56
|
+
dt_server = ContactsDatatatables.new(params)
|
57
|
+
expected_hash = {
|
58
|
+
sEcho: 1,
|
59
|
+
iTotalRecords: 57,
|
60
|
+
iTotalDisplayRecords: 32,
|
61
|
+
aaData: [['FOO', 21], ['BAR', 12]]
|
62
|
+
}
|
63
|
+
|
64
|
+
generated_hash = dt_server.as_json
|
65
|
+
|
66
|
+
assert_equal expected_hash, generated_hash
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'minitest_helper'
|
2
|
+
|
3
|
+
module DatatablesServer
|
4
|
+
describe RepositoryFactory do
|
5
|
+
it 'creates the right repository from the data class' do
|
6
|
+
module ActiveRecord
|
7
|
+
class Relation; end
|
8
|
+
end
|
9
|
+
class Foo < ActiveRecord::Relation; end
|
10
|
+
|
11
|
+
foo = Foo.new
|
12
|
+
|
13
|
+
repository = RepositoryFactory.create(foo, [], Object.new)
|
14
|
+
|
15
|
+
assert_equal ActiveRecordRepository, repository.class
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'raises an error if the repository is not implemented' do
|
19
|
+
assert_raises(RepositoryNotImplementedError) do
|
20
|
+
repository = RepositoryFactory.create(Object.new, [], Object.new)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,167 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: datatables_server
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Diego Mónaco
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-06-16 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
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
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pry-debugger
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: sqlite3
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: database_cleaner
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: activerecord
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: coveralls
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - '>='
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - '>='
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
description: |-
|
112
|
+
DatatablesServer will receive a number of variables from a Datatables client and
|
113
|
+
it will perform all the required processing (i.e. when paging, sorting, filtering etc),
|
114
|
+
and then return the data in the format required by DataTables
|
115
|
+
email:
|
116
|
+
- dfmonaco@gmail.com
|
117
|
+
executables: []
|
118
|
+
extensions: []
|
119
|
+
extra_rdoc_files: []
|
120
|
+
files:
|
121
|
+
- .gitignore
|
122
|
+
- .travis.yml
|
123
|
+
- Gemfile
|
124
|
+
- LICENSE.txt
|
125
|
+
- README.md
|
126
|
+
- Rakefile
|
127
|
+
- datatables_server.gemspec
|
128
|
+
- lib/datatables_server.rb
|
129
|
+
- lib/datatables_server/active_record_repository.rb
|
130
|
+
- lib/datatables_server/base.rb
|
131
|
+
- lib/datatables_server/custom_errors.rb
|
132
|
+
- lib/datatables_server/repository.rb
|
133
|
+
- lib/datatables_server/repository_factory.rb
|
134
|
+
- lib/datatables_server/version.rb
|
135
|
+
- test/minitest_helper.rb
|
136
|
+
- test/test_active_record_repository.rb
|
137
|
+
- test/test_datatables_server.rb
|
138
|
+
- test/test_repository_factory.rb
|
139
|
+
homepage: https://github.com/dfmonaco/datatables_server
|
140
|
+
licenses:
|
141
|
+
- MIT
|
142
|
+
metadata: {}
|
143
|
+
post_install_message:
|
144
|
+
rdoc_options: []
|
145
|
+
require_paths:
|
146
|
+
- lib
|
147
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
148
|
+
requirements:
|
149
|
+
- - '>='
|
150
|
+
- !ruby/object:Gem::Version
|
151
|
+
version: 1.9.2
|
152
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
153
|
+
requirements:
|
154
|
+
- - '>='
|
155
|
+
- !ruby/object:Gem::Version
|
156
|
+
version: '0'
|
157
|
+
requirements: []
|
158
|
+
rubyforge_project:
|
159
|
+
rubygems_version: 2.1.11
|
160
|
+
signing_key:
|
161
|
+
specification_version: 4
|
162
|
+
summary: Server-side processing for DataTables in Ruby
|
163
|
+
test_files:
|
164
|
+
- test/minitest_helper.rb
|
165
|
+
- test/test_active_record_repository.rb
|
166
|
+
- test/test_datatables_server.rb
|
167
|
+
- test/test_repository_factory.rb
|