activesearch 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -35,7 +35,7 @@ You won't get original documents though.
35
35
  end
36
36
 
37
37
  # Access the stored fields so you don't need to fetch the real document
38
- SomeModel.search("some words").first.stored["title"]
38
+ ActiveSearch.search("some words").first.stored["title"]
39
39
 
40
40
 
41
41
  ## Contributing
@@ -13,16 +13,24 @@ module ActiveSearch
13
13
 
14
14
  def store_fields(original, fields, options)
15
15
  if options && options[:store]
16
- (fields & options[:store]).each do |f|
16
+ options[:store].each do |f|
17
17
  self.stored[f] = original[f] if original.send("#{f}_changed?")
18
18
  end
19
19
  end
20
20
  end
21
21
 
22
22
  def refresh_keywords(original, fields)
23
- self.keywords = fields.inject([]) do |memo,f|
24
- original[f] ? memo | original[f].downcase.split : memo
23
+ self.keywords = fields.select { |f| original.fields[f.to_s] }.inject([]) do |memo,f|
24
+
25
+ if original.fields[f.to_s].localized?
26
+ memo += original.send("#{f}_translations").map do |locale,translation|
27
+ translation.downcase.split.map { |word| "#{locale}:#{word}"}
28
+ end.flatten
29
+ else
30
+ original[f] ? memo += original[f].downcase.split : memo
31
+ end
25
32
  end
33
+ self.keywords.uniq!
26
34
  end
27
35
 
28
36
  def self.reindex(original, fields, options)
@@ -4,7 +4,7 @@ module ActiveSearch
4
4
 
5
5
  # TODO: Wrap this so all engines behave consistently
6
6
  def self.search(text)
7
- Mongoid::Model.all_in(keywords: text.split)
7
+ Mongoid::Model.where(:keywords.in => text.split + text.split.map { |word| "#{I18n.locale}:#{word}"})
8
8
  end
9
9
 
10
10
  module Mongoid
@@ -1,3 +1,3 @@
1
1
  module ActiveSearch
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/spec/mongoid_spec.rb CHANGED
@@ -1,3 +1,4 @@
1
+ #encoding: utf-8
1
2
  require 'mongoid'
2
3
  require 'activesearch/mongoid'
3
4
 
@@ -10,7 +11,7 @@ class MongoidModel
10
11
  field :title, type: String
11
12
  field :text, type: String
12
13
  field :junk, type: String
13
- search_on :title, :text, store: [:title]
14
+ search_on :title, :text, store: [:title, :junk]
14
15
  end
15
16
 
16
17
  class AnotherMongoidModel
@@ -21,18 +22,33 @@ class AnotherMongoidModel
21
22
  search_on :title, :text, store: [:title]
22
23
  end
23
24
 
25
+
26
+ class LocalizedMongoidModel
27
+ include Mongoid::Document
28
+ include ActiveSearch::Mongoid
29
+
30
+ field :title, localize: true
31
+ search_on :title, store: [:title]
32
+ end
33
+
34
+
24
35
  describe ActiveSearch::Mongoid do
25
36
  before do
26
37
  Mongoid.master.collections.select { |c| c.name != 'system.indexes' }.each(&:drop)
27
-
28
- @findable = MongoidModel.create!(title: "Findable")
38
+ I18n.locale = :en
39
+ @findable = MongoidModel.create!(title: "Findable Findable", junk: "Junk field")
29
40
  @quite_findable = MongoidModel.create!(title: "Some title", text: "Findable text")
30
- @another = AnotherMongoidModel.create!(title: "Another findable title")
31
- @junk = MongoidModel.create!(title: "Junk", junk: "Findable junk")
41
+ @another = AnotherMongoidModel.create!(title: "Another findable title")
42
+ @junk = MongoidModel.create!(title: "Junk", junk: "Not Findable junk")
43
+ @localized = LocalizedMongoidModel.create!(title: "English English")
44
+ I18n.with_locale(:es) do
45
+ @localized.title = "Español Español"
46
+ @localized.save
47
+ end
32
48
  end
33
49
 
34
50
  it "should find the expected documents" do
35
- ActiveSearch.search("findable").map { |r| r.stored["title"] }.should == ["Findable", "Some title", "Another findable title"]
51
+ ActiveSearch.search("findable").map { |r| r.stored["title"] }.should == ["Findable Findable", "Some title", "Another findable title"]
36
52
  end
37
53
 
38
54
  it "should store the proper keywords" do
@@ -44,6 +60,17 @@ describe ActiveSearch::Mongoid do
44
60
  end
45
61
 
46
62
  it "should store the specified fields" do
47
- ActiveSearch::Mongoid::Model.where(type: "MongoidModel", original_id: @findable.id).first.stored.should == {"title" => "Findable"}
63
+ ActiveSearch::Mongoid::Model.where(type: "MongoidModel", original_id: @findable.id).first.stored.should == {"title" => "Findable Findable", "junk" => "Junk field"}
64
+ end
65
+
66
+ it "should be able to find by different locales" do
67
+ ActiveSearch.search("english").first.stored["title"]["en"].should == "English English"
68
+ I18n.with_locale(:es) do
69
+ ActiveSearch.search("español").first.stored["title"]["es"].should == "Español Español"
70
+ end
71
+ end
72
+
73
+ it "should store localized keywords" do
74
+ ActiveSearch::Mongoid::Model.where(type: "LocalizedMongoidModel", original_id: @localized.id).first.keywords.should == ["en:english", "es:español"]
48
75
  end
49
76
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activesearch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-26 00:00:00.000000000 Z
12
+ date: 2012-10-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec