Permalink
Browse files

Initial commit to sunspot_mongoid.

  • Loading branch information...
jugyo committed Jul 5, 2010
0 parents commit 9d858a57fac22ad562fd0bb647500cf80158975a
Showing with 724 additions and 0 deletions.
  1. +5 −0 .document
  2. +21 −0 .gitignore
  3. +20 −0 LICENSE
  4. +50 −0 README.md
  5. +53 −0 Rakefile
  6. +36 −0 examples/example.rb
  7. +56 −0 lib/sunspot/mongoid.rb
  8. +418 −0 lib/sunspot/rails/searchable.rb
  9. +2 −0 lib/sunspot_mongoid.rb
  10. +12 −0 test/helper.rb
  11. +51 −0 test/test_sunspot_mongoid.rb
@@ -0,0 +1,5 @@
+README.rdoc
+lib/**/*.rb
+bin/*
+features/**/*.feature
+LICENSE
@@ -0,0 +1,21 @@
+## MAC OS
+.DS_Store
+
+## TEXTMATE
+*.tmproj
+tmtags
+
+## EMACS
+*~
+\#*
+.\#*
+
+## VIM
+*.swp
+
+## PROJECT::GENERAL
+coverage
+rdoc
+pkg
+
+## PROJECT::SPECIFIC
20 LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2009 jugyo
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,50 @@
+sunspot_mongoid
+====
+
+A Sunspot wrapper for Mongoid.
+
+See also: [http://github.com/outoftime/sunspot/tree/master/sunspot_rails/](http://github.com/outoftime/sunspot/tree/master/sunspot_rails/)
+
+Example
+----
+
+ require 'sunspot_mongoid'
+
+ Mongoid.configure do |config|
+ config.master = Mongo::Connection.new.db('sunspot-mongoid-test')
+ end
+
+ # model
+ class Post
+ include Mongoid::Document
+ field :title
+
+ include Sunspot::Mongoid
+ sunspot_setup do
+ text :title
+ end
+ end
+
+ # indexing
+ Post.create(:title => 'foo')
+ Post.create(:title => 'foo bar')
+ Post.create(:title => 'bar baz')
+
+ # commit
+ Sunspot.commit
+
+ # search
+ search = Post.search do
+ keywords 'foo'
+ end
+ search.each_hit_with_result do |hit, post|
+ p post
+ end
+
+ #=> #<Post _id: 4c319556327b3c4b42000001, title: "foo">
+ #=> #<Post _id: 4c319556327b3c4b42000002, title: "foo bar">
+
+Copyright
+----
+
+Copyright (c) 2010 jugyo. See LICENSE for details.
@@ -0,0 +1,53 @@
+require 'rubygems'
+require 'rake'
+
+begin
+ require 'jeweler'
+ Jeweler::Tasks.new do |gem|
+ gem.name = "sunspot_mongoid"
+ gem.summary = %Q{TODO: one-line summary of your gem}
+ gem.description = %Q{TODO: longer description of your gem}
+ gem.email = "jugyo.org@gmail.com"
+ gem.homepage = "http://github.com/jugyo/sunspot_mongoid"
+ gem.authors = ["jugyo"]
+ gem.add_development_dependency "shoulda", ">= 0"
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
+ end
+ Jeweler::GemcutterTasks.new
+rescue LoadError
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
+end
+
+require 'rake/testtask'
+Rake::TestTask.new(:test) do |test|
+ test.libs << 'lib' << 'test'
+ test.pattern = 'test/**/test_*.rb'
+ test.verbose = true
+end
+
+begin
+ require 'rcov/rcovtask'
+ Rcov::RcovTask.new do |test|
+ test.libs << 'test'
+ test.pattern = 'test/**/test_*.rb'
+ test.verbose = true
+ end
+rescue LoadError
+ task :rcov do
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
+ end
+end
+
+task :test => :check_dependencies
+
+task :default => :test
+
+require 'rake/rdoctask'
+Rake::RDocTask.new do |rdoc|
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
+
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = "sunspot_mongoid #{version}"
+ rdoc.rdoc_files.include('README*')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
@@ -0,0 +1,36 @@
+$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
+require 'sunspot_mongoid'
+
+Mongoid.configure do |config|
+ config.master = Mongo::Connection.new.db('sunspot-mongoid-test')
+end
+
+# model
+class Post
+ include Mongoid::Document
+ field :title
+
+ include Sunspot::Mongoid
+ sunspot_setup do
+ text :title
+ end
+end
+
+# remove old indexes
+Post.destroy_all
+
+# indexing
+Post.create(:title => 'foo')
+Post.create(:title => 'foo bar')
+Post.create(:title => 'bar baz')
+
+# commit
+Sunspot.commit
+
+# search
+search = Post.search do
+ keywords 'foo'
+end
+search.each_hit_with_result do |hit, post|
+ p post
+end
@@ -0,0 +1,56 @@
+require 'mongoid'
+require 'sunspot/rails/searchable'
+
+# This code is modified for mongoid from the follow,
+# http://github.com/outoftime/sunspot/blob/master/sunspot_rails/lib/sunspot/rails/searchable.rb
+#
+# Sunspot::Rails is distributed under the MIT License, copyright © 2009 Mat Brown
+module Sunspot
+ module Mongoid
+ def self.included(base)
+ base.class_eval do
+ Sunspot::Adapters::DataAccessor.register(DataAccessor, base)
+ Sunspot::Adapters::InstanceAdapter.register(InstanceAdapter, base)
+
+ extend Sunspot::Rails::Searchable::ClassMethods
+ include Sunspot::Rails::Searchable::InstanceMethods
+
+ def self.sunspot_setup(options = {}, &block)
+ Sunspot.setup(self, &block)
+
+ class_inheritable_hash :sunspot_options
+
+ unless options[:auto_index] == false
+ before_save :maybe_mark_for_auto_indexing
+ after_save :maybe_auto_index
+ end
+
+ unless options[:auto_remove] == false
+ after_destroy do |searchable|
+ searchable.remove_from_index
+ end
+ end
+ options[:include] = Sunspot::Util::Array(options[:include])
+
+ self.sunspot_options = options
+ end
+ end
+ end
+
+ class InstanceAdapter < Sunspot::Adapters::InstanceAdapter
+ def id
+ @instance.id
+ end
+ end
+
+ class DataAccessor < Sunspot::Adapters::DataAccessor
+ def load(id)
+ @clazz.find(id) rescue nil
+ end
+
+ def load_all(ids)
+ @clazz.criteria.in(:_id => ids)
+ end
+ end
+ end
+end
Oops, something went wrong.

0 comments on commit 9d858a5

Please sign in to comment.