spotlight_search 0.1.7 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +82 -71
- data/README.md +92 -45
- data/app/controllers/spotlight_search/export_jobs_controller.rb +7 -7
- data/app/jobs/spotlight_search/export_job.rb +50 -20
- data/app/mailers/spotlight_search/export_mailer.rb +6 -0
- data/app/views/spotlight_search/export_mailer/send_error_message.html.erb +7 -0
- data/lib/generators/spotlight_search/filter_generator.rb +18 -0
- data/lib/generators/spotlight_search/install_generator.rb +31 -0
- data/lib/generators/spotlight_search/templates/application.js +15 -0
- data/lib/generators/spotlight_search/templates/coffee.js +6 -0
- data/lib/generators/spotlight_search/templates/controller.rb.erb +91 -0
- data/lib/generators/spotlight_search/templates/environment.js +19 -0
- data/lib/generators/spotlight_search/templates/filters.html.erb +12 -0
- data/lib/generators/spotlight_search/templates/scaffolds.coffee +6 -0
- data/lib/generators/spotlight_search/templates/spotlight_search.rb +1 -0
- data/lib/generators/spotlight_search/templates/webpacker.yml +102 -0
- data/lib/generators/spotlight_search/templates/webpacker_gem_assets.rb +55 -0
- data/lib/spotlight_search.rb +10 -1
- data/lib/spotlight_search/exceptions.rb +1 -1
- data/lib/spotlight_search/exportable_columns.rb +27 -32
- data/lib/spotlight_search/exportable_columns_v2.rb +90 -0
- data/lib/spotlight_search/helpers.rb +58 -9
- data/lib/spotlight_search/utils.rb +69 -0
- data/lib/spotlight_search/version.rb +1 -1
- data/spotlight_search.gemspec +3 -4
- metadata +27 -27
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b8d70a1928ef9bfa68c81817f7ab8ba5bb70848ab5c68223ad52294f2cc0e41b
|
4
|
+
data.tar.gz: 0e87e9608d99ffb50b6b6239f7af51e01f1b916919062a97b086466b8403caa5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ddd8e03cad7ed5f110c5b9e158f2f40a4efe5551371da46caf92cf3b1c57b6b2b405aee83dabf0050f732e571c7ff891407c5a65906095bddce904771772e63e
|
7
|
+
data.tar.gz: 4b3b4880334b8b5a55fb0224e4878da2673bbaade2c221194c9e178e3cd2d633dd5275bfb60ce8c353de072003ac5d2999beb92613820112aa1909736e462a2d
|
data/Gemfile.lock
CHANGED
@@ -1,103 +1,112 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
spotlight_search (0.1.
|
5
|
-
|
4
|
+
spotlight_search (0.1.9)
|
5
|
+
caxlsx (~> 3.0.1)
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: https://rubygems.org/
|
9
9
|
specs:
|
10
|
-
actioncable (5.
|
11
|
-
actionpack (= 5.
|
12
|
-
nio4r (
|
13
|
-
websocket-driver (
|
14
|
-
actionmailer (5.
|
15
|
-
actionpack (= 5.
|
16
|
-
actionview (= 5.
|
17
|
-
activejob (= 5.
|
10
|
+
actioncable (5.2.4.2)
|
11
|
+
actionpack (= 5.2.4.2)
|
12
|
+
nio4r (~> 2.0)
|
13
|
+
websocket-driver (>= 0.6.1)
|
14
|
+
actionmailer (5.2.4.2)
|
15
|
+
actionpack (= 5.2.4.2)
|
16
|
+
actionview (= 5.2.4.2)
|
17
|
+
activejob (= 5.2.4.2)
|
18
18
|
mail (~> 2.5, >= 2.5.4)
|
19
19
|
rails-dom-testing (~> 2.0)
|
20
|
-
actionpack (5.
|
21
|
-
actionview (= 5.
|
22
|
-
activesupport (= 5.
|
23
|
-
rack (~> 2.0)
|
24
|
-
rack-test (
|
20
|
+
actionpack (5.2.4.2)
|
21
|
+
actionview (= 5.2.4.2)
|
22
|
+
activesupport (= 5.2.4.2)
|
23
|
+
rack (~> 2.0, >= 2.0.8)
|
24
|
+
rack-test (>= 0.6.3)
|
25
25
|
rails-dom-testing (~> 2.0)
|
26
26
|
rails-html-sanitizer (~> 1.0, >= 1.0.2)
|
27
|
-
actionview (5.
|
28
|
-
activesupport (= 5.
|
27
|
+
actionview (5.2.4.2)
|
28
|
+
activesupport (= 5.2.4.2)
|
29
29
|
builder (~> 3.1)
|
30
|
-
|
30
|
+
erubi (~> 1.4)
|
31
31
|
rails-dom-testing (~> 2.0)
|
32
32
|
rails-html-sanitizer (~> 1.0, >= 1.0.3)
|
33
|
-
activejob (5.
|
34
|
-
activesupport (= 5.
|
33
|
+
activejob (5.2.4.2)
|
34
|
+
activesupport (= 5.2.4.2)
|
35
35
|
globalid (>= 0.3.6)
|
36
|
-
activemodel (5.
|
37
|
-
activesupport (= 5.
|
38
|
-
activerecord (5.
|
39
|
-
activemodel (= 5.
|
40
|
-
activesupport (= 5.
|
41
|
-
arel (
|
42
|
-
|
36
|
+
activemodel (5.2.4.2)
|
37
|
+
activesupport (= 5.2.4.2)
|
38
|
+
activerecord (5.2.4.2)
|
39
|
+
activemodel (= 5.2.4.2)
|
40
|
+
activesupport (= 5.2.4.2)
|
41
|
+
arel (>= 9.0)
|
42
|
+
activestorage (5.2.4.2)
|
43
|
+
actionpack (= 5.2.4.2)
|
44
|
+
activerecord (= 5.2.4.2)
|
45
|
+
marcel (~> 0.3.1)
|
46
|
+
activesupport (5.2.4.2)
|
43
47
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
44
48
|
i18n (>= 0.7, < 2)
|
45
49
|
minitest (~> 5.1)
|
46
50
|
tzinfo (~> 1.1)
|
47
|
-
arel (
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
51
|
+
arel (9.0.0)
|
52
|
+
builder (3.2.4)
|
53
|
+
caxlsx (3.0.1)
|
54
|
+
htmlentities (~> 4.3, >= 4.3.4)
|
55
|
+
mimemagic (~> 0.3)
|
56
|
+
nokogiri (~> 1.10, >= 1.10.4)
|
57
|
+
rubyzip (>= 1.3.0, < 3)
|
58
|
+
concurrent-ruby (1.1.6)
|
59
|
+
crass (1.0.6)
|
55
60
|
diff-lcs (1.3)
|
56
|
-
|
61
|
+
erubi (1.9.0)
|
57
62
|
globalid (0.4.2)
|
58
63
|
activesupport (>= 4.2.0)
|
59
64
|
htmlentities (4.3.4)
|
60
|
-
i18n (1.
|
65
|
+
i18n (1.8.2)
|
61
66
|
concurrent-ruby (~> 1.0)
|
62
|
-
loofah (2.
|
67
|
+
loofah (2.4.0)
|
63
68
|
crass (~> 1.0.2)
|
64
69
|
nokogiri (>= 1.5.9)
|
65
70
|
mail (2.7.1)
|
66
71
|
mini_mime (>= 0.1.1)
|
67
|
-
|
68
|
-
|
72
|
+
marcel (0.3.3)
|
73
|
+
mimemagic (~> 0.3.2)
|
74
|
+
method_source (1.0.0)
|
75
|
+
mimemagic (0.3.4)
|
76
|
+
mini_mime (1.0.2)
|
69
77
|
mini_portile2 (2.4.0)
|
70
|
-
minitest (5.
|
71
|
-
nio4r (2.
|
72
|
-
nokogiri (1.10.
|
78
|
+
minitest (5.14.0)
|
79
|
+
nio4r (2.5.2)
|
80
|
+
nokogiri (1.10.9)
|
73
81
|
mini_portile2 (~> 2.4.0)
|
74
|
-
rack (2.
|
75
|
-
rack-test (
|
76
|
-
rack (>= 1.0)
|
77
|
-
rails (5.
|
78
|
-
actioncable (= 5.
|
79
|
-
actionmailer (= 5.
|
80
|
-
actionpack (= 5.
|
81
|
-
actionview (= 5.
|
82
|
-
activejob (= 5.
|
83
|
-
activemodel (= 5.
|
84
|
-
activerecord (= 5.
|
85
|
-
|
82
|
+
rack (2.2.2)
|
83
|
+
rack-test (1.1.0)
|
84
|
+
rack (>= 1.0, < 3)
|
85
|
+
rails (5.2.4.2)
|
86
|
+
actioncable (= 5.2.4.2)
|
87
|
+
actionmailer (= 5.2.4.2)
|
88
|
+
actionpack (= 5.2.4.2)
|
89
|
+
actionview (= 5.2.4.2)
|
90
|
+
activejob (= 5.2.4.2)
|
91
|
+
activemodel (= 5.2.4.2)
|
92
|
+
activerecord (= 5.2.4.2)
|
93
|
+
activestorage (= 5.2.4.2)
|
94
|
+
activesupport (= 5.2.4.2)
|
86
95
|
bundler (>= 1.3.0)
|
87
|
-
railties (= 5.
|
96
|
+
railties (= 5.2.4.2)
|
88
97
|
sprockets-rails (>= 2.0.0)
|
89
98
|
rails-dom-testing (2.0.3)
|
90
99
|
activesupport (>= 4.2.0)
|
91
100
|
nokogiri (>= 1.6)
|
92
|
-
rails-html-sanitizer (1.0
|
93
|
-
loofah (~> 2.
|
94
|
-
railties (5.
|
95
|
-
actionpack (= 5.
|
96
|
-
activesupport (= 5.
|
101
|
+
rails-html-sanitizer (1.3.0)
|
102
|
+
loofah (~> 2.3)
|
103
|
+
railties (5.2.4.2)
|
104
|
+
actionpack (= 5.2.4.2)
|
105
|
+
activesupport (= 5.2.4.2)
|
97
106
|
method_source
|
98
107
|
rake (>= 0.8.7)
|
99
|
-
thor (>= 0.
|
100
|
-
rake (
|
108
|
+
thor (>= 0.19.0, < 2.0)
|
109
|
+
rake (13.0.1)
|
101
110
|
rspec (3.8.0)
|
102
111
|
rspec-core (~> 3.8.0)
|
103
112
|
rspec-expectations (~> 3.8.0)
|
@@ -111,31 +120,33 @@ GEM
|
|
111
120
|
diff-lcs (>= 1.2.0, < 2.0)
|
112
121
|
rspec-support (~> 3.8.0)
|
113
122
|
rspec-support (3.8.0)
|
114
|
-
rubyzip (
|
115
|
-
sprockets (
|
123
|
+
rubyzip (2.3.0)
|
124
|
+
sprockets (4.0.0)
|
116
125
|
concurrent-ruby (~> 1.0)
|
117
126
|
rack (> 1, < 3)
|
118
127
|
sprockets-rails (3.2.1)
|
119
128
|
actionpack (>= 4.0)
|
120
129
|
activesupport (>= 4.0)
|
121
130
|
sprockets (>= 3.0.0)
|
122
|
-
thor (0.
|
131
|
+
thor (1.0.1)
|
123
132
|
thread_safe (0.3.6)
|
124
|
-
tzinfo (1.2.
|
133
|
+
tzinfo (1.2.6)
|
125
134
|
thread_safe (~> 0.1)
|
126
|
-
websocket-driver (0.
|
135
|
+
websocket-driver (0.7.1)
|
127
136
|
websocket-extensions (>= 0.1.0)
|
128
137
|
websocket-extensions (0.1.4)
|
138
|
+
zip-zip (0.3)
|
139
|
+
rubyzip (>= 1.0.0)
|
129
140
|
|
130
141
|
PLATFORMS
|
131
142
|
ruby
|
132
143
|
|
133
144
|
DEPENDENCIES
|
134
145
|
bundler (~> 2.0)
|
135
|
-
rails (~> 5.
|
136
|
-
rake (
|
146
|
+
rails (~> 5.2.4.2)
|
147
|
+
rake (>= 12.3.3)
|
137
148
|
rspec (~> 3.0)
|
138
149
|
spotlight_search!
|
139
150
|
|
140
151
|
BUNDLED WITH
|
141
|
-
2.
|
152
|
+
2.1.4
|
data/README.md
CHANGED
@@ -1,7 +1,16 @@
|
|
1
|
+
[![Gem Version](https://badge.fury.io/rb/spotlight_search.svg)](https://badge.fury.io/rb/spotlight_search)
|
2
|
+
|
1
3
|
# SpotlightSearch
|
2
4
|
|
3
5
|
It helps filtering, sorting and exporting tables easier.
|
4
6
|
|
7
|
+
First create a new rails project with the following command. If you are adding to existing project skip this
|
8
|
+
|
9
|
+
```
|
10
|
+
rails new blog -m https://raw.githubusercontent.com/commutatus/cm-rails-template/devise_integration/template.rb
|
11
|
+
```
|
12
|
+
|
13
|
+
|
5
14
|
## Installation
|
6
15
|
|
7
16
|
Add this line to your application's Gemfile:
|
@@ -18,6 +27,31 @@ Or install it manually:
|
|
18
27
|
|
19
28
|
$ gem install spotlight_search
|
20
29
|
|
30
|
+
Generator that installs mandatory files and gems to application
|
31
|
+
|
32
|
+
$ rails g spotlight_search:install
|
33
|
+
|
34
|
+
The install generator does the following
|
35
|
+
|
36
|
+
* `require spotlight_search` added to application.js
|
37
|
+
|
38
|
+
* Copies required files for the spotlight_search to work, Such as gemassets.rb, webpacker.yml, environment.js
|
39
|
+
|
40
|
+
* Copies initializer file
|
41
|
+
|
42
|
+
* Adds a line in route for mounting.
|
43
|
+
|
44
|
+
Generator that installs filter and table files to application
|
45
|
+
|
46
|
+
$ rails g spotlight_search:filter orders --filters search:input order_status:multi_select status:select
|
47
|
+
|
48
|
+
The install generator does the following
|
49
|
+
|
50
|
+
* Copies the filter partial and the controller with necessary changes
|
51
|
+
|
52
|
+
Filter arguments can be passed as an array and the format is `filter_scope:type_of_filter_input`
|
53
|
+
|
54
|
+
|
21
55
|
## Usage
|
22
56
|
|
23
57
|
1. [Filtering, Sorting and Pagination](#filtering-sorting-and-pagination)
|
@@ -62,24 +96,20 @@ end
|
|
62
96
|
#### View
|
63
97
|
Please note that the below code is in haml.
|
64
98
|
|
65
|
-
**STEP - 1
|
99
|
+
**STEP - 1 Filters**
|
66
100
|
|
67
|
-
|
101
|
+
**Filter Wrapper, Select-tags and Inputs**
|
68
102
|
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
103
|
+
| Generator | *Mandatory(Data Attributes, Select options) | *Optional(Classes, Placeholders) |
|
104
|
+
|----------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------|
|
105
|
+
| `= filter_wrapper(data_behaviours, classes=nil)` | `{filter_url: '/users', replacement_class: 'users-table'}` | "filter-classes" |
|
106
|
+
| | | |
|
107
|
+
| `= cm_select_tag(select_options, data_behaviours, classes=nil, placeholder=nil)` | `{behaviour: "filter", scope: "status", type: "select-filter}` , User.all.map {\|user\| [user.name.titleize, user.id]} | `"user-select"`, placeholder = `"Users"` |
|
108
|
+
| | | |
|
109
|
+
| `= cm_textfield_tag(data_behaviours, classes=nil, placeholder=nil)` | `{behaviour: "filter", scope: "search", type: "input-filter}` | `"user-search"`, placeholder = `"Search"` |
|
110
|
+
| | | |
|
111
|
+
| `= clear_filters(clear_path, classes=nil, data_behaviours=nil, clear_text=nil)` | clear_path = `users_path` | `"clear-filter"`, data_behaviours = `{behaviour: 'clear'}`, clear_text = `"Clear all"` |
|
76
112
|
|
77
|
-
* `.filters` All search input / select filter should be nested inside this class name.
|
78
|
-
* `data-filter-url` Is mandatory, this is the search URL, Mostly this will hit the index action.
|
79
|
-
* `data-replacement-class` After ajax this is the class name where the data will get appended.
|
80
|
-
* `data-behaviour="filter"` If the input behaviour is set to filter then this will get added to ajax
|
81
|
-
* `data-scope="search"` This is the model scope name, The helper method will call this when filter is applied.
|
82
|
-
* `data-type="input-filter"` This is to tell if the element is input or select other value is `data-type="select-filter"`
|
83
113
|
|
84
114
|
**STEP - 2 Pagination**
|
85
115
|
|
@@ -97,22 +127,39 @@ th = sortable "name", "Name", @filtered_result.sort[:sort_column], @filtered_res
|
|
97
127
|
|
98
128
|
### Export table data to excel
|
99
129
|
|
100
|
-
|
101
|
-
An initializer will have to be created to extend the functionality to ActiveRecord.
|
130
|
+
**Note**
|
102
131
|
|
103
|
-
|
104
|
-
# config/initializers/spotlight_search.rb
|
105
|
-
ActiveRecord::Base.include SpotlightSearch::ExportableColumns
|
106
|
-
```
|
132
|
+
You will need to have a background job processor such as `sidekiq`, `resque`, `delayed_job` etc as the file will be generated in the background and will be sent to the email passed. If you need to use any other service for sending emails, you will need to override `ExportMailer` class.
|
107
133
|
|
108
|
-
#### Routes
|
109
|
-
A line has to be added to the routes.
|
110
134
|
|
111
|
-
|
112
|
-
|
135
|
+
#### <a name="export-view"></a>View
|
136
|
+
|
137
|
+
Add `exportable email, model_object` in your view to display the export button.
|
138
|
+
|
139
|
+
```html+erb
|
140
|
+
<table>
|
141
|
+
<tr>
|
142
|
+
<th>Name</th>
|
143
|
+
<th>Email</th>
|
144
|
+
</tr>
|
145
|
+
<td>
|
146
|
+
<% @records.each do |record| %>
|
147
|
+
<tr>
|
148
|
+
<td><%= record.name %></td>
|
149
|
+
<td><%= record.value %></td>
|
150
|
+
<% end %>
|
151
|
+
</td>
|
152
|
+
</table>
|
153
|
+
|
154
|
+
<%= exportable(current_user.email, Person) %>
|
113
155
|
```
|
114
156
|
|
157
|
+
This will first show a popup where an option to select the export enabled columns will be listed. This will also apply any filters that has been selected along with a sorting if applied. It then pushes the export to a background job which will send an excel file of the contents to the specified email. You can edit the style of the button using the class `export-to-file-btn`.
|
158
|
+
|
115
159
|
#### Model
|
160
|
+
|
161
|
+
##### V1
|
162
|
+
|
116
163
|
Enables or disables export and specifies which all columns can be
|
117
164
|
exported. Export is disabled for all columns by default.
|
118
165
|
|
@@ -148,33 +195,33 @@ For excluding only specific columns and allowing all others
|
|
148
195
|
end
|
149
196
|
```
|
150
197
|
|
151
|
-
|
198
|
+
##### V2
|
152
199
|
|
153
|
-
|
200
|
+
To use version two of column export, which supports model methods and nested associations, set it up in the spotlight initializer like this:
|
201
|
+
```ruby
|
202
|
+
# config/initializers/spotlight_search.rb
|
203
|
+
ActiveRecord::Base.include SpotlightSearch::ExportableColumns
|
154
204
|
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
<td>
|
162
|
-
<% @records.each do |record| %>
|
163
|
-
<tr>
|
164
|
-
<td><%= record.name %></td>
|
165
|
-
<td><%= record.value %></td>
|
166
|
-
<% end %>
|
167
|
-
</td>
|
168
|
-
</table>
|
205
|
+
SpotlightSearch.setup do |config|
|
206
|
+
config.exportable_columns_version = :v2
|
207
|
+
end
|
208
|
+
```
|
209
|
+
|
210
|
+
All fields will be disabled by default, so you will need to explicitly enable them by passing them to `export_columns`
|
169
211
|
|
170
|
-
|
212
|
+
```ruby
|
213
|
+
class Person < ActiveRecord::Base
|
214
|
+
export_columns :created_at, :formatted_amount, :preferred_month, :orderable_type, :payment_type, :status, customer: [:full_name, :email, :mobile_number, :city, :college], orderable: :orderable_display_name, seller: [:full_name]
|
215
|
+
end
|
171
216
|
```
|
172
217
|
|
173
|
-
|
218
|
+
Nested association fields should go at the end of `export_columns`, following Ruby's standard syntax of placing keyword arguments at the end
|
219
|
+
|
220
|
+
**Notes**
|
221
|
+
- You will need to make `filter_params` and `sort_params` in your controller public, or the rendering of the form will fail
|
222
|
+
- Be careful with methods that return a hash, as the algorithm will recursively create one column for every key inside that hash. One example is `Money` fields from the `money-rails` gem
|
174
223
|
|
175
|
-
**Note**
|
176
224
|
|
177
|
-
You will need to have a background job processor such as `sidekiq`, `resque`, `delayed_job` etc as the file will be generated in the background and will be sent to the email passed. If you need to use any other service for sending emails, you will need to override `ExportMailer` class.
|
178
225
|
|
179
226
|
## Development
|
180
227
|
|
@@ -2,17 +2,17 @@ module SpotlightSearch
|
|
2
2
|
class ExportJobsController < ApplicationController
|
3
3
|
def export_job
|
4
4
|
begin
|
5
|
-
klass = params[:
|
5
|
+
klass = params[:class_name].constantize
|
6
6
|
if klass.validate_exportable_columns(params[:columns])
|
7
|
-
ExportJob.perform_later(params[:email],
|
8
|
-
|
7
|
+
ExportJob.perform_later(klass.name, params[:email], params[:columns], params[:filters], params[:sort])
|
8
|
+
notice = 'Successfully queued for export'
|
9
9
|
else
|
10
|
-
|
10
|
+
notice = 'Invalid columns found'
|
11
11
|
end
|
12
|
-
rescue
|
13
|
-
|
12
|
+
rescue StandardError => e
|
13
|
+
notice = e.message
|
14
14
|
ensure
|
15
|
-
redirect_back fallback_location: root_path
|
15
|
+
redirect_back fallback_location: root_path, notice: notice
|
16
16
|
end
|
17
17
|
end
|
18
18
|
end
|
@@ -1,34 +1,46 @@
|
|
1
|
-
require '
|
1
|
+
require 'caxlsx'
|
2
2
|
|
3
3
|
module SpotlightSearch
|
4
4
|
class ExportJob < ApplicationJob
|
5
|
-
def perform(
|
6
|
-
klass =
|
7
|
-
records = get_records(klass, filters,
|
8
|
-
file_path =
|
5
|
+
def perform(klass_name, email, columns = [], filters = {}, sort = {})
|
6
|
+
klass = klass_name.constantize
|
7
|
+
records = get_records(klass, columns, filters, sort)
|
8
|
+
file_path =
|
9
|
+
case SpotlightSearch.exportable_columns_version
|
10
|
+
when :v1
|
11
|
+
create_excel(records, klass.name, columns)
|
12
|
+
when :v2
|
13
|
+
create_excel_v2(records, klass.name)
|
14
|
+
end
|
9
15
|
subject = "#{klass.name} export at #{Time.now}"
|
10
16
|
ExportMailer.send_excel_file(email, file_path, subject).deliver_now
|
11
17
|
File.delete(file_path)
|
18
|
+
rescue StandardError => e
|
19
|
+
ExportMailer.send_error_message(email, e).deliver_now
|
12
20
|
end
|
13
21
|
|
14
|
-
def get_records(klass, filters,
|
22
|
+
def get_records(klass, columns, filters, sort)
|
15
23
|
records = klass
|
16
|
-
if filters
|
17
|
-
|
18
|
-
|
19
|
-
if scope_args.is_a?(Array)
|
20
|
-
records = records.send(scope, *scope_args)
|
21
|
-
else
|
22
|
-
records = records.send(scope, scope_args)
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
if filters['sort'].present?
|
27
|
-
records = records.order("#{filters['sort']['sort_column']} #{filters['sort']['sort_direction']}")
|
24
|
+
if filters.present?
|
25
|
+
filters.each do |scope, scope_args|
|
26
|
+
records = records.send(scope, scope_args)
|
28
27
|
end
|
29
28
|
end
|
30
|
-
|
31
|
-
|
29
|
+
if sort.present?
|
30
|
+
records = records.order("#{sort['sort_column']} #{sort['sort_direction']}")
|
31
|
+
end
|
32
|
+
if filters.blank? && sort.blank?
|
33
|
+
records = records.all
|
34
|
+
end
|
35
|
+
case SpotlightSearch.exportable_columns_version
|
36
|
+
when :v1
|
37
|
+
columns = columns.map(&:to_sym)
|
38
|
+
records.select(*columns)
|
39
|
+
when :v2
|
40
|
+
deserialized_columns = SpotlightSearch::Utils.deserialize_csv_columns(columns, :as_json_params)
|
41
|
+
# This includes isn't recursve, a full solution should be recursive
|
42
|
+
records.includes(deserialized_columns[:include].keys).find_each.as_json(deserialized_columns)
|
43
|
+
end
|
32
44
|
end
|
33
45
|
|
34
46
|
# Creating excel with the passed records
|
@@ -48,5 +60,23 @@ module SpotlightSearch
|
|
48
60
|
xl.serialize(file_location)
|
49
61
|
file_location
|
50
62
|
end
|
63
|
+
|
64
|
+
def create_excel_v2(records, class_name)
|
65
|
+
flattened_records = records.map { |record| SpotlightSearch::Utils.flatten_hash(record) }
|
66
|
+
columns = flattened_records[0].keys
|
67
|
+
size_arr = []
|
68
|
+
columns.size.times { size_arr << 22 }
|
69
|
+
xl = Axlsx::Package.new
|
70
|
+
xl.workbook.add_worksheet do |sheet|
|
71
|
+
sheet.add_row columns, b: true
|
72
|
+
flattened_records.each do |record|
|
73
|
+
sheet.add_row(columns.map { |column| record[column] })
|
74
|
+
end
|
75
|
+
sheet.column_widths(*size_arr)
|
76
|
+
end
|
77
|
+
file_location = "#{Rails.root}/public/export_#{class_name}_#{Time.now.to_s}.xls"
|
78
|
+
xl.serialize(file_location)
|
79
|
+
file_location
|
80
|
+
end
|
51
81
|
end
|
52
82
|
end
|