workarea-search_autocomplete 1.0.0

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 (88) hide show
  1. checksums.yaml +7 -0
  2. data/.editorconfig +20 -0
  3. data/.eslintrc.json +35 -0
  4. data/.github/workflows/ci.yml +60 -0
  5. data/.gitignore +20 -0
  6. data/.rubocop.yml +3 -0
  7. data/.stylelintrc.json +8 -0
  8. data/CHANGELOG.md +36 -0
  9. data/Gemfile +17 -0
  10. data/README.md +17 -0
  11. data/Rakefile +60 -0
  12. data/app/assets/javascripts/workarea/storefront/search_autocomplete/config.js +12 -0
  13. data/app/assets/javascripts/workarea/storefront/search_autocomplete/modules/search_autocomplete.js +76 -0
  14. data/app/assets/stylesheets/workarea/storefront/search_autocomplete/components/_page_header.scss +9 -0
  15. data/app/assets/stylesheets/workarea/storefront/search_autocomplete/components/_search_autocomplete.scss +53 -0
  16. data/app/controllers/workarea/storefront/searches_controller.decorator +10 -0
  17. data/app/models/workarea/metrics/search_by_week.decorator +37 -0
  18. data/app/view_models/workarea/storefront/search_autocomplete_products_view_model.rb +13 -0
  19. data/app/view_models/workarea/storefront/search_autocomplete_view_model.rb +66 -0
  20. data/app/views/workarea/storefront/searches/_autocomplete_placeholder.html.haml +2 -0
  21. data/app/views/workarea/storefront/searches/autocomplete.html.haml +34 -0
  22. data/bin/rails +25 -0
  23. data/config/initializers/appends.rb +20 -0
  24. data/config/initializers/content.rb +3 -0
  25. data/config/initializers/fields.rb +13 -0
  26. data/config/locales/en.yml +8 -0
  27. data/config/routes.rb +7 -0
  28. data/lib/workarea/search_autocomplete.rb +11 -0
  29. data/lib/workarea/search_autocomplete/engine.rb +8 -0
  30. data/lib/workarea/search_autocomplete/version.rb +5 -0
  31. data/package.json +9 -0
  32. data/script/admin_ci +9 -0
  33. data/script/ci +11 -0
  34. data/script/core_ci +9 -0
  35. data/script/plugins_ci +9 -0
  36. data/script/storefront_ci +9 -0
  37. data/test/dummy/.ruby-version +1 -0
  38. data/test/dummy/Rakefile +6 -0
  39. data/test/dummy/app/assets/config/manifest.js +3 -0
  40. data/test/dummy/app/assets/images/.keep +0 -0
  41. data/test/dummy/app/assets/javascripts/application.js +14 -0
  42. data/test/dummy/app/assets/stylesheets/application.css +15 -0
  43. data/test/dummy/app/controllers/application_controller.rb +2 -0
  44. data/test/dummy/app/controllers/concerns/.keep +0 -0
  45. data/test/dummy/app/helpers/application_helper.rb +2 -0
  46. data/test/dummy/app/jobs/application_job.rb +2 -0
  47. data/test/dummy/app/mailers/application_mailer.rb +4 -0
  48. data/test/dummy/app/models/concerns/.keep +0 -0
  49. data/test/dummy/app/views/layouts/application.html.erb +15 -0
  50. data/test/dummy/app/views/layouts/mailer.html.erb +13 -0
  51. data/test/dummy/app/views/layouts/mailer.text.erb +1 -0
  52. data/test/dummy/bin/bundle +3 -0
  53. data/test/dummy/bin/rails +4 -0
  54. data/test/dummy/bin/rake +4 -0
  55. data/test/dummy/bin/setup +25 -0
  56. data/test/dummy/bin/update +25 -0
  57. data/test/dummy/config.ru +5 -0
  58. data/test/dummy/config/application.rb +34 -0
  59. data/test/dummy/config/boot.rb +5 -0
  60. data/test/dummy/config/environment.rb +5 -0
  61. data/test/dummy/config/environments/development.rb +52 -0
  62. data/test/dummy/config/environments/production.rb +83 -0
  63. data/test/dummy/config/environments/test.rb +45 -0
  64. data/test/dummy/config/initializers/application_controller_renderer.rb +8 -0
  65. data/test/dummy/config/initializers/assets.rb +12 -0
  66. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  67. data/test/dummy/config/initializers/content_security_policy.rb +25 -0
  68. data/test/dummy/config/initializers/cookies_serializer.rb +5 -0
  69. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  70. data/test/dummy/config/initializers/inflections.rb +16 -0
  71. data/test/dummy/config/initializers/mime_types.rb +4 -0
  72. data/test/dummy/config/initializers/workarea.rb +5 -0
  73. data/test/dummy/config/initializers/wrap_parameters.rb +9 -0
  74. data/test/dummy/config/locales/en.yml +33 -0
  75. data/test/dummy/config/puma.rb +34 -0
  76. data/test/dummy/config/routes.rb +5 -0
  77. data/test/dummy/config/spring.rb +6 -0
  78. data/test/dummy/db/seeds.rb +2 -0
  79. data/test/dummy/lib/assets/.keep +0 -0
  80. data/test/dummy/log/.keep +0 -0
  81. data/test/integration/workarea/storefront/search_autocomplete_integration_test.rb +29 -0
  82. data/test/models/workarea/metrics/search_by_week_autocomplete_test.rb +20 -0
  83. data/test/system/workarea/storefront/search_autocomplete_system_test.rb +171 -0
  84. data/test/teaspoon_env.rb +6 -0
  85. data/test/test_helper.rb +10 -0
  86. data/test/view_models/workarea/storefront/search_autocomplete_view_model_test.rb +125 -0
  87. data/workarea-search_autocomplete.gemspec +23 -0
  88. metadata +148 -0
@@ -0,0 +1,33 @@
1
+ # Files in the config/locales directory are used for internationalization
2
+ # and are automatically loaded by Rails. If you want to use locales other
3
+ # than English, add the necessary files in this directory.
4
+ #
5
+ # To use the locales, use `I18n.t`:
6
+ #
7
+ # I18n.t 'hello'
8
+ #
9
+ # In views, this is aliased to just `t`:
10
+ #
11
+ # <%= t('hello') %>
12
+ #
13
+ # To use a different locale, set it with `I18n.locale`:
14
+ #
15
+ # I18n.locale = :es
16
+ #
17
+ # This would use the information in config/locales/es.yml.
18
+ #
19
+ # The following keys must be escaped otherwise they will not be retrieved by
20
+ # the default I18n backend:
21
+ #
22
+ # true, false, on, off, yes, no
23
+ #
24
+ # Instead, surround them with single quotes.
25
+ #
26
+ # en:
27
+ # 'true': 'foo'
28
+ #
29
+ # To learn more, please read the Rails Internationalization guide
30
+ # available at http://guides.rubyonrails.org/i18n.html.
31
+
32
+ en:
33
+ hello: "Hello world"
@@ -0,0 +1,34 @@
1
+ # Puma can serve each request in a thread from an internal thread pool.
2
+ # The `threads` method setting takes two numbers: a minimum and maximum.
3
+ # Any libraries that use thread pools should be configured to match
4
+ # the maximum value specified for Puma. Default is set to 5 threads for minimum
5
+ # and maximum; this matches the default thread size of Active Record.
6
+ #
7
+ threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }
8
+ threads threads_count, threads_count
9
+
10
+ # Specifies the `port` that Puma will listen on to receive requests; default is 3000.
11
+ #
12
+ port ENV.fetch("PORT") { 3000 }
13
+
14
+ # Specifies the `environment` that Puma will run in.
15
+ #
16
+ environment ENV.fetch("RAILS_ENV") { "development" }
17
+
18
+ # Specifies the number of `workers` to boot in clustered mode.
19
+ # Workers are forked webserver processes. If using threads and workers together
20
+ # the concurrency of the application would be max `threads` * `workers`.
21
+ # Workers do not work on JRuby or Windows (both of which do not support
22
+ # processes).
23
+ #
24
+ # workers ENV.fetch("WEB_CONCURRENCY") { 2 }
25
+
26
+ # Use the `preload_app!` method when specifying a `workers` number.
27
+ # This directive tells Puma to first boot the application and load code
28
+ # before forking the application. This takes advantage of Copy On Write
29
+ # process behavior so workers use less memory.
30
+ #
31
+ # preload_app!
32
+
33
+ # Allow puma to be restarted by `rails restart` command.
34
+ plugin :tmp_restart
@@ -0,0 +1,5 @@
1
+ Rails.application.routes.draw do
2
+ mount Workarea::Core::Engine => '/'
3
+ mount Workarea::Admin::Engine => '/admin', as: 'admin'
4
+ mount Workarea::Storefront::Engine => '/', as: 'storefront'
5
+ end
@@ -0,0 +1,6 @@
1
+ %w[
2
+ .ruby-version
3
+ .rbenv-vars
4
+ tmp/restart.txt
5
+ tmp/caching-dev.txt
6
+ ].each { |path| Spring.watch(path) }
@@ -0,0 +1,2 @@
1
+ require 'workarea/seeds'
2
+ Workarea::Seeds.run
File without changes
File without changes
@@ -0,0 +1,29 @@
1
+ require 'test_helper'
2
+
3
+ module Workarea
4
+ module Storefront
5
+ class SearchAutocompleteIntegrationTest < Workarea::IntegrationTest
6
+ def test_autocomplete
7
+ test_one = create_product(name: 'Test One')
8
+ test_two = create_product(name: 'Test Two')
9
+ create_search_by_week(query_string: 'test one', searches: 5, total_results: 5)
10
+ create_search_by_week(query_string: 'test two', searches: 10, total_results: 5)
11
+
12
+ get storefront.autocomplete_search_path(q: '')
13
+ assert(response.ok?)
14
+
15
+ get storefront.autocomplete_search_path(q: 'tes')
16
+ assert(response.ok?)
17
+ assert_match(/test two.*test one/m, response.body)
18
+ assert_includes(response.body, storefront.product_path(test_two))
19
+ refute_includes(response.body, storefront.product_path(test_one))
20
+
21
+ get storefront.autocomplete_search_path(q: 'TeS')
22
+ assert(response.ok?)
23
+ assert_match(/test two.*test one/m, response.body)
24
+ assert_includes(response.body, storefront.product_path(test_two))
25
+ refute_includes(response.body, storefront.product_path(test_one))
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,20 @@
1
+ require 'test_helper'
2
+
3
+ module Workarea
4
+ module Metrics
5
+ class SearchByWeekAutocompleteTest < TestCase
6
+ def test_autocomplete
7
+ create_search_by_week(total_results: 0, query_string: 'test one')
8
+ create_search_by_week(total_results: 5, query_string: 'test two', reporting_on: 1.week.ago.beginning_of_week)
9
+ create_search_by_week(total_results: 5, query_string: 'test two', reporting_on: 2.weeks.ago.beginning_of_week)
10
+ create_search_by_week(searches: 3, total_results: 10, query_string: 'test three')
11
+ create_search_by_week(total_results: 15, query_string: 'blah blah blah')
12
+
13
+ results = SearchByWeek.autocomplete('tes')
14
+ assert_equal(2, results.size)
15
+ assert_equal('test three', results.first)
16
+ assert_equal('test two', results.second)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,171 @@
1
+ require 'test_helper'
2
+
3
+ module Workarea
4
+ module Storefront
5
+ class SearchAutocompleteSystemTest < Workarea::SystemTest
6
+ setup :generate_trending_products, :generate_trending_searches
7
+
8
+ def generate_trending_products
9
+ create_product(name: 'Foo Product', id: 'foo_product')
10
+ create_product(name: 'Bar Product', id: 'bar_product')
11
+ create_product(name: 'Baz Product', id: 'baz_product')
12
+
13
+ create_product_by_week(
14
+ product_id: 'foo_product',
15
+ revenue_change: 10,
16
+ orders: 5,
17
+ reporting_on: Time.zone.local(2018, 12, 3)
18
+ )
19
+ create_product_by_week(
20
+ product_id: 'foo_product',
21
+ revenue_change: -10,
22
+ orders: 0,
23
+ reporting_on: Time.zone.local(2018, 12, 10)
24
+ )
25
+ create_product_by_week(
26
+ product_id: 'foo_product',
27
+ revenue_change: 20,
28
+ orders: 10,
29
+ reporting_on: Time.zone.local(2018, 12, 17)
30
+ )
31
+ create_product_by_week(
32
+ product_id: 'bar_product',
33
+ revenue_change: 10,
34
+ orders: 5,
35
+ reporting_on: Time.zone.local(2018, 12, 3)
36
+ )
37
+ create_product_by_week(
38
+ product_id: 'bar_product',
39
+ revenue_change: -10,
40
+ orders: 0,
41
+ reporting_on: Time.zone.local(2018, 12, 10)
42
+ )
43
+ create_product_by_week(
44
+ product_id: 'bar_product',
45
+ revenue_change: 0,
46
+ orders: 0,
47
+ reporting_on: Time.zone.local(2018, 12, 17)
48
+ )
49
+ create_product_by_week(
50
+ product_id: 'baz_product',
51
+ revenue_change: 10,
52
+ orders: 5,
53
+ reporting_on: Time.zone.local(2018, 12, 3)
54
+ )
55
+ create_product_by_week(
56
+ product_id: 'baz_product',
57
+ revenue_change: -10,
58
+ orders: 1,
59
+ reporting_on: Time.zone.local(2018, 12, 10)
60
+ )
61
+ create_product_by_week(
62
+ product_id: 'baz_product',
63
+ revenue_change: 0,
64
+ orders: 0,
65
+ reporting_on: Time.zone.local(2018, 12, 17)
66
+ )
67
+
68
+ travel_to Time.zone.local(2019, 1, 17)
69
+ Workarea::Insights::TrendingProducts.generate_monthly!
70
+ end
71
+
72
+ def generate_trending_searches
73
+ create_search_by_week(
74
+ query_string: 'foo search',
75
+ revenue_change: 10,
76
+ orders: 5,
77
+ reporting_on: Time.zone.local(2018, 12, 3)
78
+ )
79
+ create_search_by_week(
80
+ query_string: 'foo search',
81
+ revenue_change: -10,
82
+ orders: 0,
83
+ reporting_on: Time.zone.local(2018, 12, 10)
84
+ )
85
+ create_search_by_week(
86
+ query_string: 'foo search',
87
+ revenue_change: 20,
88
+ orders: 10,
89
+ reporting_on: Time.zone.local(2018, 12, 17)
90
+ )
91
+ create_search_by_week(
92
+ query_string: 'bar search',
93
+ revenue_change: 10,
94
+ orders: 5,
95
+ reporting_on: Time.zone.local(2018, 12, 3)
96
+ )
97
+ create_search_by_week(
98
+ query_string: 'bar search',
99
+ revenue_change: -10,
100
+ orders: 0,
101
+ reporting_on: Time.zone.local(2018, 12, 10)
102
+ )
103
+ create_search_by_week(
104
+ query_string: 'bar search',
105
+ revenue_change: 0,
106
+ orders: 0,
107
+ reporting_on: Time.zone.local(2018, 12, 17)
108
+ )
109
+ create_search_by_week(
110
+ query_string: 'baz search',
111
+ revenue_change: 10,
112
+ orders: 5,
113
+ reporting_on: Time.zone.local(2018, 12, 3)
114
+ )
115
+ create_search_by_week(
116
+ query_string: 'baz search',
117
+ revenue_change: -10,
118
+ orders: 1,
119
+ reporting_on: Time.zone.local(2018, 12, 10)
120
+ )
121
+ create_search_by_week(
122
+ query_string: 'baz search',
123
+ revenue_change: 0,
124
+ orders: 0,
125
+ reporting_on: Time.zone.local(2018, 12, 17)
126
+ )
127
+
128
+ travel_to Time.zone.local(2019, 1, 17)
129
+ Workarea::Insights::TrendingSearches.generate_monthly!
130
+ end
131
+
132
+ def test_autocomplete
133
+ create_product(name: 'Test One')
134
+ create_product(name: 'Test Two')
135
+ create_search_by_week(query_string: 'test one', searches: 5, total_results: 5)
136
+ create_search_by_week(query_string: 'test two', searches: 10, total_results: 5)
137
+
138
+ visit storefront.root_path
139
+ fill_in 'q', with: ''
140
+ within '#search_autocomplete' do
141
+ assert_text(t('workarea.storefront.search_autocomplete.trending_products'))
142
+ assert_text(t('workarea.storefront.search_autocomplete.trending_searches'))
143
+ assert_match(/Foo Product.*Baz Product.*Bar Product/m, page.body)
144
+ assert_match(/foo search.*baz search.*bar search/m, page.body)
145
+ end
146
+
147
+ fill_in 'q', with: 'te'
148
+ within '#search_autocomplete' do
149
+ assert_text('Test Two')
150
+ assert_match(/test two.*test one/m, page.body)
151
+ end
152
+
153
+ fill_in 'q', with: ''
154
+ within '#search_autocomplete' do
155
+ assert_text(t('workarea.storefront.search_autocomplete.trending_products'))
156
+ assert_text(t('workarea.storefront.search_autocomplete.trending_searches'))
157
+ assert_match(/Foo Product.*Baz Product.*Bar Product/m, page.body)
158
+ assert_match(/foo search.*baz search.*bar search/m, page.body)
159
+ end
160
+
161
+ fill_in 'q', with: 'corge'
162
+ within '#search_autocomplete' do
163
+ assert_text(t('workarea.storefront.search_autocomplete.trending_products'))
164
+ assert_text(t('workarea.storefront.search_autocomplete.trending_searches'))
165
+ assert_match(/Foo Product.*Baz Product.*Bar Product/m, page.body)
166
+ assert_match(/foo search.*baz search.*bar search/m, page.body)
167
+ end
168
+ end
169
+ end
170
+ end
171
+ end
@@ -0,0 +1,6 @@
1
+ require 'workarea/testing/teaspoon'
2
+
3
+ Teaspoon.configure do |config|
4
+ config.root = Workarea::SearchAutocomplete::Engine.root
5
+ Workarea::Teaspoon.apply(config)
6
+ end
@@ -0,0 +1,10 @@
1
+ # Configure Rails Environment
2
+ ENV['RAILS_ENV'] = 'test'
3
+
4
+ require File.expand_path("../../test/dummy/config/environment.rb", __FILE__)
5
+ require 'rails/test_help'
6
+ require 'workarea/test_help'
7
+
8
+ # Filter out Minitest backtrace while allowing backtrace from other libraries
9
+ # to be shown.
10
+ Minitest.backtrace_filter = Minitest::BacktraceFilter.new
@@ -0,0 +1,125 @@
1
+ require 'test_helper'
2
+
3
+ module Workarea
4
+ module Storefront
5
+ class SearchAutocompleteViewModelTest < TestCase
6
+ include SearchIndexing
7
+
8
+ def test_trending_products
9
+ assert(SearchAutocompleteViewModel.new.trending_products?)
10
+ assert(SearchAutocompleteViewModel.new('foo').trending_products?)
11
+
12
+ IndexProduct.perform(create_product(name: 'foo'))
13
+ refute(SearchAutocompleteViewModel.new('foo').trending_products?)
14
+
15
+ create_search_by_week(query_string: 'foo', searches: 5, total_results: 5)
16
+ refute(SearchAutocompleteViewModel.new('fo').trending_products?)
17
+ end
18
+
19
+ def test_products
20
+ foo = create_product(name: 'Foo', id: 'foo')
21
+ bar = create_product(name: 'Bar', id: 'bar')
22
+ BulkIndexProducts.perform_by_models([foo, bar])
23
+
24
+ create_product_by_week(
25
+ product_id: 'foo',
26
+ revenue_change: 10,
27
+ orders: 5,
28
+ reporting_on: Time.zone.local(2018, 12, 3)
29
+ )
30
+ create_product_by_week(
31
+ product_id: 'foo',
32
+ revenue_change: 20,
33
+ orders: 10,
34
+ reporting_on: Time.zone.local(2018, 12, 17)
35
+ )
36
+ create_product_by_week(
37
+ product_id: 'bar',
38
+ revenue_change: 10,
39
+ orders: 5,
40
+ reporting_on: Time.zone.local(2018, 12, 3)
41
+ )
42
+
43
+ travel_to Time.zone.local(2019, 1, 17)
44
+ Insights::TrendingProducts.generate_monthly!
45
+
46
+ assert_equal(%w(foo bar), SearchAutocompleteViewModel.new.products.map(&:id))
47
+ assert_equal(%w(foo bar), SearchAutocompleteViewModel.new('baz').products.map(&:id))
48
+ assert_equal(%w(foo), SearchAutocompleteViewModel.new('foo').products.map(&:id))
49
+
50
+ (Workarea.config.storefront_search_autocomplete_max_products + 1).times do |i|
51
+ IndexProduct.perform(create_product(name: "Foo #{i}"))
52
+ end
53
+
54
+ assert_equal(
55
+ Workarea.config.storefront_search_autocomplete_max_products,
56
+ SearchAutocompleteViewModel.new('foo').products.size
57
+ )
58
+ end
59
+
60
+ def test_searches
61
+ create_search_by_week(
62
+ query_string: 'foo',
63
+ revenue_change: 10,
64
+ orders: 5,
65
+ total_results: 5,
66
+ reporting_on: Time.zone.local(2018, 12, 3)
67
+ )
68
+ create_search_by_week(
69
+ query_string: 'foo',
70
+ revenue_change: 20,
71
+ orders: 10,
72
+ total_results: 5,
73
+ reporting_on: Time.zone.local(2018, 12, 17)
74
+ )
75
+ create_search_by_week(
76
+ query_string: 'bar',
77
+ revenue_change: 10,
78
+ orders: 5,
79
+ total_results: 5,
80
+ reporting_on: Time.zone.local(2018, 12, 3)
81
+ )
82
+
83
+ travel_to Time.zone.local(2019, 1, 17)
84
+ Workarea::Insights::TrendingSearches.generate_monthly!
85
+
86
+ assert_equal(%w(foo bar), SearchAutocompleteViewModel.new.searches)
87
+ assert_equal(%w(foo bar), SearchAutocompleteViewModel.new('baz').searches)
88
+
89
+ foo = create_product(name: 'Foo')
90
+ bar = create_product(name: 'Bar')
91
+ BulkIndexProducts.perform_by_models([foo, bar])
92
+
93
+ assert_equal(%w(foo), SearchAutocompleteViewModel.new('foo').searches)
94
+
95
+ (Workarea.config.storefront_search_autocomplete_max_searches + 1).times do |i|
96
+ create_search_by_week(
97
+ query_string: "foo #{i}",
98
+ revenue_change: 10,
99
+ orders: 5,
100
+ total_results: 5,
101
+ reporting_on: Time.zone.local(2018, 12, 3)
102
+ )
103
+ end
104
+
105
+ travel_to Time.zone.local(2019, 1, 17)
106
+ Workarea::Insights::TrendingSearches.generate_monthly!
107
+
108
+ assert_equal(
109
+ Workarea.config.storefront_search_autocomplete_max_searches,
110
+ SearchAutocompleteViewModel.new('foo').searches.size
111
+ )
112
+ end
113
+
114
+ def test_query_string
115
+ assert_nil(SearchAutocompleteViewModel.new.query_string)
116
+ assert_equal('foo', SearchAutocompleteViewModel.new('foo').query_string)
117
+
118
+ create_search_by_week(query_string: 'foo', searches: 10, total_results: 5)
119
+ create_search_by_week(query_string: 'foo bar', searches: 5, total_results: 5)
120
+
121
+ assert_equal('foo', SearchAutocompleteViewModel.new('foo').query_string)
122
+ end
123
+ end
124
+ end
125
+ end