mongoid_search 0.3.4 → 0.3.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,6 @@
1
1
  namespace :mongoid_search do
2
2
  desc 'Goes through all documents with search enabled and indexes the keywords.'
3
- task :index => :environment do
3
+ task index: :environment do
4
4
  ::Rails.application.eager_load!
5
5
  if Mongoid::Search.classes.blank?
6
6
  Mongoid::Search::Log.log "No model to index keywords.\n"
@@ -1,28 +1,28 @@
1
- # encoding: utf-8
1
+
2
2
  module Mongoid::Search::Util
3
3
  def self.keywords(klass, fields)
4
4
  if fields.is_a?(Array)
5
5
  fields.map do |field|
6
- self.keywords(klass, field)
6
+ keywords(klass, field)
7
7
  end
8
8
  elsif fields.is_a?(Hash)
9
9
  fields.keys.map do |field|
10
10
  attribute = klass.send(field)
11
11
  unless attribute.blank?
12
12
  if attribute.is_a?(Array)
13
- attribute.map{ |a| self.keywords(a, fields[field]) }
13
+ attribute.map { |a| keywords(a, fields[field]) }
14
14
  else
15
- self.keywords(attribute, fields[field])
15
+ keywords(attribute, fields[field])
16
16
  end
17
17
  end
18
18
  end
19
19
  else
20
- value = if klass.respond_to?(fields.to_s + "_translations")
21
- klass.send(fields.to_s + "_translations").values
20
+ value = if klass.respond_to?(fields.to_s + '_translations')
21
+ klass.send(fields.to_s + '_translations').values
22
22
  elsif klass.respond_to?(fields)
23
23
  klass.send(fields)
24
24
  else
25
- value = klass[fields];
25
+ value = klass[fields]
26
26
  end
27
27
  value = value.join(' ') if value.respond_to?(:join)
28
28
  normalize_keywords(value) if value
@@ -38,19 +38,18 @@ module Mongoid::Search::Util
38
38
  strip_accents = Mongoid::Search.strip_accents
39
39
 
40
40
  return [] if text.blank?
41
- text = text.to_s.
42
- mb_chars.
43
- normalize(:kd).
44
- downcase.
45
- to_s.
46
- gsub(strip_symbols, ' '). # strip symbols
47
- gsub(strip_accents, ''). # strip accents
48
- gsub(/[#{ligatures.keys.join("")}]/) {|c| ligatures[c]}.
49
- split(' ').
50
- reject { |word| word.size < Mongoid::Search.minimum_word_size }
41
+ text = text.to_s
42
+ .mb_chars
43
+ .normalize(:kd)
44
+ .downcase
45
+ .to_s
46
+ .gsub(strip_symbols, ' ') # strip symbols
47
+ .gsub(strip_accents, '') # strip accents
48
+ .gsub(/[#{ligatures.keys.join("")}]/) { |c| ligatures[c] }
49
+ .split(' ')
50
+ .reject { |word| word.size < Mongoid::Search.minimum_word_size }
51
51
  text = text.reject { |word| ignore_list.include?(word) } unless ignore_list.blank?
52
52
  text = text.map(&stem_proc) if stem_keywords
53
53
  text
54
54
  end
55
-
56
55
  end
@@ -5,13 +5,17 @@ class Product
5
5
 
6
6
  field :brand
7
7
  field :name
8
- field :attrs, :type => Array
9
- field :info, :type => Hash
8
+ field :attrs, type: Array
9
+ field :info, type: Hash
10
10
 
11
- has_many :tags
12
- belongs_to :category
11
+ has_many :tags
12
+ if Mongoid::Compatibility::Version.mongoid6_or_newer?
13
+ belongs_to :category, required: false
14
+ else
15
+ belongs_to :category
16
+ end
13
17
  embeds_many :subproducts
14
18
 
15
- search_in :brand, :name, :outlet, :attrs, :tags => :name, :category => [:name, :description],
16
- :subproducts => [:brand, :name], :info => [ :summary, :description ]
19
+ search_in :brand, :name, :outlet, :attrs, tags: :name, category: %i[name description],
20
+ subproducts: %i[brand name], info: %i[summary description]
17
21
  end
@@ -5,5 +5,5 @@ class Subproduct
5
5
  field :brand
6
6
  field :name
7
7
 
8
- embedded_in :product, :inverse_of => :subproducts
8
+ embedded_in :product, inverse_of: :subproducts
9
9
  end
@@ -8,8 +8,8 @@ class Tag
8
8
  belongs_to :product
9
9
 
10
10
  def title
11
- self.name
11
+ name
12
12
  end
13
13
 
14
- search_in :title, :product => [:name, { :info => [ :summary, :description ], :category => [:name, :description]}]
14
+ search_in :title, product: [:name, { info: %i[summary description], category: %i[name description] }]
15
15
  end
@@ -1,4 +1,4 @@
1
- autoload :Product, "models/product.rb"
1
+ autoload :Product, 'models/product.rb'
2
2
  class Variant < Product
3
3
  field :color
4
4
  search_in :color
@@ -1,11 +1,10 @@
1
- # encoding: utf-8
2
-
3
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
4
2
 
5
3
  describe Mongoid::Search do
6
-
7
4
  before(:all) do
8
5
  @default_proc = Mongoid::Search.stem_proc
6
+ @default_strip_symbols = Mongoid::Search.strip_symbols
7
+ @default_strip_accents = Mongoid::Search.strip_accents
9
8
  end
10
9
 
11
10
  after(:all) do
@@ -17,193 +16,192 @@ describe Mongoid::Search do
17
16
  Mongoid::Search.stem_keywords = false
18
17
  Mongoid::Search.ignore_list = nil
19
18
  Mongoid::Search.stem_proc = @default_proc
20
- @product = Product.create :brand => "Apple",
21
- :name => "iPhone",
22
- :tags => (@tags = ["Amazing", "Awesome", "Olé"].map { |tag| Tag.new(:name => tag) }),
23
- :category => Category.new(:name => "Mobile", :description => "Reviews"),
24
- :subproducts => [Subproduct.new(:brand => "Apple", :name => "Craddle")],
25
- :info => { :summary => "Info-summary",
26
- :description => "Info-description"}
27
- end
28
-
29
- describe "Serialized hash fields" do
30
- context "when the hash is populated" do
31
- it "should return the product" do
32
- expect(Product.full_text_search("Info-summary").first).to eq @product
33
- expect(Product.full_text_search("Info-description").first).to eq @product
19
+ @product = Product.create brand: 'Apple',
20
+ name: 'iPhone',
21
+ tags: (@tags = %w[Amazing Awesome Olé].map { |tag| Tag.new(name: tag) }),
22
+ category: Category.new(name: 'Mobile', description: 'Reviews'),
23
+ subproducts: [Subproduct.new(brand: 'Apple', name: 'Craddle')],
24
+ info: { summary: 'Info-summary',
25
+ description: 'Info-description' }
26
+ end
27
+
28
+ describe 'Serialized hash fields' do
29
+ context 'when the hash is populated' do
30
+ it 'should return the product' do
31
+ expect(Product.full_text_search('Info-summary').first).to eq @product
32
+ expect(Product.full_text_search('Info-description').first).to eq @product
34
33
  end
35
34
  end
36
35
 
37
- context "when the hash is empty" do
36
+ context 'when the hash is empty' do
38
37
  before(:each) do
39
38
  @product.info = nil
40
39
  @product.save
41
40
  end
42
41
 
43
- it "should not return the product" do
44
- expect(Product.full_text_search("Info-description").size).to eq 0
45
- expect(Product.full_text_search("Info-summary").size).to eq 0
42
+ it 'should not return the product' do
43
+ expect(Product.full_text_search('Info-description').size).to eq 0
44
+ expect(Product.full_text_search('Info-summary').size).to eq 0
46
45
  end
47
46
  end
48
47
  end
49
48
 
50
- context "utf-8 characters" do
49
+ context 'utf-8 characters' do
51
50
  before do
52
51
  Mongoid::Search.stem_keywords = false
53
52
  Mongoid::Search.ignore_list = nil
54
- @product = Product.create :brand => "Эльбрус",
55
- :name => "Процессор",
56
- :tags => ["Amazing", "Awesome", "Olé"].map { |tag| Tag.new(:name => tag) },
57
- :category => Category.new(:name => "процессоры"),
58
- :subproducts => []
53
+ @product = Product.create brand: 'Эльбрус',
54
+ name: 'Процессор',
55
+ tags: %w[Amazing Awesome Olé].map { |tag| Tag.new(name: tag) },
56
+ category: Category.new(name: 'процессоры'),
57
+ subproducts: []
59
58
  end
60
59
 
61
- it "should leave utf8 characters" do
62
- expect(@product._keywords).to eq ["amazing", "awesome", "ole", "процессор", "процессоры", "эльбрус"]
60
+ it 'should leave utf8 characters' do
61
+ expect(@product._keywords).to eq %w[amazing awesome ole процессор процессоры эльбрус]
63
62
  end
64
63
 
65
64
  it "should return results in search when case doesn't match" do
66
- expect(Product.full_text_search("ЭЛЬБРУС").size).to eq 1
65
+ expect(Product.full_text_search('ЭЛЬБРУС').size).to eq 1
67
66
  end
68
67
  end
69
68
 
70
- context "when references are nil" do
71
- context "when instance is being created" do
72
- it "should not complain about method missing" do
69
+ context 'when references are nil' do
70
+ context 'when instance is being created' do
71
+ it 'should not complain about method missing' do
73
72
  expect { Product.create! }.not_to raise_error
74
73
  end
75
74
  end
76
75
 
77
76
  it 'should validate keywords' do
78
- product = Product.create :brand => "Apple", :name => "iPhone"
79
- expect(product._keywords).to eq(["apple", "iphone"])
77
+ product = Product.create brand: 'Apple', name: 'iPhone'
78
+ expect(product._keywords).to eq(%w[apple iphone])
80
79
  end
81
80
  end
82
81
 
83
-
84
- it "should set the _keywords field for array fields also" do
82
+ it 'should set the _keywords field for array fields also' do
85
83
  @product.attrs = ['lightweight', 'plastic', :red]
86
84
  @product.save!
87
85
  expect(@product._keywords).to include 'lightweight', 'plastic', 'red'
88
86
  end
89
87
 
90
- it "should inherit _keywords field and build upon" do
91
- variant = Variant.create :brand => "Apple",
92
- :name => "iPhone",
93
- :tags => ["Amazing", "Awesome", "Olé"].map { |tag| Tag.new(:name => tag) },
94
- :category => Category.new(:name => "Mobile"),
95
- :subproducts => [Subproduct.new(:brand => "Apple", :name => "Craddle")],
96
- :color => :white
88
+ it 'should inherit _keywords field and build upon' do
89
+ variant = Variant.create brand: 'Apple',
90
+ name: 'iPhone',
91
+ tags: %w[Amazing Awesome Olé].map { |tag| Tag.new(name: tag) },
92
+ category: Category.new(name: 'Mobile'),
93
+ subproducts: [Subproduct.new(brand: 'Apple', name: 'Craddle')],
94
+ color: :white
97
95
  expect(variant._keywords).to include 'white'
98
- expect(Variant.full_text_search(:name => 'Apple', :color => :white)).to eq [variant]
96
+ expect(Variant.full_text_search(name: 'Apple', color: :white)).to eq [variant]
99
97
  end
100
98
 
101
- it "should expand the ligature to ease searching" do
99
+ it 'should expand the ligature to ease searching' do
102
100
  # ref: http://en.wikipedia.org/wiki/Typographic_ligature, only for french right now. Rules for other languages are not know
103
- variant1 = Variant.create :tags => ["œuvre"].map {|tag| Tag.new(:name => tag)}
104
- variant2 = Variant.create :tags => ["æquo"].map {|tag| Tag.new(:name => tag)}
101
+ variant1 = Variant.create tags: ['œuvre'].map { |tag| Tag.new(name: tag) }
102
+ variant2 = Variant.create tags: ['æquo'].map { |tag| Tag.new(name: tag) }
105
103
 
106
- expect(Variant.full_text_search("œuvre")).to eq [variant1]
107
- expect(Variant.full_text_search("oeuvre")).to eq [variant1]
108
- expect(Variant.full_text_search("æquo")).to eq [variant2]
109
- expect(Variant.full_text_search("aequo")).to eq [variant2]
104
+ expect(Variant.full_text_search('œuvre')).to eq [variant1]
105
+ expect(Variant.full_text_search('oeuvre')).to eq [variant1]
106
+ expect(Variant.full_text_search('æquo')).to eq [variant2]
107
+ expect(Variant.full_text_search('aequo')).to eq [variant2]
110
108
  end
111
109
 
112
- it "should set the _keywords field with stemmed words if stem is enabled" do
110
+ it 'should set the _keywords field with stemmed words if stem is enabled' do
113
111
  Mongoid::Search.stem_keywords = true
114
112
  @product.save!
115
- expect(@product._keywords.sort).to eq ["amaz", "appl", "awesom", "craddl", "iphon", "mobil", "review", "ol", "info", "descript", "summari"].sort
113
+ expect(@product._keywords.sort).to eq %w[amaz appl awesom craddl iphon mobil review ol info descript summari].sort
116
114
  end
117
115
 
118
- it "should set the _keywords field with custom stemmed words if stem is enabled with a custom lambda" do
116
+ it 'should set the _keywords field with custom stemmed words if stem is enabled with a custom lambda' do
119
117
  Mongoid::Search.stem_keywords = true
120
- Mongoid::Search.stem_proc = Proc.new { |word| word.upcase }
118
+ Mongoid::Search.stem_proc = proc { |word| word.upcase }
121
119
  @product.save!
122
- expect(@product._keywords.sort).to eq ["AMAZING", "APPLE", "AWESOME", "CRADDLE", "DESCRIPTION", "INFO", "IPHONE", "MOBILE", "OLE", "REVIEWS", "SUMMARY"]
120
+ expect(@product._keywords.sort).to eq %w[AMAZING APPLE AWESOME CRADDLE DESCRIPTION INFO IPHONE MOBILE OLE REVIEWS SUMMARY]
123
121
  end
124
122
 
125
- it "should ignore keywords in an ignore list" do
126
- Mongoid::Search.ignore_list = YAML.load(File.open(File.dirname(__FILE__) + '/config/ignorelist.yml'))["ignorelist"]
123
+ it 'should ignore keywords in an ignore list' do
124
+ Mongoid::Search.ignore_list = YAML.safe_load(File.open(File.dirname(__FILE__) + '/config/ignorelist.yml'))['ignorelist']
127
125
  @product.save!
128
- expect(@product._keywords.sort).to eq ["apple", "craddle", "iphone", "mobile", "reviews", "ole", "info", "description", "summary"].sort
126
+ expect(@product._keywords.sort).to eq %w[apple craddle iphone mobile reviews ole info description summary].sort
129
127
  end
130
128
 
131
- it "should incorporate numbers as keywords" do
132
- @product = Product.create :brand => "Ford",
133
- :name => "T 1908",
134
- :tags => ["Amazing", "First", "Car"].map { |tag| Tag.new(:name => tag) },
135
- :category => Category.new(:name => "Vehicle")
129
+ it 'should incorporate numbers as keywords' do
130
+ @product = Product.create brand: 'Ford',
131
+ name: 'T 1908',
132
+ tags: %w[Amazing First Car].map { |tag| Tag.new(name: tag) },
133
+ category: Category.new(name: 'Vehicle')
136
134
 
137
135
  @product.save!
138
- expect(@product._keywords).to eq ["1908", "amazing", "car", "first", "ford", "vehicle"]
136
+ expect(@product._keywords).to eq %w[1908 amazing car first ford vehicle]
139
137
  end
140
138
 
141
- it "should return results in search" do
142
- expect(Product.full_text_search("apple").size).to eq 1
139
+ it 'should return results in search' do
140
+ expect(Product.full_text_search('apple').size).to eq 1
143
141
  end
144
142
 
145
- it "should return results in search for dynamic attribute" do
146
- @product[:outlet] = "online shop"
143
+ it 'should return results in search for dynamic attribute' do
144
+ @product[:outlet] = 'online shop'
147
145
  @product.save!
148
- expect(Product.full_text_search("online").size).to eq 1
146
+ expect(Product.full_text_search('online').size).to eq 1
149
147
  end
150
148
 
151
- it "should return results in search even searching a accented word" do
152
- expect(Product.full_text_search("Ole").size).to eq 1
153
- expect(Product.full_text_search("Olé").size).to eq 1
149
+ it 'should return results in search even searching a accented word' do
150
+ expect(Product.full_text_search('Ole').size).to eq 1
151
+ expect(Product.full_text_search('Olé').size).to eq 1
154
152
  end
155
153
 
156
154
  it "should return results in search even if the case doesn't match" do
157
- expect(Product.full_text_search("oLe").size).to eq 1
155
+ expect(Product.full_text_search('oLe').size).to eq 1
158
156
  end
159
157
 
160
- it "should return results in search with a partial word by default" do
161
- expect(Product.full_text_search("iph").size).to eq 1
158
+ it 'should return results in search with a partial word by default' do
159
+ expect(Product.full_text_search('iph').size).to eq 1
162
160
  end
163
161
 
164
- it "should return results for any matching word with default search" do
165
- expect(Product.full_text_search("apple motorola").size).to eq 1
162
+ it 'should return results for any matching word with default search' do
163
+ expect(Product.full_text_search('apple motorola').size).to eq 1
166
164
  end
167
165
 
168
- it "should not return results when all words do not match, if using :match => :all" do
166
+ it 'should not return results when all words do not match, if using :match => :all' do
169
167
  Mongoid::Search.match = :all
170
- expect(Product.full_text_search("apple motorola").size).to eq 0
168
+ expect(Product.full_text_search('apple motorola').size).to eq 0
171
169
  end
172
170
 
173
- it "should return results for any matching word, using :match => :all, passing :match => :any to .full_text_search" do
171
+ it 'should return results for any matching word, using :match => :all, passing :match => :any to .full_text_search' do
174
172
  Mongoid::Search.match = :all
175
- expect(Product.full_text_search("apple motorola", :match => :any).size).to eq 1
173
+ expect(Product.full_text_search('apple motorola', match: :any).size).to eq 1
176
174
  end
177
175
 
178
- it "should not return results when all words do not match, passing :match => :all to .full_text_search" do
179
- expect(Product.full_text_search("apple motorola", :match => :all).size).to eq 0
176
+ it 'should not return results when all words do not match, passing :match => :all to .full_text_search' do
177
+ expect(Product.full_text_search('apple motorola', match: :all).size).to eq 0
180
178
  end
181
179
 
182
- it "should return no results when a blank search is made" do
180
+ it 'should return no results when a blank search is made' do
183
181
  Mongoid::Search.allow_empty_search = false
184
- expect(Product.full_text_search("").size).to eq 0
182
+ expect(Product.full_text_search('').size).to eq 0
185
183
  end
186
184
 
187
- it "should return results when a blank search is made when :allow_empty_search is true" do
185
+ it 'should return results when a blank search is made when :allow_empty_search is true' do
188
186
  Mongoid::Search.allow_empty_search = true
189
- expect(Product.full_text_search("").size).to eq 1
187
+ expect(Product.full_text_search('').size).to eq 1
190
188
  end
191
189
 
192
- it "should search for embedded documents" do
193
- expect(Product.full_text_search("craddle").size).to eq 1
190
+ it 'should search for embedded documents' do
191
+ expect(Product.full_text_search('craddle').size).to eq 1
194
192
  end
195
193
 
196
- it "should search for reference documents" do
197
- expect(Product.full_text_search("reviews").size).to eq 1
194
+ it 'should search for reference documents' do
195
+ expect(Product.full_text_search('reviews').size).to eq 1
198
196
  end
199
197
 
200
198
  it 'should work in a chainable fashion' do
201
- expect(@product.category.products.where(:brand => 'Apple').full_text_search('apple').size).to eq 1
202
- expect(@product.category.products.full_text_search('craddle').where(:brand => 'Apple').size).to eq 1
199
+ expect(@product.category.products.where(brand: 'Apple').full_text_search('apple').size).to eq 1
200
+ expect(@product.category.products.full_text_search('craddle').where(brand: 'Apple').size).to eq 1
203
201
  end
204
202
 
205
203
  it 'should return the classes that include the search module' do
206
- expect(Mongoid::Search.classes).to eq [Product, Tag]
204
+ expect(Mongoid::Search.classes.sort_by(&:name)).to eq [Product, Tag]
207
205
  end
208
206
 
209
207
  it 'should have a method to index keywords' do
@@ -214,22 +212,21 @@ describe Mongoid::Search do
214
212
  expect(Product.index_keywords!).not_to include(false)
215
213
  end
216
214
 
217
- context "when regex search is false" do
215
+ context 'when regex search is false' do
218
216
  before do
219
217
  Mongoid::Search.regex_search = false
220
218
  end
221
219
 
222
- it "should not return results in search with a partial word if not using regex search" do
223
- expect(Product.full_text_search("iph").size).to eq 0
220
+ it 'should not return results in search with a partial word if not using regex search' do
221
+ expect(Product.full_text_search('iph').size).to eq 0
224
222
  end
225
223
 
226
- it "should return results in search with a full word if not using regex search" do
227
- expect(Product.full_text_search("iphone").size).to eq 1
224
+ it 'should return results in search with a full word if not using regex search' do
225
+ expect(Product.full_text_search('iphone').size).to eq 1
228
226
  end
229
227
  end
230
228
 
231
- context "when regex search is true" do
232
-
229
+ context 'when regex search is true' do
233
230
  before do
234
231
  Mongoid::Search.regex_search = true
235
232
  end
@@ -238,90 +235,104 @@ describe Mongoid::Search do
238
235
  Mongoid::Search.regex_search = false
239
236
  end
240
237
 
241
- it "should not return results in search with a partial word if using regex search" do
242
- expect(Product.full_text_search("iph").size).to eq 1
238
+ it 'should not return results in search with a partial word if using regex search' do
239
+ expect(Product.full_text_search('iph').size).to eq 1
243
240
  end
244
241
 
245
- it "should return results in search with a full word if using regex search" do
246
- expect(Product.full_text_search("iphone").size).to eq 1
242
+ it 'should return results in search with a full word if using regex search' do
243
+ expect(Product.full_text_search('iphone').size).to eq 1
244
+ end
245
+
246
+ context 'when query include special characters that should not be stripped' do
247
+ before do
248
+ Mongoid::Search.strip_symbols = /[\n]/
249
+ Mongoid::Search.strip_accents = /[^\s\p{Graph}]/
250
+ end
251
+
252
+ after do
253
+ Mongoid::Search.strip_symbols = @default_strip_symbols
254
+ Mongoid::Search.strip_accents = @default_strip_accents
255
+ end
256
+
257
+ it 'should escape regex special characters' do
258
+ expect(Product.full_text_search('iphone (steve').size).to eq 1
259
+ end
247
260
  end
248
261
 
249
262
  context 'Match partial words on the beginning' do
250
-
251
263
  before do
252
- Mongoid::Search.regex = Proc.new { |query| /^#{query}/ }
264
+ Mongoid::Search.regex = proc { |query| /^#{query}/ }
253
265
  end
254
266
 
255
- it "should return results in search which starts with query string" do
256
- expect(Product.full_text_search("iphone").size).to eq 1
267
+ it 'should return results in search which starts with query string' do
268
+ expect(Product.full_text_search('iphone').size).to eq 1
257
269
  end
258
270
 
259
- it "should not return results in search which does not start with query string" do
260
- expect(Product.full_text_search("phone").size).to eq 0
271
+ it 'should not return results in search which does not start with query string' do
272
+ expect(Product.full_text_search('phone').size).to eq 0
261
273
  end
262
274
  end
263
275
 
264
276
  context 'Match partial words on the end' do
265
-
266
277
  before do
267
- Mongoid::Search.regex = Proc.new { |query| /#{query}$/ }
278
+ Mongoid::Search.regex = proc { |query| /#{query}$/ }
268
279
  end
269
-
270
- it "should return results in search which ends with query string" do
271
- expect(Product.full_text_search("phone").size).to eq 1
280
+
281
+ it 'should return results in search which ends with query string' do
282
+ expect(Product.full_text_search('phone').size).to eq 1
272
283
  end
273
284
 
274
- it "should not return results in search which does not end with query string" do
275
- expect(Product.full_text_search("phon").size).to eq 0
285
+ it 'should not return results in search which does not end with query string' do
286
+ expect(Product.full_text_search('phon').size).to eq 0
276
287
  end
277
288
  end
278
289
  end
279
290
 
280
- context "relevant search" do
291
+ context 'relevant search' do
281
292
  before do
282
293
  Mongoid::Search.relevant_search = true
283
- @imac = Product.create :name => 'apple imac'
294
+ @imac = Product.create name: 'apple imac'
284
295
  end
285
296
 
286
- it "should return results ordered by relevance and with correct ids" do
287
- expect(Product.full_text_search('apple imac').map(&:_id)).to eq [@imac._id, @product._id]
297
+ it 'should return results ordered by relevance and with correct ids' do
298
+ expect(Product.full_text_search('apple imac').map(&:_id)).to eq [@imac._id, @product._id]
288
299
  end
289
300
 
290
- it "results should be recognized as persisted objects" do
291
- expect(Product.full_text_search('apple imac').map(&:persisted?)).not_to include false
301
+ it 'results should be recognized as persisted objects' do
302
+ expect(Product.full_text_search('apple imac').map(&:persisted?)).not_to include false
292
303
  end
293
304
 
294
- it "should include relevance information" do
295
- expect(Product.full_text_search('apple imac').map(&:relevance)).to eq [2, 1]
305
+ it 'should include relevance information' do
306
+ expect(Product.full_text_search('apple imac').map(&:relevance)).to eq [2, 1]
296
307
  end
297
308
  end
298
309
 
299
- context "when using methods for keywords" do
300
- it "should set the _keywords from methods" do
301
- expect(@tags.first._keywords).to include "amazing"
310
+ context 'when using methods for keywords' do
311
+ it 'should set the _keywords from methods' do
312
+ expect(@tags.first._keywords).to include 'amazing'
302
313
  end
303
314
  end
304
315
 
305
- context "when using deeply nested fields for keywords" do
306
- context "when explicitly calling set_keywords" do
307
- it "should set the _keywords from parent" do
316
+ context 'when using deeply nested fields for keywords' do
317
+ context 'when explicitly calling set_keywords' do
318
+ it 'should set the _keywords from parent' do
308
319
  @tags.first.send(:set_keywords)
309
- expect(@tags.first._keywords).to eq ["amazing", "description", "info", "iphone", "mobile", "reviews", "summary"]
320
+ expect(@tags.first._keywords).to eq %w[amazing description info iphone mobile reviews summary]
310
321
  end
311
322
  end
312
323
  end
313
324
 
314
- context "when using localized fields" do
315
- it "should set the keywords from all localizations" do
316
- @product = Product.create :brand => "Ford",
317
- :name => "T 1908",
318
- :tags => ["Amazing", "First", "Car"].map { |tag| Tag.new(:name => tag) },
319
- :category => Category.new(:name_translations => { :en => "Vehicle", :de => "Fahrzeug" })
320
- expect(@product._keywords).to include("fahrzeug")
325
+ context 'when using localized fields' do
326
+ it 'should set the keywords from all localizations' do
327
+ @product = Product.create brand: 'Ford',
328
+ name: 'T 1908',
329
+ tags: %w[Amazing First Car].map { |tag| Tag.new(name: tag) },
330
+ category: Category.new(name_translations: { en: 'Vehicle', de: 'Fahrzeug' })
331
+ expect(@product._keywords).to include('fahrzeug')
321
332
  end
322
333
  end
323
334
 
324
- context "minimum word size" do
335
+ context 'minimum word size' do
325
336
  before(:each) do
326
337
  Mongoid::Search.minimum_word_size = 3
327
338
  end
@@ -330,14 +341,14 @@ describe Mongoid::Search do
330
341
  Mongoid::Search.minimum_word_size = 2
331
342
  end
332
343
 
333
- it "should ignore keywords with length less than minimum word size" do
334
- @product = Product.create :name => 'a'
344
+ it 'should ignore keywords with length less than minimum word size' do
345
+ @product = Product.create name: 'a'
335
346
  expect(@product._keywords.size).to eq 0
336
347
 
337
- @product = Product.create :name => 'ap'
348
+ @product = Product.create name: 'ap'
338
349
  expect(@product._keywords.size).to eq 0
339
350
 
340
- @product = Product.create :name => 'app'
351
+ @product = Product.create name: 'app'
341
352
  expect(@product._keywords.size).to eq 1
342
353
  end
343
354
  end