spotlight_search 0.1.8 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (28) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +81 -72
  3. data/README.md +93 -45
  4. data/app/controllers/spotlight_search/export_jobs_controller.rb +7 -7
  5. data/app/jobs/spotlight_search/export_job.rb +50 -20
  6. data/app/mailers/spotlight_search/export_mailer.rb +6 -0
  7. data/app/views/spotlight_search/export_mailer/send_error_message.html.erb +7 -0
  8. data/lib/generators/spotlight_search/filter_generator.rb +21 -0
  9. data/lib/generators/spotlight_search/install_generator.rb +27 -0
  10. data/lib/generators/spotlight_search/templates/application.css.scss +26 -0
  11. data/lib/generators/spotlight_search/templates/application.js +19 -0
  12. data/lib/generators/spotlight_search/templates/coffee.js +6 -0
  13. data/lib/generators/spotlight_search/templates/controller.rb.erb +91 -0
  14. data/lib/generators/spotlight_search/templates/environment.js +20 -0
  15. data/lib/generators/spotlight_search/templates/filters.html.erb +10 -0
  16. data/lib/generators/spotlight_search/templates/scaffolds.coffee +11 -0
  17. data/lib/generators/spotlight_search/templates/spotlight_search.rb +5 -0
  18. data/lib/generators/spotlight_search/templates/webpacker.yml +102 -0
  19. data/lib/generators/spotlight_search/templates/webpacker_gem_assets.rb +55 -0
  20. data/lib/spotlight_search.rb +9 -0
  21. data/lib/spotlight_search/exceptions.rb +1 -1
  22. data/lib/spotlight_search/exportable_columns.rb +24 -29
  23. data/lib/spotlight_search/exportable_columns_v2.rb +90 -0
  24. data/lib/spotlight_search/helpers.rb +80 -14
  25. data/lib/spotlight_search/utils.rb +69 -0
  26. data/lib/spotlight_search/version.rb +1 -1
  27. data/spotlight_search.gemspec +3 -4
  28. metadata +27 -26
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3e9cd4941331e8c05faf205f5e88b871d70281e28b537ff8884a873c4c039c01
4
- data.tar.gz: 65588a311e3dd0ca07e73064f65eb38dd92eaf21c525da71866a4621edf419bc
3
+ metadata.gz: db095dee4fcbb232c1f7eddd83dfdfe279243415f3f6e54636740c2abbbe9ac3
4
+ data.tar.gz: c93d9955d07504a7aecca13cca15d1655fa847d0c13b1dc3643414ceeab39cb3
5
5
  SHA512:
6
- metadata.gz: e4e0877a70a4340f373fbcb5cad506bdb70311db3f04cb57a9ff8985f1b1321388e9e37f9109c2a262b798b450b233449b0401f1a8799ff76808733f8ec39499
7
- data.tar.gz: b7a088d2af85d00e71663cc50a37cacc149432f188e84907c33d138a2380ed705f0fb5fad8252c841d3f33d9736e0891c1b90f8e3d310ef05bb6c62a6e8d837b
6
+ metadata.gz: 67fc0b1c020ee49f59dbf539bf7854fcf65a8d8ce393b6315450c07812bec38020ace65fd29c36d76fb7408ddee3a6296c92ebf94dddc13026bc000660e26651
7
+ data.tar.gz: f4b76130d03a78c7fefdf61feffa8a61712097bd9807b479e3d9f41246fa69ed45bed3071a4df6b59ca94f5a3ea03dc0f1ecbdf74fdc701c6b845b92354b3123
@@ -1,103 +1,112 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- spotlight_search (0.1.1)
5
- axlsx
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.0.7.2)
11
- actionpack (= 5.0.7.2)
12
- nio4r (>= 1.2, < 3.0)
13
- websocket-driver (~> 0.6.1)
14
- actionmailer (5.0.7.2)
15
- actionpack (= 5.0.7.2)
16
- actionview (= 5.0.7.2)
17
- activejob (= 5.0.7.2)
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.0.7.2)
21
- actionview (= 5.0.7.2)
22
- activesupport (= 5.0.7.2)
23
- rack (~> 2.0)
24
- rack-test (~> 0.6.3)
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.0.7.2)
28
- activesupport (= 5.0.7.2)
27
+ actionview (5.2.4.2)
28
+ activesupport (= 5.2.4.2)
29
29
  builder (~> 3.1)
30
- erubis (~> 2.7.0)
30
+ erubi (~> 1.4)
31
31
  rails-dom-testing (~> 2.0)
32
32
  rails-html-sanitizer (~> 1.0, >= 1.0.3)
33
- activejob (5.0.7.2)
34
- activesupport (= 5.0.7.2)
33
+ activejob (5.2.4.2)
34
+ activesupport (= 5.2.4.2)
35
35
  globalid (>= 0.3.6)
36
- activemodel (5.0.7.2)
37
- activesupport (= 5.0.7.2)
38
- activerecord (5.0.7.2)
39
- activemodel (= 5.0.7.2)
40
- activesupport (= 5.0.7.2)
41
- arel (~> 7.0)
42
- activesupport (5.0.7.2)
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 (7.1.4)
48
- axlsx (2.0.1)
49
- htmlentities (~> 4.3.1)
50
- nokogiri (>= 1.4.1)
51
- rubyzip (~> 1.0.0)
52
- builder (3.2.3)
53
- concurrent-ruby (1.1.5)
54
- crass (1.0.4)
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
- erubis (2.7.0)
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.6.0)
65
+ i18n (1.8.2)
61
66
  concurrent-ruby (~> 1.0)
62
- loofah (2.2.3)
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
- method_source (0.9.2)
68
- mini_mime (1.0.1)
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.11.3)
71
- nio4r (2.3.1)
72
- nokogiri (1.10.3)
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.0.7)
75
- rack-test (0.6.3)
76
- rack (>= 1.0)
77
- rails (5.0.7.2)
78
- actioncable (= 5.0.7.2)
79
- actionmailer (= 5.0.7.2)
80
- actionpack (= 5.0.7.2)
81
- actionview (= 5.0.7.2)
82
- activejob (= 5.0.7.2)
83
- activemodel (= 5.0.7.2)
84
- activerecord (= 5.0.7.2)
85
- activesupport (= 5.0.7.2)
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.0.7.2)
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.4)
93
- loofah (~> 2.2, >= 2.2.2)
94
- railties (5.0.7.2)
95
- actionpack (= 5.0.7.2)
96
- activesupport (= 5.0.7.2)
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.18.1, < 2.0)
100
- rake (10.5.0)
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,31 @@ 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 (1.0.0)
115
- sprockets (3.7.2)
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.20.3)
131
+ thor (1.0.1)
123
132
  thread_safe (0.3.6)
124
- tzinfo (1.2.5)
133
+ tzinfo (1.2.6)
125
134
  thread_safe (~> 0.1)
126
- websocket-driver (0.6.5)
135
+ websocket-driver (0.7.1)
127
136
  websocket-extensions (>= 0.1.0)
128
- websocket-extensions (0.1.4)
137
+ websocket-extensions (0.1.5)
129
138
 
130
139
  PLATFORMS
131
140
  ruby
132
141
 
133
142
  DEPENDENCIES
134
143
  bundler (~> 2.0)
135
- rails (~> 5.0.7)
136
- rake (~> 10.0)
144
+ rails (~> 5.2.4.2)
145
+ rake (>= 12.3.3)
137
146
  rspec (~> 3.0)
138
147
  spotlight_search!
139
148
 
140
149
  BUNDLED WITH
141
- 2.0.1
150
+ 2.1.4
data/README.md CHANGED
@@ -4,6 +4,13 @@
4
4
 
5
5
  It helps filtering, sorting and exporting tables easier.
6
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
+
7
14
  ## Installation
8
15
 
9
16
  Add this line to your application's Gemfile:
@@ -20,6 +27,34 @@ Or install it manually:
20
27
 
21
28
  $ gem install spotlight_search
22
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
+ $ rails g spotlight_search filter orders --filters scope_name:filter_type
46
+ $ rails g spotlight_search filter orders --filters search:input order_status:multi_select status:select
47
+
48
+ scope_name is the model scope name, scope can written after running this generator, it won't throw any error that it has to be present.
49
+
50
+ Following filter type are supported
51
+ * input
52
+ * single-select
53
+ * multi-select
54
+ * datetime
55
+ * daterange
56
+
57
+
23
58
  ## Usage
24
59
 
25
60
  1. [Filtering, Sorting and Pagination](#filtering-sorting-and-pagination)
@@ -64,24 +99,20 @@ end
64
99
  #### View
65
100
  Please note that the below code is in haml.
66
101
 
67
- **STEP - 1 Search**
68
-
69
- First step is to add the input box to search. Here there are few elements that should be placed mandatorily.
102
+ **STEP - 1 Filters**
70
103
 
71
- ```
72
- .filters.w-100 data-filter-url="/admin/workshops" data-replacement-class="workshops_table"
73
- .col-md-4.input-group.search
74
- input#workshop-search-filter.form-control.filter-box name=("search_term_for_workshops ") placeholder=("Search Workshops") type="text" data-behaviour="filter" data-scope="search" data-type="input-filter"
75
- ```
104
+ **Filter Wrapper, Select-tags and Inputs**
76
105
 
77
- The elements that should be placed mandatorily are
106
+ | Generator | *Mandatory(Data Attributes, Select options) | *Optional(Classes, Placeholders) |
107
+ |----------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------|
108
+ | `= filter_wrapper(data_behaviours, classes=nil)` | `{filter_url: '/users', replacement_class: 'users-table'}` | "filter-classes" |
109
+ | | | |
110
+ | `= 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"` |
111
+ | | | |
112
+ | `= cm_textfield_tag(data_behaviours, classes=nil, placeholder=nil)` | `{behaviour: "filter", scope: "search", type: "input-filter}` | `"user-search"`, placeholder = `"Search"` |
113
+ | | | |
114
+ | `= 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"` |
78
115
 
79
- * `.filters` All search input / select filter should be nested inside this class name.
80
- * `data-filter-url` Is mandatory, this is the search URL, Mostly this will hit the index action.
81
- * `data-replacement-class` After ajax this is the class name where the data will get appended.
82
- * `data-behaviour="filter"` If the input behaviour is set to filter then this will get added to ajax
83
- * `data-scope="search"` This is the model scope name, The helper method will call this when filter is applied.
84
- * `data-type="input-filter"` This is to tell if the element is input or select other value is `data-type="select-filter"`
85
116
 
86
117
  **STEP - 2 Pagination**
87
118
 
@@ -99,22 +130,39 @@ th = sortable "name", "Name", @filtered_result.sort[:sort_column], @filtered_res
99
130
 
100
131
  ### Export table data to excel
101
132
 
102
- #### Initializer
103
- An initializer will have to be created to extend the functionality to ActiveRecord.
133
+ **Note**
104
134
 
105
- ```ruby
106
- # config/initializers/spotlight_search.rb
107
- ActiveRecord::Base.include SpotlightSearch::ExportableColumns
108
- ```
135
+ 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.
109
136
 
110
- #### Routes
111
- A line has to be added to the routes.
112
137
 
113
- ```ruby
114
- mount SpotlightSearch::Engine => '/spotlight_search'
138
+ #### <a name="export-view"></a>View
139
+
140
+ Add `exportable email, model_object` in your view to display the export button.
141
+
142
+ ```html+erb
143
+ <table>
144
+ <tr>
145
+ <th>Name</th>
146
+ <th>Email</th>
147
+ </tr>
148
+ <td>
149
+ <% @records.each do |record| %>
150
+ <tr>
151
+ <td><%= record.name %></td>
152
+ <td><%= record.value %></td>
153
+ <% end %>
154
+ </td>
155
+ </table>
156
+
157
+ <%= exportable(current_user.email, Person) %>
115
158
  ```
116
159
 
160
+ 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`.
161
+
117
162
  #### Model
163
+
164
+ ##### V1
165
+
118
166
  Enables or disables export and specifies which all columns can be
119
167
  exported. Export is disabled for all columns by default.
120
168
 
@@ -150,33 +198,33 @@ For excluding only specific columns and allowing all others
150
198
  end
151
199
  ```
152
200
 
153
- #### <a name="export-view"></a>View
201
+ ##### V2
154
202
 
155
- Add `exportable email, model_object` in your view to display the export button.
203
+ To use version two of column export, which supports model methods and nested associations, set it up in the spotlight initializer like this:
204
+ ```ruby
205
+ # config/initializers/spotlight_search.rb
206
+ ActiveRecord::Base.include SpotlightSearch::ExportableColumns
156
207
 
157
- ```html+erb
158
- <table>
159
- <tr>
160
- <th>Name</th>
161
- <th>Email</th>
162
- </tr>
163
- <td>
164
- <% @records.each do |record| %>
165
- <tr>
166
- <td><%= record.name %></td>
167
- <td><%= record.value %></td>
168
- <% end %>
169
- </td>
170
- </table>
208
+ SpotlightSearch.setup do |config|
209
+ config.exportable_columns_version = :v2
210
+ end
211
+ ```
212
+
213
+ All fields will be disabled by default, so you will need to explicitly enable them by passing them to `export_columns`
171
214
 
172
- <%= exportable(current_user.email, current_user.class) %>
215
+ ```ruby
216
+ class Person < ActiveRecord::Base
217
+ 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]
218
+ end
173
219
  ```
174
220
 
175
- 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`.
221
+ Nested association fields should go at the end of `export_columns`, following Ruby's standard syntax of placing keyword arguments at the end
222
+
223
+ **Notes**
224
+ - You will need to make `filter_params` and `sort_params` in your controller public, or the rendering of the form will fail
225
+ - 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
176
226
 
177
- **Note**
178
227
 
179
- 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.
180
228
 
181
229
  ## Development
182
230
 
@@ -2,17 +2,17 @@ module SpotlightSearch
2
2
  class ExportJobsController < ApplicationController
3
3
  def export_job
4
4
  begin
5
- klass = params[:klass].constantize
5
+ klass = params[:class_name].constantize
6
6
  if klass.validate_exportable_columns(params[:columns])
7
- ExportJob.perform_later(params[:email], klass.to_s, params[:columns], params[:filters])
8
- flash[:success] = 'Successfully queued for export'
7
+ ExportJob.perform_later(klass.name, params[:email], params[:columns], params[:filters], params[:sort])
8
+ notice = 'Successfully queued for export'
9
9
  else
10
- flash[:error] = 'Invalid columns found'
10
+ notice = 'Invalid columns found'
11
11
  end
12
- rescue
13
- flash[:error] = 'No records to import'
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 'axlsx'
1
+ require 'caxlsx'
2
2
 
3
3
  module SpotlightSearch
4
4
  class ExportJob < ApplicationJob
5
- def perform(email, klass, columns = [], filters = {})
6
- klass = klass.constantize
7
- records = get_records(klass, filters, columns)
8
- file_path = create_excel(records, klass.name, columns)
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, columns)
22
+ def get_records(klass, columns, filters, sort)
15
23
  records = klass
16
- if filters
17
- if filters['filters'].present?
18
- filters['filters'].each do |scope, scope_args|
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
- columns = columns.map(&:to_sym)
31
- records.select(*columns)
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