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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0798fa16c1404198a97647a9bfc2f4514f3a21e4
4
- data.tar.gz: 04e1541db4c718874ecbf131613569b34ebadf30
3
+ metadata.gz: 843cafe60eba33bd11e4a1db95f408e29dc95df6
4
+ data.tar.gz: 9e52d51f84b77cea416333e8634198019c497d7c
5
5
  SHA512:
6
- metadata.gz: 030493b51f5b002e10b36561b9ec9dbd900c457ab3159f000c19091caccd757cc506e79901d61d321f546422e40eb160b24ea0c0ff00a28fc61e76538e049957
7
- data.tar.gz: 08eb69d5a37e099868c9869f2a66e1959cf3fd02fa19f86b94a3e05bd5ca2dcba472272456a980d27c5459fcefa733bb4f70bb172d5983e493f0090fa9052652
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'
@@ -1,4 +1,5 @@
1
1
  require 'active_model_serializers'
2
+ require 'count_estimate'
2
3
  require 'data_tables/modules/pagination'
3
4
  require 'data_tables/modules/search'
4
5
 
@@ -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&.model&.count || 0,
31
- recordsFiltered: @collection&.total_entries || 0
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(:or))
31
+ @collection.where(search_by.reduce(:and))
32
32
  end
33
33
 
34
34
  def build_search(model, searches)
35
- queries = []
36
- searches.inject(queries) do |query, junk|
37
- column, search = junk
38
- case search
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 = Arel::Nodes::OuterJoin.new(assoc_klass.arel_table,
45
+ outer_join = join_type.new(assoc_klass.arel_table,
44
46
  Arel::Nodes::On.new(
45
- model.arel_table[assoc.active_record_primary_key].eq(assoc_klass.arel_table[assoc.foreign_key])
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
- query << build_search(assoc_klass, search).reduce(:or)
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
- query << model.arel_table[k.name].matches("%#{search}%")
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
- query << model.arel_table[k.name].eq(search)
56
- else
57
- query
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
@@ -1,5 +1,5 @@
1
1
  module DataTables
2
2
  module Responder
3
- VERSION = '0.2.2'
3
+ VERSION = '0.2.3'
4
4
  end
5
5
  end
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.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-09 00:00:00.000000000 Z
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