effective_datatables 2.6.3 → 2.6.4

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: 89650689f933090e188f7f97fa874585a6ed80e9
4
- data.tar.gz: 292f7c4078fe2626e739ae6ac05bc8ca13ae3ae0
3
+ metadata.gz: 147fbd09ec680b0db8ba4a47af18f0286cd0c055
4
+ data.tar.gz: 1588dd78852406c4259c01dfb1298a4500d03271
5
5
  SHA512:
6
- metadata.gz: f8dea3d9a6ae53f4cbd38cf79b01805d01de6d9711a860240f72feb65ef1457ab5d94f83995e3511bb0e3e71ff7d4f33f8a4f0909eed04630f0633c9edc27dda
7
- data.tar.gz: 0c83c4f5009cb5f12c9d5aee0ae4e132bce70ea73f3b26556ad10718fe4931ff3f2575bf5b06911911eef5c82bed363f7d6cee5405f8670ffb737b7856e7eb85
6
+ metadata.gz: 142a1acc50d459b49b40d0eb9f2694811c6587fbb9018b2f75be841645dfae80b81e030fbd2241f2be8fdfab0d223e03156c4655c603d9c32b4a54ab2a30910f
7
+ data.tar.gz: aa9f267f18cfe926aca23e9cbb3432a9cce68806b21e01492f7864705fbca660aa35ca7ee5a5bd125e789f708394a3edb321eb188c5b6ba50becd2443094d0c1
@@ -112,26 +112,15 @@ module Effective
112
112
  end
113
113
 
114
114
  def present?
115
- total_records.to_i > 0
115
+ total_records > 0
116
116
  end
117
117
 
118
118
  def empty?
119
- total_records.to_i == 0
119
+ total_records == 0
120
120
  end
121
121
 
122
122
  def total_records
123
- @total_records ||= (
124
- if active_record_collection?
125
- if collection_class.connection.respond_to?(:unprepared_statement)
126
- collection_sql = collection_class.connection.unprepared_statement { collection.to_sql }
127
- (collection_class.connection.exec_query("SELECT COUNT(*) FROM (#{collection_sql}) AS datatables_total_count").rows[0][0] rescue 1).to_i
128
- else
129
- (collection_class.connection.exec_query("SELECT COUNT(*) FROM (#{collection.to_sql}) AS datatables_total_count").rows[0][0] rescue 1).to_i
130
- end
131
- else
132
- collection.size
133
- end
134
- )
123
+ @total_records ||= (active_record_collection? ? active_record_collection_size(collection) : collection.size)
135
124
  end
136
125
 
137
126
  def view=(view_context)
@@ -194,12 +183,36 @@ module Effective
194
183
  end
195
184
 
196
185
  def active_record_collection?
197
- collection.ancestors.include?(ActiveRecord::Base) rescue false
186
+ @active_record_collection ||= (collection.ancestors.include?(ActiveRecord::Base) rescue false)
198
187
  end
199
188
 
200
189
  def array_collection?
201
190
  collection.kind_of?(Array) && collection.first.kind_of?(Array)
202
191
  end
203
192
 
193
+ # Not every ActiveRecord query will work when calling the simple .count
194
+ # Custom selects:
195
+ # User.select(:email, :first_name).count will throw an error
196
+ # .count(:all) and .size seem to work
197
+ # Grouped Queries:
198
+ # User.all.group(:email).count will return a Hash
199
+ def active_record_collection_size(collection)
200
+ count = (collection.size rescue nil)
201
+
202
+ case count
203
+ when Integer
204
+ count
205
+ when Hash
206
+ count.size # This represents the number of displayed datatable rows, not the sum all groups (which might be more)
207
+ else
208
+ if collection.klass.connection.respond_to?(:unprepared_statement)
209
+ collection_sql = collection.klass.connection.unprepared_statement { collection.to_sql }
210
+ (collection.klass.connection.exec_query("SELECT COUNT(*) FROM (#{collection_sql}) AS datatables_total_count").rows[0][0] rescue 1)
211
+ else
212
+ (collection.klass.connection.exec_query("SELECT COUNT(*) FROM (#{collection.to_sql}) AS datatables_total_count").rows[0][0] rescue 1)
213
+ end.to_i
214
+ end
215
+ end
216
+
204
217
  end
205
218
  end
@@ -17,13 +17,7 @@ module Effective
17
17
  col = table_tool.search(col)
18
18
 
19
19
  if table_tool.search_terms.present? && array_tool.search_terms.blank?
20
- if collection_class.connection.respond_to?(:unprepared_statement)
21
- # https://github.com/rails/rails/issues/15331
22
- col_sql = collection_class.connection.unprepared_statement { col.to_sql }
23
- self.display_records = (collection_class.connection.execute("SELECT COUNT(*) FROM (#{col_sql}) AS datatables_filtered_count").first.values.first rescue 1).to_i
24
- else
25
- self.display_records = (collection_class.connection.execute("SELECT COUNT(*) FROM (#{col.to_sql}) AS datatables_filtered_count").first.values.first rescue 1).to_i
26
- end
20
+ self.display_records = active_record_collection_size(col)
27
21
  end
28
22
  end
29
23
 
@@ -1,3 +1,3 @@
1
1
  module EffectiveDatatables
2
- VERSION = '2.6.3'.freeze
2
+ VERSION = '2.6.4'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: effective_datatables
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.6.3
4
+ version: 2.6.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Code and Effect
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-08-05 00:00:00.000000000 Z
11
+ date: 2016-08-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -221,7 +221,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
221
221
  version: '0'
222
222
  requirements: []
223
223
  rubyforge_project:
224
- rubygems_version: 2.4.6
224
+ rubygems_version: 2.4.5.1
225
225
  signing_key:
226
226
  specification_version: 4
227
227
  summary: Uniquely powerful server-side searching, sorting and filtering of any ActiveRecord