data_tables-responder 0.2.2 → 0.2.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/data_tables-responder.gemspec +1 -0
- data/lib/data_tables/adapter.rb +1 -0
- data/lib/data_tables/modules/pagination.rb +11 -3
- data/lib/data_tables/modules/search.rb +22 -14
- data/lib/data_tables/responder/version.rb +1 -1
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 843cafe60eba33bd11e4a1db95f408e29dc95df6
|
4
|
+
data.tar.gz: 9e52d51f84b77cea416333e8634198019c497d7c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 702847fb2fee755b7a439c959d247f7829f1bc70ba2eb24cebcca9cdccd7d6c41673b31f7edb5be44a1e53f352d133e37681ef056e71b5b8a2bf2eb9f8fb15d2
|
7
|
+
data.tar.gz: 007d3b0842e8fb4ce9efbd38edb81a4b96761667409bce1c7c2d5a82cc8f1204b190f45ce4c2decd9e88e4f61fe46532516aa1346fb7a0e26ad435eabf35fc04
|
@@ -27,6 +27,7 @@ Gem::Specification.new do |spec|
|
|
27
27
|
spec.require_paths = ['lib']
|
28
28
|
|
29
29
|
spec.add_dependency 'active_model_serializers', '~> 0.10.0'
|
30
|
+
spec.add_dependency 'count_estimate'
|
30
31
|
|
31
32
|
spec.add_development_dependency 'bundler', '~> 1.12'
|
32
33
|
spec.add_development_dependency 'rake', '~> 10.0'
|
data/lib/data_tables/adapter.rb
CHANGED
@@ -22,18 +22,26 @@ module DataTables
|
|
22
22
|
start = (request_parameters[:start] || '0').to_i
|
23
23
|
length = (request_parameters[:length] || '10').to_i
|
24
24
|
page = (start / length) + 1
|
25
|
-
@collection = @collection.paginate(page: page, per_page: length)
|
25
|
+
@collection = @collection.paginate(page: page, per_page: length, total_entries: records_total)
|
26
26
|
end
|
27
27
|
|
28
28
|
def as_json
|
29
29
|
{
|
30
|
-
recordsTotal: @collection&.
|
31
|
-
recordsFiltered:
|
30
|
+
recordsTotal: @collection&.total_entries&.to_i,
|
31
|
+
recordsFiltered: records_filtered&.to_i
|
32
32
|
}
|
33
33
|
end
|
34
34
|
|
35
35
|
protected
|
36
36
|
|
37
|
+
def records_total
|
38
|
+
@collection&.model&.all.count_estimate
|
39
|
+
end
|
40
|
+
|
41
|
+
def records_filtered
|
42
|
+
@collection&.unscope(:limit, :offset)&.count_estimate
|
43
|
+
end
|
44
|
+
|
37
45
|
attr_reader :adapter_options
|
38
46
|
|
39
47
|
private
|
@@ -28,37 +28,45 @@ module DataTables
|
|
28
28
|
|
29
29
|
search_by = build_search(model, searches)
|
30
30
|
|
31
|
-
@collection.where(search_by.reduce(:
|
31
|
+
@collection.where(search_by.reduce(:and))
|
32
32
|
end
|
33
33
|
|
34
34
|
def build_search(model, searches)
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
35
|
+
# join_type = Arel::Nodes::OuterJoin
|
36
|
+
join_type = Arel::Nodes::InnerJoin
|
37
|
+
|
38
|
+
searches.inject([]) do |queries, junk|
|
39
|
+
column, query = junk
|
40
|
+
case query
|
39
41
|
when Hash
|
40
42
|
assoc = model.reflect_on_association(column)
|
41
43
|
assoc_klass = assoc.klass
|
42
44
|
|
43
|
-
outer_join =
|
45
|
+
outer_join = join_type.new(assoc_klass.arel_table,
|
44
46
|
Arel::Nodes::On.new(
|
45
|
-
model.arel_table[assoc.
|
47
|
+
model.arel_table[assoc.foreign_key].eq(assoc_klass.arel_table[assoc.active_record_primary_key])
|
46
48
|
))
|
47
|
-
@collection.joins(outer_join)
|
48
|
-
|
49
|
+
@collection = @collection.joins(outer_join)
|
50
|
+
queries << build_search(assoc_klass, query).reduce(:and)
|
49
51
|
else
|
50
52
|
col_s = column.to_s
|
51
53
|
case (k = model.columns.find(nil) { |c| c.name == col_s })&.type
|
52
54
|
when :string
|
53
|
-
|
55
|
+
# I'm pretty sure this is safe from SQL Injection
|
56
|
+
queries << model.arel_table[k.name].matches("%#{query}%")
|
54
57
|
when :integer
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
+
if value = query&.to_i
|
59
|
+
queries << model.arel_table[k.name].eq(value)
|
60
|
+
end
|
61
|
+
when :datetime
|
62
|
+
datetime = Time.parse(query)
|
63
|
+
range = (datetime-1.second)..(datetime+1.second)
|
64
|
+
queries << model.arel_table[k.name].between(range)
|
58
65
|
end
|
59
66
|
end
|
67
|
+
|
68
|
+
queries
|
60
69
|
end
|
61
|
-
queries
|
62
70
|
end
|
63
71
|
|
64
72
|
protected
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: data_tables-responder
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dale Stevens
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-01-
|
11
|
+
date: 2017-01-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: active_model_serializers
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: 0.10.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: count_estimate
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: bundler
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|