table_on_steroids 0.1.1.7 → 0.1.1.8
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
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e597331d8b98c9be9e068e59619f0227be2a6468d6dff1c3220d418d34951574
|
4
|
+
data.tar.gz: 8bec801c4b6a262b544bdaa8391d9ca74465af8bd11b58f2246b3d2662147f18
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 361a777ad599f401c5f41cc26583c1209d2f0341fd0181094f731398b0059ea047e9a1ad6f15bcdc5e1283cc15e5599fdcc18d4243f3ecadfd7e44cdb52795b9
|
7
|
+
data.tar.gz: 5530d6af19d153462dc298d82713b07b1de4b256726f5a9c23fe88eeb1fee7d2bf8c1ad620ddd97f704840d2159581d27f3f3ab141552177347eb23cec714b69
|
@@ -1,6 +1,8 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
#####
|
2
|
-
# The knowledge sharing concern permits creates a fast way to display,
|
3
|
-
# filter and order data of a specific object.
|
4
|
+
# The knowledge sharing concern permits creates a fast way to display,
|
5
|
+
# filter and order data of a specific object.
|
4
6
|
# To make it work:
|
5
7
|
# In the Object controller, include TableOnSteroid and add this private methods:
|
6
8
|
|
@@ -9,11 +11,11 @@
|
|
9
11
|
# 'key_defining_column_1' => {
|
10
12
|
# label: 'Label 1', >> the column label
|
11
13
|
# type: 'filter', >> can be 'filter', 'order' or nothing if no operations
|
12
|
-
# multiselect: true, >> OPTIONAL in case of a filter
|
14
|
+
# multiselect: true, >> OPTIONAL in case of a filter
|
13
15
|
# select_values: ['Option 1','Option 2'], >> in case of a filter, the options to select
|
14
16
|
# filter_lambda: -> (objects, value) { .. } >> A lambda defining the filter if applicable
|
15
17
|
# order_lambda: -> (objects) { .. }, >> A lambda defining the order if applicable
|
16
|
-
# value_lambda: -> (object) { .. } >> A lambda defining the value in the table
|
18
|
+
# value_lambda: -> (object) { .. } >> A lambda defining the value in the table
|
17
19
|
# },
|
18
20
|
# 'key_defining_column_2' => {
|
19
21
|
# ...
|
@@ -36,67 +38,70 @@ module TableOnSteroids
|
|
36
38
|
included do
|
37
39
|
OBJECTS_PER_PAGE = 50
|
38
40
|
end
|
39
|
-
|
40
|
-
def filter_and_order(objects, columns_on_steroid, global_search=nil, include_counts=false, all_pages=false, table_on_steroids=nil
|
41
|
+
|
42
|
+
def filter_and_order(objects, columns_on_steroid, global_search = nil, include_counts = false, all_pages = false, table_on_steroids = nil)
|
41
43
|
# execute the global search if you have one
|
42
|
-
if
|
43
|
-
objects = global_search.call(objects,params[:search]) if global_search
|
44
|
-
objects = all_column_search(objects, columns_on_steroid, params[:search], table_on_steroids)
|
44
|
+
if params[:search].present?
|
45
|
+
objects = global_search.call(objects, params[:search]) if global_search
|
46
|
+
objects = all_column_search(objects, columns_on_steroid, params[:search], table_on_steroids) unless global_search
|
45
47
|
end
|
46
48
|
|
47
|
-
[
|
48
|
-
|
49
|
-
|
50
|
-
objects = objects_where(objects, columns_on_steroid, t)
|
49
|
+
%i[activerecord array].each do |t|
|
50
|
+
# column search
|
51
|
+
objects = objects_where(objects, columns_on_steroid, t)
|
51
52
|
|
52
|
-
#apply filters
|
53
|
+
# apply filters
|
53
54
|
if params[:filters].present?
|
54
|
-
params[:filters].each_pair do |
|
55
|
+
params[:filters].each_pair do |k, v|
|
55
56
|
filter = columns_on_steroid[k]
|
56
57
|
next unless filter.present? && filter[t].present?
|
58
|
+
|
57
59
|
objects = filter[t][:filter_lambda].call(objects, v)
|
58
60
|
end
|
59
61
|
end
|
60
62
|
|
61
|
-
#order
|
62
|
-
if params[:knowledge] && params[:knowledge] && params[:knowledge][:order].present? && (object_order = columns_on_steroid[params[:knowledge][:order]]).present?
|
63
|
-
if
|
64
|
-
objects = objects.reorder(nil) if
|
63
|
+
# order
|
64
|
+
if params[:knowledge] && params[:knowledge] && params[:knowledge][:order].present? && (object_order = columns_on_steroid[params[:knowledge][:order]]).present?
|
65
|
+
if object_order[t] && object_order[t][:order_lambda]
|
66
|
+
objects = objects.reorder(nil) if objects.is_a?(ActiveRecord::Base) || objects.is_a?(ActiveRecord::Relation)
|
65
67
|
objects = object_order[t][:order_lambda].call(objects)
|
66
68
|
end
|
67
|
-
elsif
|
68
|
-
if
|
69
|
-
objects = objects.reorder(nil) if
|
69
|
+
elsif (object_order = columns_on_steroid.select { |_c, v| v[t] && v[t][:default_order] }.first&.last).present?
|
70
|
+
if object_order[t] && object_order[t][:order_lambda]
|
71
|
+
objects = objects.reorder(nil) if objects.is_a?(ActiveRecord::Base) || objects.is_a?(ActiveRecord::Relation)
|
70
72
|
objects = object_order[t][:order_lambda].call(objects)
|
71
73
|
end
|
72
|
-
elsif
|
73
|
-
#objects = objects.order('created_at desc')
|
74
|
+
elsif objects.is_a?(ActiveRecord::Base) || objects.is_a?(ActiveRecord::Relation)
|
75
|
+
# objects = objects.order('created_at desc')
|
74
76
|
end
|
75
|
-
|
76
77
|
end
|
77
78
|
|
78
|
-
#pagination
|
79
|
+
# pagination
|
79
80
|
return (include_counts ? [objects, 1, objects.count] : objects) if all_pages
|
80
|
-
|
81
|
-
|
81
|
+
|
82
|
+
# GO to specific object page
|
83
|
+
page = get_page(objects, params, table_on_steroids)
|
84
|
+
|
85
|
+
if objects.is_a?(ActiveRecord::Base) || objects.is_a?(ActiveRecord::Relation)
|
86
|
+
objects = objects.page(page).per(OBJECTS_PER_PAGE)
|
82
87
|
total_pages = objects.total_pages
|
83
88
|
total_count = objects.total_count
|
84
|
-
else
|
85
|
-
objects = Kaminari.paginate_array(objects).page(
|
89
|
+
else
|
90
|
+
objects = Kaminari.paginate_array(objects).page(page).per(OBJECTS_PER_PAGE)
|
86
91
|
total_pages = objects.total_pages
|
87
92
|
total_count = objects.total_count
|
88
93
|
end
|
89
|
-
include_counts ? [objects, total_pages, total_count] : objects
|
94
|
+
include_counts ? [objects, total_pages, total_count, page] : objects
|
90
95
|
end
|
91
|
-
|
96
|
+
|
92
97
|
def table_csv(objects, columns_on_steroid)
|
93
98
|
titles = []
|
94
99
|
csvs = CSV.generate do |csv|
|
95
|
-
columns_on_steroid.select{ |
|
100
|
+
columns_on_steroid.select { |_c, v| v[:download_value_lambda].present? }.each { |_c, v| (v[:download_label].present? ? titles.push(*v[:download_label]) : titles << v[:label]) }
|
96
101
|
csv << titles
|
97
102
|
objects.each do |o|
|
98
103
|
vals = []
|
99
|
-
columns_on_steroid.select{ |
|
104
|
+
columns_on_steroid.select { |_c, v| v[:download_value_lambda].present? }.each do |_c, v|
|
100
105
|
vals.push(*v[:download_value_lambda].call(o))
|
101
106
|
end
|
102
107
|
csv << vals
|
@@ -104,104 +109,121 @@ module TableOnSteroids
|
|
104
109
|
end
|
105
110
|
end
|
106
111
|
|
107
|
-
def all_column_search(objects, columns_on_steroid, query, table_on_steroids=nil)
|
112
|
+
def all_column_search(objects, columns_on_steroid, query, table_on_steroids = nil)
|
108
113
|
global_active_record_search_key = table_on_steroids&.dig(:global_active_record_search_key) || :id
|
109
114
|
global_array_search_key_index = table_on_steroids&.dig(:global_array_search_key_index) || 0
|
110
|
-
global_search_key = (
|
115
|
+
global_search_key = (objects && objects[0].is_a?(Array) ? global_array_search_key_index : global_active_record_search_key)
|
111
116
|
global_search_key_params = {}
|
112
117
|
matched_object_keys = []
|
113
118
|
|
114
|
-
[
|
115
|
-
columns_on_steroid.each do |
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
119
|
+
%i[activerecord array].each do |t|
|
120
|
+
columns_on_steroid.each do |_key, value|
|
121
|
+
next unless value[t].present? && ((value[t][:search_lambda].present? && !%w[date integer].include?(value[:datatype])) || value[t][:global_search_lambda].present?)
|
122
|
+
|
123
|
+
objects_returned = if value[t][:global_search_lambda].present?
|
124
|
+
value[t][:global_search_lambda].call(objects, query)
|
125
|
+
else
|
126
|
+
value[t][:search_lambda].call(objects, query)
|
127
|
+
end
|
128
|
+
objects_returned&.each { |o| matched_object_keys << o[global_search_key] }
|
124
129
|
end
|
125
130
|
end
|
126
131
|
|
127
|
-
if
|
132
|
+
if objects.is_a?(ActiveRecord::Base) || objects.is_a?(ActiveRecord::Relation)
|
128
133
|
global_search_key_params[global_active_record_search_key] = matched_object_keys.uniq
|
129
134
|
objects = objects.where(global_search_key_params)
|
130
|
-
elsif
|
131
|
-
objects = objects.select{|o| o if
|
135
|
+
elsif objects.is_a?(Array)
|
136
|
+
objects = objects.select { |o| o if matched_object_keys.uniq.include?(o[global_search_key]) }
|
132
137
|
end
|
133
138
|
end
|
134
|
-
|
135
|
-
|
139
|
+
|
136
140
|
def objects_where(objects, columns_on_steroid, t)
|
137
|
-
columns_on_steroid.select
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
141
|
+
columns_on_steroid.select do |_c, v|
|
142
|
+
v[t] && v[t][:search_lambda].present?
|
143
|
+
end .each do |c, v|
|
144
|
+
if params['search_operator_' + c] # (v[:datatype].present? && ['date','integer'].include?(v[:datatype]))
|
145
|
+
objects = v[t][:search_lambda].call(objects, params['search_' + c], params['search_operator_' + c]) if params['search_' + c] && !params['search_' + c].blank?
|
146
|
+
else
|
147
|
+
objects = v[t][:search_lambda].call(objects, params['search_' + c]) if params['search_' + c] && !params['search_' + c].blank?
|
148
|
+
end
|
149
|
+
end
|
146
150
|
objects
|
147
151
|
end
|
148
152
|
|
149
153
|
def objects_where_date(objects, column, value, operator)
|
150
154
|
case operator
|
151
|
-
when
|
152
|
-
|
153
|
-
when
|
154
|
-
|
155
|
-
when
|
156
|
-
|
155
|
+
when '<', '>='
|
156
|
+
objects.where((column + ' ' + operator + ' ?'), (value + ' 00:00:00'))
|
157
|
+
when '>', '<='
|
158
|
+
objects.where((column + ' ' + operator + ' ?'), (value + ' 23:59:59'))
|
159
|
+
when '='
|
160
|
+
objects.where((column + ' between ? and ?'), (value + ' 00:00:00'), (value + ' 23:59:59'))
|
157
161
|
end
|
158
162
|
end
|
159
163
|
|
160
164
|
def objects_where_ruby_date(object, column, value, operator)
|
161
165
|
value = Date.strptime(value, '%m/%d/%Y').midnight
|
162
166
|
case operator
|
163
|
-
when
|
167
|
+
when '<'
|
164
168
|
return object.send(column) < value
|
165
|
-
when
|
169
|
+
when '>'
|
166
170
|
return object.send(column) > value + 1.days - 1.seconds
|
167
|
-
when
|
171
|
+
when '='
|
168
172
|
return ((object.send(column) > value) && (object.send(column) < (value + 1.days - 1.seconds)))
|
169
173
|
end
|
170
174
|
end
|
171
175
|
|
172
176
|
def object_where_integer(integer, operator, value)
|
173
177
|
case operator
|
174
|
-
when
|
175
|
-
|
176
|
-
when
|
177
|
-
|
178
|
-
when
|
179
|
-
|
178
|
+
when '<', '>='
|
179
|
+
integer.to_f < value.to_f
|
180
|
+
when '='
|
181
|
+
integer.to_f == value.to_f
|
182
|
+
when '>'
|
183
|
+
integer.to_f > value.to_f
|
180
184
|
end
|
181
185
|
end
|
182
186
|
|
183
|
-
#save for later
|
187
|
+
# save for later
|
184
188
|
def objects_where_or(objects, columns_on_steroid)
|
185
|
-
where_sql =
|
189
|
+
where_sql = ''
|
186
190
|
values = []
|
187
|
-
|
188
|
-
#search all columns
|
189
|
-
|
190
|
-
where_sql += columns_on_steroid.select
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
191
|
+
|
192
|
+
# search all columns
|
193
|
+
unless query.blank?
|
194
|
+
where_sql += columns_on_steroid.select do |_c, v|
|
195
|
+
v[:or_where_sql].present?
|
196
|
+
end .map do |_c, v|
|
197
|
+
v[:or_where_sql].map do |w|
|
198
|
+
w[:where]
|
199
|
+
end .join(' or ')
|
200
|
+
end .join(' or ')
|
201
|
+
|
202
|
+
columns_on_steroid.select do |_c, v|
|
203
|
+
v[:or_where_sql].present?
|
204
|
+
end .map do |_c, v|
|
205
|
+
v[:or_where_sql].each do |w|
|
206
|
+
values << (w[:value] || ('%' + query + '%'))
|
207
|
+
end
|
208
|
+
end
|
209
|
+
|
210
|
+
where_sql = ('(' + where_sql + ')') unless where_sql.blank?
|
201
211
|
|
202
212
|
objects = objects.where(where_sql, *values)
|
203
213
|
end
|
204
214
|
objects
|
205
215
|
end
|
216
|
+
|
217
|
+
def get_page(objects, params, table_on_steroids)
|
218
|
+
active_record_object_fetch_opts = table_on_steroids&.dig(:active_record_object_fetch_opts)
|
219
|
+
key_lambda = table_on_steroids&.dig(:key_lambda)
|
220
|
+
return params[:page] unless active_record_object_fetch_opts && key_lambda
|
221
|
+
|
222
|
+
object = objects.where(active_record_object_fetch_opts).first
|
223
|
+
return params[:page] unless object
|
224
|
+
|
225
|
+
index = objects.index { |o| key_lambda.call(o) == key_lambda.call(object) }
|
226
|
+
index ? index / OBJECTS_PER_PAGE + 1 : params[:page]
|
227
|
+
end
|
206
228
|
end
|
207
229
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: table_on_steroids
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.1.
|
4
|
+
version: 0.1.1.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Marieke Gueye
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date: 2019-
|
12
|
+
date: 2019-10-25 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bootstrap-datepicker-rails
|