activesearch 0.0.3 → 0.0.4

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.
@@ -0,0 +1,54 @@
1
+ #encoding: utf-8
2
+
3
+ require File.join(File.dirname(__FILE__), 'spec_helper')
4
+
5
+ def cleanup(engine)
6
+ case engine
7
+ when "ElasticSearch"
8
+ Tire::Configuration.client.delete "#{Tire::Configuration.url}/_all"
9
+ load File.join(File.dirname(__FILE__), 'models', 'elastic_search.rb')
10
+ when "Mongoid"
11
+ Mongoid.master.collections.select { |c| c.name != 'system.indexes' }.each(&:drop)
12
+ end
13
+ end
14
+
15
+ Dir[File.join(File.dirname(__FILE__), 'models', '*.rb')].map { |f| File.basename(f, '.rb') }.each do |filename|
16
+ engine = filename.split('_').collect { |w| w.capitalize }.join
17
+
18
+ describe "ActiveSearch::#{engine}" do
19
+ before(:all) do
20
+ require File.join(File.dirname(__FILE__), 'models', filename)
21
+ end
22
+
23
+ before do
24
+ cleanup(engine)
25
+ @findable = Object.const_get("#{engine}Model").create(title: "Findable Findable", junk: "Junk field")
26
+ @quite_findable = Object.const_get("#{engine}Model").create(title: "Some title", text: "Findable text")
27
+ @another = Object.const_get("Another#{engine}Model").create(title: "Another findable title")
28
+ @junk = Object.const_get("#{engine}Model").create(title: "Junk", junk: "Not Findable junk")
29
+ end
30
+
31
+ it "should find the expected documents" do
32
+ results = ActiveSearch.search("findable").map { |doc| doc.to_hash.select { |k,v| %w[title junk].include?(k.to_s) } }
33
+ results.sort_by { |result| result["title"] }.should == [
34
+ {
35
+ "title" => "Another findable title"
36
+ },
37
+ {
38
+ "title" => "Findable Findable",
39
+ "junk" => "Junk field"
40
+ },
41
+ {
42
+ "title" => "Some title"
43
+ },
44
+ ]
45
+ ActiveSearch.search("some text").first.to_hash["title"].should == "Some title"
46
+ ActiveSearch.search("junk").first.to_hash["title"].should == "Junk"
47
+ end
48
+
49
+ it "should remove destroyed documents from index" do
50
+ @findable.destroy
51
+ ActiveSearch.search("findable").count.should == 2
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,35 @@
1
+ require 'tire'
2
+ require 'activesearch/elastic_search'
3
+
4
+ Tire.configure { logger File.join(File.dirname(__FILE__), '..', '..', 'log', 'elasticsearch.log'), :level => 'debug' }
5
+
6
+ module ElasticSearchRefresh
7
+
8
+ def save
9
+ super.tap { tire.index.refresh }
10
+ end
11
+
12
+ def destroy
13
+ super.tap { tire.index.refresh; sleep(1) }
14
+ end
15
+ end
16
+
17
+ class ElasticSearchModel < ActiveMimic
18
+ include ActiveSearch::ElasticSearch
19
+ include ElasticSearchRefresh
20
+
21
+ attribute :title
22
+ attribute :text
23
+ attribute :junk
24
+
25
+ search_by :title, :text, store: [:title, :junk]
26
+
27
+ end
28
+
29
+ class AnotherElasticSearchModel < ActiveMimic
30
+ include ActiveSearch::ElasticSearch
31
+ include ElasticSearchRefresh
32
+
33
+ attribute :title, type: String
34
+ search_by :title, store: [:title]
35
+ end
@@ -0,0 +1,31 @@
1
+ require 'mongoid'
2
+ require 'activesearch/mongoid'
3
+
4
+ Mongoid.database = Mongo::Connection.new("localhost").db("activesearch_test")
5
+
6
+ class MongoidModel
7
+ include Mongoid::Document
8
+ include ActiveSearch::Mongoid
9
+
10
+ field :title, type: String
11
+ field :text, type: String
12
+ field :junk, type: String
13
+ search_by :title, :text, store: [:title, :junk]
14
+ end
15
+
16
+ class AnotherMongoidModel
17
+ include Mongoid::Document
18
+ include ActiveSearch::Mongoid
19
+
20
+ field :title, type: String
21
+ search_by :title, :text, store: [:title]
22
+ end
23
+
24
+
25
+ class LocalizedMongoidModel
26
+ include Mongoid::Document
27
+ include ActiveSearch::Mongoid
28
+
29
+ field :title, localize: true
30
+ search_by :title, store: [:title]
31
+ end
data/spec/mongoid_spec.rb CHANGED
@@ -1,34 +1,16 @@
1
1
  #encoding: utf-8
2
+ require File.join(File.dirname(__FILE__), 'spec_helper')
2
3
  require 'mongoid'
3
4
  require 'activesearch/mongoid'
4
5
 
5
6
  Mongoid.database = Mongo::Connection.new("localhost").db("activesearch_test")
6
7
 
7
- class MongoidModel
8
- include Mongoid::Document
9
- include ActiveSearch::Mongoid
10
-
11
- field :title, type: String
12
- field :text, type: String
13
- field :junk, type: String
14
- search_on :title, :text, store: [:title, :junk]
15
- end
16
-
17
- class AnotherMongoidModel
18
- include Mongoid::Document
19
- include ActiveSearch::Mongoid
20
-
21
- field :title, type: String
22
- search_on :title, :text, store: [:title]
23
- end
24
-
25
-
26
8
  class LocalizedMongoidModel
27
9
  include Mongoid::Document
28
10
  include ActiveSearch::Mongoid
29
11
 
30
12
  field :title, localize: true
31
- search_on :title, store: [:title]
13
+ search_by :title, store: [:title]
32
14
  end
33
15
 
34
16
 
@@ -36,41 +18,21 @@ describe ActiveSearch::Mongoid do
36
18
  before do
37
19
  Mongoid.master.collections.select { |c| c.name != 'system.indexes' }.each(&:drop)
38
20
  I18n.locale = :en
39
- @findable = MongoidModel.create!(title: "Findable Findable", junk: "Junk field")
40
- @quite_findable = MongoidModel.create!(title: "Some title", text: "Findable text")
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")
21
+ @localized = LocalizedMongoidModel.create!(title: "English English")
44
22
  I18n.with_locale(:es) do
45
23
  @localized.title = "Español Español"
46
- @localized.save
24
+ @localized.save!
47
25
  end
48
26
  end
49
27
 
50
- it "should find the expected documents" do
51
- ActiveSearch.search("findable").map { |r| r.stored["title"] }.should == ["Findable Findable", "Some title", "Another findable title"]
52
- end
53
-
54
- it "should store the proper keywords" do
55
- ActiveSearch::Mongoid::Model.where(type: "MongoidModel", original_id: @quite_findable.id).first.keywords.should == %w{some title findable text}
56
- end
57
-
58
- it "should be chainable" do
59
- ActiveSearch.search("findable").should respond_to(:where)
60
- end
61
-
62
- it "should store the specified fields" do
63
- ActiveSearch::Mongoid::Model.where(type: "MongoidModel", original_id: @findable.id).first.stored.should == {"title" => "Findable Findable", "junk" => "Junk field"}
64
- end
65
-
66
28
  it "should be able to find by different locales" do
67
- ActiveSearch.search("english").first.stored["title"]["en"].should == "English English"
29
+ ActiveSearch.search("english").first._stored["title"]["en"].should == "English English"
68
30
  I18n.with_locale(:es) do
69
- ActiveSearch.search("español").first.stored["title"]["es"].should == "Español Español"
31
+ ActiveSearch.search("español").first._stored["title"]["es"].should == "Español Español"
70
32
  end
71
33
  end
72
34
 
73
35
  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"]
36
+ ActiveSearch::Mongoid::Model.where(_original_type: "LocalizedMongoidModel", _original_id: @localized.id).first._keywords.should == ["en:english", "es:español"]
75
37
  end
76
38
  end
@@ -0,0 +1,31 @@
1
+ require 'active_model'
2
+ require 'active_attr'
3
+
4
+ class ActiveMimic
5
+ extend ActiveModel::Callbacks
6
+ extend ActiveModel::Naming
7
+ include ActiveModel::Serialization
8
+ include ActiveAttr::Attributes
9
+ include ActiveAttr::MassAssignment
10
+
11
+ attribute :id
12
+
13
+ define_model_callbacks :save
14
+ define_model_callbacks :destroy
15
+
16
+ def self.create(attrs)
17
+ new(attrs).tap(&:save)
18
+ end
19
+
20
+ def save
21
+ run_callbacks :save do
22
+ true
23
+ end
24
+ end
25
+
26
+ def destroy
27
+ run_callbacks :destroy do
28
+ true
29
+ end
30
+ end
31
+ 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.3
4
+ version: 0.0.4
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-29 00:00:00.000000000 Z
12
+ date: 2012-11-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -43,6 +43,22 @@ dependencies:
43
43
  - - ! '>='
44
44
  - !ruby/object:Gem::Version
45
45
  version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: active_attr
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
46
62
  - !ruby/object:Gem::Dependency
47
63
  name: mongoid
48
64
  requirement: !ruby/object:Gem::Requirement
@@ -50,7 +66,7 @@ dependencies:
50
66
  requirements:
51
67
  - - ~>
52
68
  - !ruby/object:Gem::Version
53
- version: '2.4'
69
+ version: '2'
54
70
  type: :development
55
71
  prerelease: false
56
72
  version_requirements: !ruby/object:Gem::Requirement
@@ -58,7 +74,39 @@ dependencies:
58
74
  requirements:
59
75
  - - ~>
60
76
  - !ruby/object:Gem::Version
61
- version: '2.4'
77
+ version: '2'
78
+ - !ruby/object:Gem::Dependency
79
+ name: tire
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: parallel_tests
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
62
110
  description: ORM agnostic full text search
63
111
  email:
64
112
  - papipo@gmail.com
@@ -72,12 +120,21 @@ files:
72
120
  - README.md
73
121
  - Rakefile
74
122
  - activesearch.gemspec
123
+ - elasticsearch.log
75
124
  - lib/activesearch.rb
125
+ - lib/activesearch/elastic_search.rb
126
+ - lib/activesearch/elastic_search/proxy.rb
76
127
  - lib/activesearch/mongoid.rb
77
128
  - lib/activesearch/mongoid/model.rb
129
+ - lib/activesearch/result.rb
78
130
  - lib/activesearch/version.rb
131
+ - log/elasticsearch.log
79
132
  - spec/config/mongoid.yml
133
+ - spec/engines_spec.rb
134
+ - spec/models/elastic_search.rb
135
+ - spec/models/mongoid.rb
80
136
  - spec/mongoid_spec.rb
137
+ - spec/spec_helper.rb
81
138
  homepage: ''
82
139
  licenses: []
83
140
  post_install_message:
@@ -105,4 +162,8 @@ summary: ActiveSearch lets you plug in a ruby module in any class that will allo
105
162
  you to do full text searches.
106
163
  test_files:
107
164
  - spec/config/mongoid.yml
165
+ - spec/engines_spec.rb
166
+ - spec/models/elastic_search.rb
167
+ - spec/models/mongoid.rb
108
168
  - spec/mongoid_spec.rb
169
+ - spec/spec_helper.rb