mini-smart-box 0.0.1

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.
Files changed (38) hide show
  1. checksums.yaml +7 -0
  2. data/elasticsearch-rails-8.0.1/CHANGELOG.md +44 -0
  3. data/elasticsearch-rails-8.0.1/Gemfile +38 -0
  4. data/elasticsearch-rails-8.0.1/LICENSE.txt +202 -0
  5. data/elasticsearch-rails-8.0.1/README.md +149 -0
  6. data/elasticsearch-rails-8.0.1/Rakefile +67 -0
  7. data/elasticsearch-rails-8.0.1/elasticsearch-rails.gemspec +67 -0
  8. data/elasticsearch-rails-8.0.1/lib/elasticsearch/rails/instrumentation/controller_runtime.rb +58 -0
  9. data/elasticsearch-rails-8.0.1/lib/elasticsearch/rails/instrumentation/log_subscriber.rb +67 -0
  10. data/elasticsearch-rails-8.0.1/lib/elasticsearch/rails/instrumentation/publishers.rb +53 -0
  11. data/elasticsearch-rails-8.0.1/lib/elasticsearch/rails/instrumentation/railtie.rb +44 -0
  12. data/elasticsearch-rails-8.0.1/lib/elasticsearch/rails/instrumentation.rb +53 -0
  13. data/elasticsearch-rails-8.0.1/lib/elasticsearch/rails/lograge.rb +57 -0
  14. data/elasticsearch-rails-8.0.1/lib/elasticsearch/rails/tasks/import.rb +128 -0
  15. data/elasticsearch-rails-8.0.1/lib/elasticsearch/rails/version.rb +22 -0
  16. data/elasticsearch-rails-8.0.1/lib/elasticsearch/rails.rb +24 -0
  17. data/elasticsearch-rails-8.0.1/lib/rails/templates/01-basic.rb +364 -0
  18. data/elasticsearch-rails-8.0.1/lib/rails/templates/02-pretty.rb +348 -0
  19. data/elasticsearch-rails-8.0.1/lib/rails/templates/03-expert.rb +358 -0
  20. data/elasticsearch-rails-8.0.1/lib/rails/templates/04-dsl.rb +146 -0
  21. data/elasticsearch-rails-8.0.1/lib/rails/templates/05-settings-files.rb +88 -0
  22. data/elasticsearch-rails-8.0.1/lib/rails/templates/articles.yml.gz +0 -0
  23. data/elasticsearch-rails-8.0.1/lib/rails/templates/articles_settings.json +1 -0
  24. data/elasticsearch-rails-8.0.1/lib/rails/templates/index.html.dsl.erb +178 -0
  25. data/elasticsearch-rails-8.0.1/lib/rails/templates/index.html.erb +178 -0
  26. data/elasticsearch-rails-8.0.1/lib/rails/templates/indexer.rb +44 -0
  27. data/elasticsearch-rails-8.0.1/lib/rails/templates/search.css +76 -0
  28. data/elasticsearch-rails-8.0.1/lib/rails/templates/search_controller_test.dsl.rb +148 -0
  29. data/elasticsearch-rails-8.0.1/lib/rails/templates/search_controller_test.rb +148 -0
  30. data/elasticsearch-rails-8.0.1/lib/rails/templates/searchable.dsl.rb +234 -0
  31. data/elasticsearch-rails-8.0.1/lib/rails/templates/searchable.rb +224 -0
  32. data/elasticsearch-rails-8.0.1/lib/rails/templates/seeds.rb +75 -0
  33. data/elasticsearch-rails-8.0.1/spec/instrumentation/log_subscriber_spec.rb +57 -0
  34. data/elasticsearch-rails-8.0.1/spec/instrumentation_spec.rb +103 -0
  35. data/elasticsearch-rails-8.0.1/spec/lograge_spec.rb +52 -0
  36. data/elasticsearch-rails-8.0.1/spec/spec_helper.rb +65 -0
  37. data/mini-smart-box.gemspec +11 -0
  38. metadata +76 -0
@@ -0,0 +1,348 @@
1
+ # Licensed to Elasticsearch B.V. under one or more contributor
2
+ # license agreements. See the NOTICE file distributed with
3
+ # this work for additional information regarding copyright
4
+ # ownership. Elasticsearch B.V. licenses this file to you under
5
+ # the Apache License, Version 2.0 (the "License"); you may
6
+ # not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing,
12
+ # software distributed under the License is distributed on an
13
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
+ # KIND, either express or implied. See the License for the
15
+ # specific language governing permissions and limitations
16
+ # under the License.
17
+
18
+ # $ rails new searchapp --skip --skip-bundle --template https://raw.github.com/elasticsearch/elasticsearch-rails/main/elasticsearch-rails/lib/rails/templates/02-pretty.rb
19
+
20
+ unless File.read('README.md').include? '## [1] Basic'
21
+ say_status "ERROR", "You have to run the 01-basic.rb template first.", :red
22
+ exit(1)
23
+ end
24
+
25
+ puts
26
+ say_status "README", "Updating Readme...\n", :yellow
27
+ puts '-'*80, ''; sleep 0.25
28
+
29
+ append_to_file 'README.md', <<-README
30
+
31
+ ## [2] Pretty
32
+
33
+ The `pretty` template builds on the `basic` version and brings couple of improvements:
34
+
35
+ * Using the [Bootstrap](http://getbootstrap.com) framework to enhance the visual style of the application
36
+ * Using an `Article.search` class method to customize the default search definition
37
+ * Highlighting matching phrases in search results
38
+ * Paginating results with Kaminari
39
+
40
+ README
41
+
42
+ git add: "README.md"
43
+ git commit: "-m '[02] Updated the application README'"
44
+
45
+ # ----- Update application.rb ---------------------------------------------------------------------
46
+
47
+ puts
48
+ say_status "Rubygems", "Adding Rails logger integration...\n", :yellow
49
+ puts '-'*80, ''; sleep 0.25
50
+
51
+ insert_into_file 'config/application.rb',
52
+ "\n\nrequire 'elasticsearch/rails/instrumentation'",
53
+ after: /Bundler\.require.+$/
54
+
55
+ git add: "config/application.rb"
56
+ git commit: "-m 'Added the Rails logger integration to application.rb'"
57
+
58
+ # ----- Add gems into Gemfile ---------------------------------------------------------------------
59
+
60
+ puts
61
+ say_status "Rubygems", "Adding Rubygems into Gemfile...\n", :yellow
62
+ puts '-'*80, ''; sleep 0.25
63
+
64
+ # NOTE: Kaminari has to be loaded before Elasticsearch::Model so the callbacks are executed
65
+ #
66
+ insert_into_file 'Gemfile', <<-CODE, before: /gem ["']elasticsearch["'].+$/
67
+
68
+ # NOTE: Kaminari has to be loaded before Elasticsearch::Model so the callbacks are executed
69
+ gem 'kaminari'
70
+
71
+ CODE
72
+
73
+ run "bundle install"
74
+
75
+ git add: "Gemfile*"
76
+ git commit: "-m 'Added the Kaminari gem'"
77
+
78
+ # ----- Add `Article.search` class method ---------------------------------------------------------
79
+
80
+ puts
81
+ say_status "Model", "Adding a `Article.search` class method...\n", :yellow
82
+ puts '-'*80, ''; sleep 0.5
83
+
84
+ insert_into_file 'app/models/article.rb', <<-CODE, after: 'include Elasticsearch::Model::Callbacks'
85
+
86
+
87
+ def self.search(query)
88
+ __elasticsearch__.search(
89
+ {
90
+ query: {
91
+ multi_match: {
92
+ query: query,
93
+ fields: ['title^10', 'content']
94
+ }
95
+ },
96
+ highlight: {
97
+ pre_tags: ['<em class="label label-highlight">'],
98
+ post_tags: ['</em>'],
99
+ fields: {
100
+ title: { number_of_fragments: 0 },
101
+ content: { fragment_size: 25 }
102
+ }
103
+ }
104
+ }
105
+ )
106
+ end
107
+ CODE
108
+
109
+ insert_into_file "#{Rails::VERSION::STRING > '4' ? 'test/models' : 'test/unit' }/article_test.rb", <<-CODE, after: /class ArticleTest < ActiveSupport::TestCase$/
110
+
111
+ teardown do
112
+ Article.__elasticsearch__.unstub(:search)
113
+ end
114
+
115
+ CODE
116
+
117
+ gsub_file "#{Rails::VERSION::STRING > '4' ? 'test/models' : 'test/unit' }/article_test.rb", %r{# test "the truth" do.*?# end}m, <<-CODE
118
+
119
+ test "has a search method delegating to __elasticsearch__" do
120
+ Article.__elasticsearch__.expects(:search).with do |definition|
121
+ assert_equal 'foo', definition[:query][:multi_match][:query]
122
+ true
123
+ end
124
+
125
+ Article.search 'foo'
126
+ end
127
+ CODE
128
+
129
+ insert_into_file "test/test_helper.rb",
130
+ "require 'mocha/minitest'\n\n",
131
+ before: "class ActiveSupport::TestCase\n"
132
+
133
+ git add: "app/models/article.rb"
134
+ git add: "test/**/article_test.rb"
135
+ git add: "test/test_helper.rb"
136
+ git commit: "-m 'Added an `Article.search` method'"
137
+
138
+ # ----- Add loading Bootstrap assets --------------------------------------------------------------
139
+
140
+ puts
141
+ say_status "Bootstrap", "Adding Bootstrap asset links into the 'application' layout...\n", :yellow
142
+ puts '-'*80, ''; sleep 0.5
143
+
144
+ gsub_file 'app/views/layouts/application.html.erb', %r{<%= yield %>}, <<-CODE unless File.read('app/views/layouts/application.html.erb').include?('class="container"')
145
+ <div class="container">
146
+ <%= yield %>
147
+ </div>
148
+ CODE
149
+
150
+ insert_into_file 'app/views/layouts/application.html.erb', <<-CODE, before: '</head>'
151
+ <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
152
+ <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
153
+ <script src="https://stackpath.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
154
+ CODE
155
+
156
+ git commit: "-a -m 'Added loading Bootstrap assets in the application layout'"
157
+
158
+ # ----- Customize the search form -----------------------------------------------------------------
159
+
160
+ puts
161
+ say_status "Bootstrap", "Customizing the index page...\n", :yellow
162
+ puts '-'*80, ''; sleep 0.5
163
+
164
+ gsub_file 'app/views/articles/index.html.erb', %r{<%= label_tag .* :search %>}m do |match|
165
+ <<-CODE
166
+ <div class="input-group">
167
+ <%= text_field_tag :q, params[:q], class: 'form-control', placeholder: 'Search...' %>
168
+ </div>
169
+ CODE
170
+ end
171
+
172
+ # ----- Customize the header -----------------------------------------------------------------
173
+
174
+ gsub_file 'app/views/articles/index.html.erb', %r{<h1>.*Articles</h1>} do |match|
175
+ "<h1><%= controller.action_name == 'search' ? 'Search results' : 'Articles' %></h1>"
176
+ end
177
+
178
+ # ----- Customize the results listing -------------------------------------------------------------
179
+
180
+ gsub_file 'app/views/articles/index.html.erb', %r{<table>} do |match|
181
+ '<table class="table table-hover">'
182
+ end
183
+
184
+ gsub_file 'app/views/articles/index.html.erb', %r{<td><%= link_to [^%]+} do |match|
185
+ match.gsub!('<td>', '<td style="width: 50px">')
186
+ match.include?("btn") ? match : (match + ", class: 'btn btn-outline-primary btn-sm'")
187
+ end
188
+
189
+ gsub_file 'app/views/articles/index.html.erb', %r{<br>\s*(<\%= link_to 'New Article'.*)}m do |content|
190
+ replace = content.match(%r{<br>\s*(<\%= link_to 'New Article'.*)}m)[1]
191
+ <<-END.gsub(/^ /, '')
192
+ <hr>
193
+
194
+ <p style="text-align: center; margin-bottom: 21px">
195
+ #{replace}
196
+ </p>
197
+ END
198
+ end
199
+
200
+ gsub_file 'app/views/articles/index.html.erb', %r{<%= link_to 'New Article',\s*new_article_path} do |match|
201
+ return match if match.include?('btn')
202
+ match + ", class: 'btn btn-primary btn-xs', style: 'color: #fff'"
203
+ end
204
+
205
+ gsub_file 'app/views/articles/index.html.erb', %r{<%= link_to 'All Articles',\s*articles_path} do |match|
206
+ return match if match.include?('btn')
207
+ "\n " + match + ", class: 'btn btn-primary btn-xs', style: 'color: #fff'"
208
+ end
209
+
210
+ # ----- Customize the form -----------------------------------------------------------------
211
+
212
+ gsub_file 'app/views/articles/_form.html.erb', %r{<div class="field">} do |match|
213
+ %Q|<div class="form-group">|
214
+ end
215
+
216
+ git add: "app/views"
217
+ git commit: "-m 'Refactored the articles listing to use Bootstrap components'"
218
+
219
+ # ----- Use highlighted excerpts in the listing ---------------------------------------------------
220
+
221
+ gsub_file 'app/views/articles/index.html.erb', %r{<% @articles.each do \|article\| %>$} do |match|
222
+ "<% @articles.__send__ controller.action_name == 'search' ? :each_with_hit : :each do |article, hit| %>"
223
+ end
224
+
225
+ gsub_file 'app/views/articles/index.html.erb', %r{<td><%= article.title %></td>$} do |match|
226
+ "<td><%= hit.try(:highlight).try(:title) ? hit.highlight.title.join.html_safe : article.title %></td>"
227
+ end
228
+
229
+ gsub_file 'app/views/articles/index.html.erb', %r{<td><%= article.content %></td>$} do |match|
230
+ "<td><%= hit.try(:highlight).try(:content) ? hit.highlight.content.join('&hellip;').html_safe : article.content %></td>"
231
+ end
232
+
233
+ git commit: "-a -m 'Added highlighting for matches'"
234
+
235
+ # ----- Paginate the results ----------------------------------------------------------------------
236
+
237
+ gsub_file 'app/controllers/articles_controller.rb', %r{@articles = Article.all} do |match|
238
+ "@articles = Article.page(params[:page])"
239
+ end
240
+
241
+ gsub_file 'app/controllers/articles_controller.rb', %r{@articles = Article.search\(params\[\:q\]\).records} do |match|
242
+ "@articles = Article.search(params[:q]).page(params[:page]).records"
243
+ end
244
+
245
+ insert_into_file 'app/views/articles/index.html.erb', after: '</table>' do
246
+ <<-CODE.gsub(/^ /, '')
247
+
248
+
249
+ <div class="text-center">
250
+ <%= paginate @articles %>
251
+ </div>
252
+ CODE
253
+ end
254
+
255
+ generate "kaminari:views", "bootstrap3", "--force"
256
+
257
+ gsub_file 'app/views/kaminari/_paginator.html.erb', %r{<nav>}, '<nav class="pagination">'
258
+
259
+ git add: "."
260
+ git commit: "-m 'Added pagination to articles listing'"
261
+
262
+ # ----- Custom CSS --------------------------------------------------------------------------------
263
+
264
+ puts
265
+ say_status "CSS", "Adding custom styles...\n", :yellow
266
+ puts '-'*80, ''; sleep 0.5
267
+
268
+ append_to_file 'app/assets/stylesheets/application.css' do
269
+ unless File.read('app/assets/stylesheets/application.css').include?('.label-highlight')
270
+ <<-CODE
271
+
272
+ body * {
273
+ font-size: 100% !important;
274
+ }
275
+
276
+ .table {
277
+ border-bottom: 1px solid #dee2e6;
278
+ }
279
+
280
+ .table td {
281
+ vertical-align: middle !important;
282
+ }
283
+
284
+ .label-highlight {
285
+ font-size: 100% !important;
286
+ font-weight: inherit !important;
287
+ font-style: inherit !important;
288
+ color: #333 !important;
289
+ background: #fff401 !important;
290
+ padding: 0.25em 0.5em
291
+ border-radius: 5px;
292
+ }
293
+
294
+ nav.pagination {
295
+ text-align: center;
296
+ display: inline-block;
297
+ }
298
+
299
+ ul.pagination {
300
+ margin-bottom: 0;
301
+ }
302
+
303
+ CODE
304
+ else
305
+ ''
306
+ end
307
+ end
308
+
309
+ git commit: "-a -m 'Added custom style definitions into application.css'"
310
+
311
+ # ----- Generate 1,000 articles -------------------------------------------------------------------
312
+
313
+ puts
314
+ say_status "Database", "Creating 1,000 articles...", :yellow
315
+ puts '-'*80, '';
316
+
317
+ run "rails runner 'Article.__elasticsearch__.create_index! force: true'"
318
+ rake "db:seed COUNT=1_000"
319
+
320
+ # ----- Print Git log -----------------------------------------------------------------------------
321
+
322
+ puts
323
+ say_status "Git", "Details about the application:", :yellow
324
+ puts '-'*80, ''
325
+
326
+ git tag: "pretty"
327
+ git log: "--reverse --oneline pretty...basic"
328
+
329
+ # ----- Start the application ---------------------------------------------------------------------
330
+
331
+ unless ENV['RAILS_NO_SERVER_START']
332
+ require 'net/http'
333
+ if (begin; Net::HTTP.get(URI('http://localhost:3000')); rescue Errno::ECONNREFUSED; false; rescue Exception; true; end)
334
+ puts "\n"
335
+ say_status "ERROR", "Some other application is running on port 3000!\n", :red
336
+ puts '-'*80
337
+
338
+ port = ask("Please provide free port:", :bold)
339
+ else
340
+ port = '3000'
341
+ end
342
+
343
+ puts "", "="*80
344
+ say_status "DONE", "\e[1mStarting the application. Open http://localhost:#{port}\e[0m", :yellow
345
+ puts "="*80, ""
346
+
347
+ run "rails server --port=#{port}"
348
+ end