Skip to content
This repository was archived by the owner on May 20, 2024. It is now read-only.

Commit 9d858a5

Browse files
author
jugyo
committedJul 5, 2010
Initial commit to sunspot_mongoid.
0 parents  commit 9d858a5

11 files changed

+724
-0
lines changed
 

‎.document

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
README.rdoc
2+
lib/**/*.rb
3+
bin/*
4+
features/**/*.feature
5+
LICENSE

‎.gitignore

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
## MAC OS
2+
.DS_Store
3+
4+
## TEXTMATE
5+
*.tmproj
6+
tmtags
7+
8+
## EMACS
9+
*~
10+
\#*
11+
.\#*
12+
13+
## VIM
14+
*.swp
15+
16+
## PROJECT::GENERAL
17+
coverage
18+
rdoc
19+
pkg
20+
21+
## PROJECT::SPECIFIC

‎LICENSE

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Copyright (c) 2009 jugyo
2+
3+
Permission is hereby granted, free of charge, to any person obtaining
4+
a copy of this software and associated documentation files (the
5+
"Software"), to deal in the Software without restriction, including
6+
without limitation the rights to use, copy, modify, merge, publish,
7+
distribute, sublicense, and/or sell copies of the Software, and to
8+
permit persons to whom the Software is furnished to do so, subject to
9+
the following conditions:
10+
11+
The above copyright notice and this permission notice shall be
12+
included in all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

‎README.md

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
sunspot_mongoid
2+
====
3+
4+
A Sunspot wrapper for Mongoid.
5+
6+
See also: [http://github.com/outoftime/sunspot/tree/master/sunspot_rails/](http://github.com/outoftime/sunspot/tree/master/sunspot_rails/)
7+
8+
Example
9+
----
10+
11+
require 'sunspot_mongoid'
12+
13+
Mongoid.configure do |config|
14+
config.master = Mongo::Connection.new.db('sunspot-mongoid-test')
15+
end
16+
17+
# model
18+
class Post
19+
include Mongoid::Document
20+
field :title
21+
22+
include Sunspot::Mongoid
23+
sunspot_setup do
24+
text :title
25+
end
26+
end
27+
28+
# indexing
29+
Post.create(:title => 'foo')
30+
Post.create(:title => 'foo bar')
31+
Post.create(:title => 'bar baz')
32+
33+
# commit
34+
Sunspot.commit
35+
36+
# search
37+
search = Post.search do
38+
keywords 'foo'
39+
end
40+
search.each_hit_with_result do |hit, post|
41+
p post
42+
end
43+
44+
#=> #<Post _id: 4c319556327b3c4b42000001, title: "foo">
45+
#=> #<Post _id: 4c319556327b3c4b42000002, title: "foo bar">
46+
47+
Copyright
48+
----
49+
50+
Copyright (c) 2010 jugyo. See LICENSE for details.

‎Rakefile

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
require 'rubygems'
2+
require 'rake'
3+
4+
begin
5+
require 'jeweler'
6+
Jeweler::Tasks.new do |gem|
7+
gem.name = "sunspot_mongoid"
8+
gem.summary = %Q{TODO: one-line summary of your gem}
9+
gem.description = %Q{TODO: longer description of your gem}
10+
gem.email = "jugyo.org@gmail.com"
11+
gem.homepage = "http://github.com/jugyo/sunspot_mongoid"
12+
gem.authors = ["jugyo"]
13+
gem.add_development_dependency "shoulda", ">= 0"
14+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15+
end
16+
Jeweler::GemcutterTasks.new
17+
rescue LoadError
18+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19+
end
20+
21+
require 'rake/testtask'
22+
Rake::TestTask.new(:test) do |test|
23+
test.libs << 'lib' << 'test'
24+
test.pattern = 'test/**/test_*.rb'
25+
test.verbose = true
26+
end
27+
28+
begin
29+
require 'rcov/rcovtask'
30+
Rcov::RcovTask.new do |test|
31+
test.libs << 'test'
32+
test.pattern = 'test/**/test_*.rb'
33+
test.verbose = true
34+
end
35+
rescue LoadError
36+
task :rcov do
37+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
38+
end
39+
end
40+
41+
task :test => :check_dependencies
42+
43+
task :default => :test
44+
45+
require 'rake/rdoctask'
46+
Rake::RDocTask.new do |rdoc|
47+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
48+
49+
rdoc.rdoc_dir = 'rdoc'
50+
rdoc.title = "sunspot_mongoid #{version}"
51+
rdoc.rdoc_files.include('README*')
52+
rdoc.rdoc_files.include('lib/**/*.rb')
53+
end

‎examples/example.rb

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2+
require 'sunspot_mongoid'
3+
4+
Mongoid.configure do |config|
5+
config.master = Mongo::Connection.new.db('sunspot-mongoid-test')
6+
end
7+
8+
# model
9+
class Post
10+
include Mongoid::Document
11+
field :title
12+
13+
include Sunspot::Mongoid
14+
sunspot_setup do
15+
text :title
16+
end
17+
end
18+
19+
# remove old indexes
20+
Post.destroy_all
21+
22+
# indexing
23+
Post.create(:title => 'foo')
24+
Post.create(:title => 'foo bar')
25+
Post.create(:title => 'bar baz')
26+
27+
# commit
28+
Sunspot.commit
29+
30+
# search
31+
search = Post.search do
32+
keywords 'foo'
33+
end
34+
search.each_hit_with_result do |hit, post|
35+
p post
36+
end

‎lib/sunspot/mongoid.rb

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
require 'mongoid'
2+
require 'sunspot/rails/searchable'
3+
4+
# This code is modified for mongoid from the follow,
5+
# http://github.com/outoftime/sunspot/blob/master/sunspot_rails/lib/sunspot/rails/searchable.rb
6+
#
7+
# Sunspot::Rails is distributed under the MIT License, copyright © 2009 Mat Brown
8+
module Sunspot
9+
module Mongoid
10+
def self.included(base)
11+
base.class_eval do
12+
Sunspot::Adapters::DataAccessor.register(DataAccessor, base)
13+
Sunspot::Adapters::InstanceAdapter.register(InstanceAdapter, base)
14+
15+
extend Sunspot::Rails::Searchable::ClassMethods
16+
include Sunspot::Rails::Searchable::InstanceMethods
17+
18+
def self.sunspot_setup(options = {}, &block)
19+
Sunspot.setup(self, &block)
20+
21+
class_inheritable_hash :sunspot_options
22+
23+
unless options[:auto_index] == false
24+
before_save :maybe_mark_for_auto_indexing
25+
after_save :maybe_auto_index
26+
end
27+
28+
unless options[:auto_remove] == false
29+
after_destroy do |searchable|
30+
searchable.remove_from_index
31+
end
32+
end
33+
options[:include] = Sunspot::Util::Array(options[:include])
34+
35+
self.sunspot_options = options
36+
end
37+
end
38+
end
39+
40+
class InstanceAdapter < Sunspot::Adapters::InstanceAdapter
41+
def id
42+
@instance.id
43+
end
44+
end
45+
46+
class DataAccessor < Sunspot::Adapters::DataAccessor
47+
def load(id)
48+
@clazz.find(id) rescue nil
49+
end
50+
51+
def load_all(ids)
52+
@clazz.criteria.in(:_id => ids)
53+
end
54+
end
55+
end
56+
end

‎lib/sunspot/rails/searchable.rb

+418
Large diffs are not rendered by default.

‎lib/sunspot_mongoid.rb

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
require 'sunspot'
2+
require 'sunspot/mongoid'

‎test/helper.rb

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
require 'rubygems'
2+
require 'test/unit'
3+
require 'shoulda'
4+
require 'rr'
5+
6+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
7+
$LOAD_PATH.unshift(File.dirname(__FILE__))
8+
require 'sunspot_mongoid'
9+
10+
class Test::Unit::TestCase
11+
include RR::Adapters::TestUnit
12+
end

‎test/test_sunspot_mongoid.rb

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
require 'helper'
2+
3+
#
4+
# NOTE: I think tests are too few...
5+
#
6+
class TestSunspotMongoid < Test::Unit::TestCase
7+
class Foo
8+
include Mongoid::Document
9+
field :title
10+
11+
include Sunspot::Mongoid
12+
sunspot_setup do
13+
text :title
14+
end
15+
end
16+
17+
class Bar
18+
include Mongoid::Document
19+
field :title
20+
21+
include Sunspot::Mongoid
22+
sunspot_setup(:auto_index => false, :auto_remove => false) do
23+
text :title
24+
end
25+
end
26+
27+
context 'default' do
28+
should 'sunspot_options is specified' do
29+
assert Foo.sunspot_options == {:include => []}
30+
assert Bar.sunspot_options == {:auto_index=>false, :auto_remove=>false, :include=>[]}
31+
end
32+
33+
should 'be called Sunspot.setup when call Foo.sunspot_setup' do
34+
mock(Sunspot).setup(Foo)
35+
Foo.sunspot_setup
36+
end
37+
38+
should 'get as text_fields from Sunspot::Setup' do
39+
text_field = Sunspot::Setup.for(Foo).all_text_fields.first
40+
assert text_field.type == Sunspot::Type::TextType.instance
41+
assert text_field.name == :title
42+
end
43+
44+
should 'search' do
45+
options = {}
46+
mock.proxy(Foo).solr_execute_search(options)
47+
mock(Sunspot).new_search(Foo) { mock(Object.new).execute }
48+
Foo.search(options)
49+
end
50+
end
51+
end

0 commit comments

Comments
 (0)
This repository has been archived.