data_tables-responder 0.4.1 → 0.4.2

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: d023fd29dc4a27f1f0b86fe48f778923a768eb1c
4
- data.tar.gz: 70197f9c9f67a15028f43653080c3edc7b29b64d
3
+ metadata.gz: a83b526e4f4224368d73806333dff683042e76cc
4
+ data.tar.gz: 135113fa38ca6d8df40315a0d5cde7372cf92739
5
5
  SHA512:
6
- metadata.gz: 193ca253646034ee7746b5821008a9939f0ab8a7816bf7d231c5dda2ff62ed537cdd8255ac333ae315de55dd95cbb16bf1062f335e9b3cf6e2cbe6839cb45ccd
7
- data.tar.gz: 9ff1044bc705159ea5b6105d804f9cadb144be9f4639820eb58659125aa291556604a268960c659225b88a0f8e8a5d39e68ed329696762069a734f2602781efb
6
+ metadata.gz: a403b7f8443f716245bb1a2c1714586d9f08c899ded9378bd742cc939a667ebd2a9cf0a0c738480ccb55157a0e6dacf5785419daa5964d52b50042341216f0f3
7
+ data.tar.gz: 64d5519e436855129d00dd227c79b9fabd39e60fec2676e311338bace7df56620925c2df7d0ad82bbdde4b9b462ff5867f296676e07880f8cab94619d63fb4df
data/.travis.yml ADDED
@@ -0,0 +1,18 @@
1
+ language: ruby
2
+ sudo: false
3
+ rvm:
4
+ - 2.3.1
5
+ - 2.4.0
6
+ cache: bundler
7
+ script:
8
+ - bundle exec rspec
9
+
10
+ after_success:
11
+ - bundle exec codeclimate-test-reporter
12
+
13
+ addons:
14
+ postgresql: "9.3"
15
+
16
+ env:
17
+ - RSPEC_VERSION="<2.99"
18
+ - RSPEC_VERSION="~>3.0
@@ -34,7 +34,8 @@ Gem::Specification.new do |spec|
34
34
 
35
35
  spec.add_development_dependency 'activerecord', rails_versions
36
36
  spec.add_development_dependency 'pry-byebug'
37
- spec.add_development_dependency 'bundler', '~> 1.12'
37
+ spec.add_development_dependency 'database_cleaner'
38
+ spec.add_development_dependency 'bundler', '~> 1.3'
38
39
  spec.add_development_dependency 'rake', '~> 10.0'
39
40
  spec.add_development_dependency 'rspec', '~> 3.0'
40
41
  end
@@ -4,20 +4,18 @@ module DataTables
4
4
 
5
5
  attr_reader :scope, :context
6
6
 
7
- def initialize(scope, request_parameters)
7
+ def initialize(model, scope, params)
8
8
  @scope = scope.dup
9
- @request_parameters = request_parameters
9
+ @model = model
10
+ @params = params
10
11
  end
11
12
 
12
13
  def order
13
- # default_order = @request_parameters.dig(:order, :value)
14
-
15
- model = @scope.try(:model) || @scope
16
- columns = orderable_columns(@request_parameters[:order], @request_parameters[:columns])
14
+ columns = orderable_columns(@params[:order], @params[:columns])
17
15
 
18
16
  orders = DataTables::Responder.flat_keys_to_nested columns
19
17
 
20
- order_by, join_hash = build_order(model, orders)
18
+ order_by, join_hash = build_order(@model, orders)
21
19
 
22
20
  @scope = @scope.joins(join_hash)
23
21
 
@@ -49,13 +47,13 @@ module DataTables
49
47
  protected
50
48
 
51
49
  def orderable_columns(orders, columns)
52
- sums = {}
53
- (orders || []).inject(sums) do |sum, order|
54
- if (name = columns[order[:column]][:data]).present?
55
- sum[name] = order[:dir]
50
+ orders&.inject({}) do |collection, order|
51
+ column = columns[order[:column]]
52
+ if (column[:orderable] && column[:data].present?)
53
+ collection[column[:data]] = order[:dir]
56
54
  end
55
+ collection
57
56
  end
58
- sums
59
57
  end
60
58
 
61
59
  end
@@ -3,21 +3,22 @@ module DataTables
3
3
  module Modules
4
4
  class Pagination
5
5
 
6
- FIRST_PAGE = 1
6
+ FIRST_PAGE = '0'
7
+ DEFAULT_LENGTH = '10'
7
8
 
8
- attr_reader :original_scope, :filtered_scope, :context
9
+ attr_reader :scope, :context
9
10
 
10
- def initialize(original_scope, filtered_scope, request_parameters)
11
- @original_scope = original_scope
12
- @filtered_scope = filtered_scope
13
- @request_parameters = request_parameters
11
+ def initialize(model, scope, params)
12
+ @scope = scope.dup
13
+ @model = model
14
+ @params = params
14
15
  end
15
16
 
16
17
  def paginate
17
- start = (@request_parameters[:start] || '0').to_i
18
- length = (@request_parameters[:length] || '10').to_i
18
+ start = (@params[:start] || FIRST_PAGE).to_i
19
+ length = (@params[:length] || DEFAULT_LENGTH).to_i
19
20
  page = (start / length)
20
- @filtered_scope = @filtered_scope.offset(page * length).limit(length)
21
+ @scope.offset(page * length).limit(length)
21
22
  end
22
23
 
23
24
  end
@@ -4,20 +4,18 @@ module DataTables
4
4
 
5
5
  attr_reader :scope, :context
6
6
 
7
- def initialize(scope, request_parameters)
8
- @scope = scope
9
- @request_parameters = request_parameters
7
+ def initialize(model, scope, params)
8
+ @scope = scope.dup
9
+ @model = model
10
+ @params = params
10
11
  end
11
12
 
12
13
  def search
13
- default_search = @request_parameters.dig(:search, :value)
14
-
15
- model = @scope.try(:model) || @scope
16
- columns = searchable_columns(default_search)
14
+ columns = searchable_columns(@params.dig(:search, :value), @params[:columns])
17
15
 
18
16
  searches = DataTables::Responder.flat_keys_to_nested columns
19
17
 
20
- search_by, join_hash = build_search(model, searches)
18
+ search_by, join_hash = build_search(@model, searches)
21
19
 
22
20
  @scope = @scope.joins(join_hash)
23
21
 
@@ -55,7 +53,7 @@ module DataTables
55
53
  # I'm pretty sure this is safe from SQL Injection
56
54
  model.arel_table[column].matches("%#{query}%")
57
55
  when :integer
58
- model.arel_table[column].eq(value) if value = query&.to_i
56
+ value = query&.to_i and model.arel_table[column].eq(value)
59
57
  when :datetime
60
58
  datetime = Time.parse(query)
61
59
  range = (datetime-1.second)..(datetime+1.second)
@@ -67,18 +65,15 @@ module DataTables
67
65
  result
68
66
  end
69
67
 
70
- def searchable_columns(default_search)
71
- @searchable_columns = {}
72
- @request_parameters[:columns]&.inject(@searchable_columns) do |a, b|
73
- if (b[:searchable] && b[:data].present?)
74
- if ((value = b.dig(:search, :value).present? ? b.dig(:search, :value) : default_search).present?)
75
- a[b[:data]] = value
68
+ def searchable_columns(default_search, columns)
69
+ columns&.inject({}) do |collection, column|
70
+ if (column[:searchable] && column[:data].present?)
71
+ if ((value = column.dig(:search, :value).present? ? column.dig(:search, :value) : default_search).present?)
72
+ collection[column[:data]] = value
76
73
  end
77
74
  end
78
- a
75
+ collection
79
76
  end
80
-
81
- @searchable_columns
82
77
  end
83
78
 
84
79
  end
@@ -1,5 +1,5 @@
1
1
  module DataTables
2
2
  module Responder
3
- VERSION = '0.4.1'
3
+ VERSION = '0.4.2'
4
4
  end
5
5
  end
@@ -18,12 +18,11 @@ module DataTables
18
18
 
19
19
  filtered_scope = original_scope&.dup || model.none
20
20
 
21
- filtered_scope = order(filtered_scope, params)
21
+ filtered_scope = order(model, filtered_scope, params)
22
22
 
23
- filtered_scope = search(filtered_scope, params)
23
+ filtered_scope = search(model, filtered_scope, params)
24
24
 
25
- # Rails.logger.warn "SEARCH BY: #{search_by}"
26
- filtered_scope = paginate(original_scope, filtered_scope, params)
25
+ filtered_scope = paginate(model, filtered_scope, params)
27
26
  end
28
27
 
29
28
  def self.flat_keys_to_nested(hash)
@@ -34,16 +33,16 @@ module DataTables
34
33
  end
35
34
  end
36
35
 
37
- def self.paginate(original_scope, filtered_scope, params)
38
- Modules::Pagination.new(original_scope, filtered_scope, params).paginate
36
+ def self.paginate(model, filtered_scope, params)
37
+ Modules::Pagination.new(model, filtered_scope, params).paginate
39
38
  end
40
39
 
41
- def self.search(filtered_scope, params)
42
- Modules::Search.new(filtered_scope, params).search
40
+ def self.search(model, filtered_scope, params)
41
+ Modules::Search.new(model, filtered_scope, params).search
43
42
  end
44
43
 
45
- def self.order(filtered_scope, params)
46
- Modules::Order.new(filtered_scope, params).order
44
+ def self.order(model, filtered_scope, params)
45
+ Modules::Order.new(model, filtered_scope, params).order
47
46
  end
48
47
 
49
48
  end
data/lib/data_tables.rb CHANGED
@@ -1,6 +1,6 @@
1
1
 
2
2
  module DataTables
3
3
  def self.root
4
- @root ||= Pathname.new(File.dirname(File.expand_path(File.dirname(__FILE__), '/../')))
4
+ @root ||= Pathname.new(File.expand_path('../', File.dirname(__FILE__)))
5
5
  end
6
6
  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.4.1
4
+ version: 0.4.2
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-04-11 00:00:00.000000000 Z
11
+ date: 2017-06-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: active_model_serializers
@@ -98,20 +98,34 @@ dependencies:
98
98
  - - ">="
99
99
  - !ruby/object:Gem::Version
100
100
  version: '0'
101
+ - !ruby/object:Gem::Dependency
102
+ name: database_cleaner
103
+ requirement: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ type: :development
109
+ prerelease: false
110
+ version_requirements: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
101
115
  - !ruby/object:Gem::Dependency
102
116
  name: bundler
103
117
  requirement: !ruby/object:Gem::Requirement
104
118
  requirements:
105
119
  - - "~>"
106
120
  - !ruby/object:Gem::Version
107
- version: '1.12'
121
+ version: '1.3'
108
122
  type: :development
109
123
  prerelease: false
110
124
  version_requirements: !ruby/object:Gem::Requirement
111
125
  requirements:
112
126
  - - "~>"
113
127
  - !ruby/object:Gem::Version
114
- version: '1.12'
128
+ version: '1.3'
115
129
  - !ruby/object:Gem::Dependency
116
130
  name: rake
117
131
  requirement: !ruby/object:Gem::Requirement
@@ -149,6 +163,7 @@ extra_rdoc_files: []
149
163
  files:
150
164
  - ".gitignore"
151
165
  - ".rspec"
166
+ - ".travis.yml"
152
167
  - Gemfile
153
168
  - LICENSE.txt
154
169
  - README.md