aq1018-sunspot_mongoid 0.4.0

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.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/LICENSE ADDED
@@ -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.
data/README.md ADDED
@@ -0,0 +1,58 @@
1
+ sunspot_mongoid
2
+ ====
3
+
4
+ A Sunspot wrapper for Mongoid.
5
+
6
+ Install
7
+ ----
8
+
9
+ gem install sunspot_mongoid
10
+
11
+ Examples
12
+ ----
13
+
14
+ class Post
15
+ include Mongoid::Document
16
+ field :title
17
+
18
+ include Sunspot::Mongoid
19
+ searchable do
20
+ text :title
21
+ end
22
+ end
23
+
24
+ For Rails3
25
+ ----
26
+
27
+ ### as gem:
28
+
29
+ add a gem to Gemfile as following,
30
+
31
+ gem 'sunspot_mongoid'
32
+
33
+ create config/initializers/sunspot_mongoid.rb,
34
+
35
+ Sunspot.session = Sunspot::Rails.build_session
36
+ ActionController::Base.module_eval { include(Sunspot::Rails::RequestLifecycle) }
37
+
38
+ ### as plugin:
39
+
40
+ add gems to Gemfile as following,
41
+
42
+ gem 'sunspot'
43
+ gem 'sunspot_rails'
44
+
45
+ and install sunspot_mongoid as rails plugin,
46
+
47
+ rails plugin install git://github.com/jugyo/sunspot_mongoid.git
48
+
49
+ Links
50
+ ----
51
+
52
+ * [sunspot](http://github.com/outoftime/sunspot)
53
+ * [sunspot_rails](http://github.com/outoftime/sunspot/tree/master/sunspot_rails/)
54
+
55
+ Copyright
56
+ ----
57
+
58
+ Copyright (c) 2010 jugyo. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,57 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "aq1018-sunspot_mongoid"
8
+ gem.summary = %Q{A Sunspot wrapper for Mongoid.}
9
+ gem.description = %Q{A Sunspot wrapper for Mongoid that is like sunspot_rails.}
10
+ gem.email = "jugyo.org@gmail.com aq1018@gmail.com"
11
+ gem.homepage = "http://github.com/aq1018/sunspot_mongoid"
12
+ gem.authors = ["jugyo", "aq1018"]
13
+ gem.add_runtime_dependency "mongoid", ">= 0"
14
+ gem.add_runtime_dependency "sunspot", ">= 1.1.0"
15
+ gem.add_runtime_dependency "sunspot_rails", ">= 1.1.0"
16
+ gem.add_development_dependency "shoulda", ">= 0"
17
+ gem.add_development_dependency "rr", ">= 0"
18
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
19
+ end
20
+ Jeweler::GemcutterTasks.new
21
+ rescue LoadError
22
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
23
+ end
24
+
25
+ require 'rake/testtask'
26
+ Rake::TestTask.new(:test) do |test|
27
+ test.libs << 'lib' << 'test'
28
+ test.pattern = 'test/**/test_*.rb'
29
+ test.verbose = true
30
+ end
31
+
32
+ begin
33
+ require 'rcov/rcovtask'
34
+ Rcov::RcovTask.new do |test|
35
+ test.libs << 'test'
36
+ test.pattern = 'test/**/test_*.rb'
37
+ test.verbose = true
38
+ end
39
+ rescue LoadError
40
+ task :rcov do
41
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
42
+ end
43
+ end
44
+
45
+ task :test => :check_dependencies
46
+
47
+ task :default => :test
48
+
49
+ require 'rake/rdoctask'
50
+ Rake::RDocTask.new do |rdoc|
51
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
52
+
53
+ rdoc.rdoc_dir = 'rdoc'
54
+ rdoc.title = "sunspot_mongoid #{version}"
55
+ rdoc.rdoc_files.include('README*')
56
+ rdoc.rdoc_files.include('lib/**/*.rb')
57
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.4.0
@@ -0,0 +1,69 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{aq1018-sunspot_mongoid}
8
+ s.version = "0.4.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["jugyo", "aq1018"]
12
+ s.date = %q{2011-02-15}
13
+ s.description = %q{A Sunspot wrapper for Mongoid that is like sunspot_rails.}
14
+ s.email = %q{jugyo.org@gmail.com aq1018@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ "LICENSE",
22
+ "README.md",
23
+ "Rakefile",
24
+ "VERSION",
25
+ "aq1018-sunspot_mongoid.gemspec",
26
+ "examples/example.rb",
27
+ "init.rb",
28
+ "lib/sunspot/mongoid.rb",
29
+ "lib/sunspot/mongoid/railtie.rb",
30
+ "lib/sunspot_mongoid.rb",
31
+ "tasks/sunspot_mongoid.rake",
32
+ "test/helper.rb",
33
+ "test/test_sunspot_mongoid.rb"
34
+ ]
35
+ s.homepage = %q{http://github.com/aq1018/sunspot_mongoid}
36
+ s.require_paths = ["lib"]
37
+ s.rubygems_version = %q{1.5.2}
38
+ s.summary = %q{A Sunspot wrapper for Mongoid.}
39
+ s.test_files = [
40
+ "examples/example.rb",
41
+ "test/helper.rb",
42
+ "test/test_sunspot_mongoid.rb"
43
+ ]
44
+
45
+ if s.respond_to? :specification_version then
46
+ s.specification_version = 3
47
+
48
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
49
+ s.add_runtime_dependency(%q<mongoid>, [">= 0"])
50
+ s.add_runtime_dependency(%q<sunspot>, [">= 1.1.0"])
51
+ s.add_runtime_dependency(%q<sunspot_rails>, [">= 1.1.0"])
52
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
53
+ s.add_development_dependency(%q<rr>, [">= 0"])
54
+ else
55
+ s.add_dependency(%q<mongoid>, [">= 0"])
56
+ s.add_dependency(%q<sunspot>, [">= 1.1.0"])
57
+ s.add_dependency(%q<sunspot_rails>, [">= 1.1.0"])
58
+ s.add_dependency(%q<shoulda>, [">= 0"])
59
+ s.add_dependency(%q<rr>, [">= 0"])
60
+ end
61
+ else
62
+ s.add_dependency(%q<mongoid>, [">= 0"])
63
+ s.add_dependency(%q<sunspot>, [">= 1.1.0"])
64
+ s.add_dependency(%q<sunspot_rails>, [">= 1.1.0"])
65
+ s.add_dependency(%q<shoulda>, [">= 0"])
66
+ s.add_dependency(%q<rr>, [">= 0"])
67
+ end
68
+ end
69
+
@@ -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
+ searchable 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
data/init.rb ADDED
@@ -0,0 +1,3 @@
1
+ require 'sunspot_mongoid'
2
+ Sunspot.session = Sunspot::Rails.build_session
3
+ ActionController::Base.module_eval { include(Sunspot::Rails::RequestLifecycle) }
@@ -0,0 +1,10 @@
1
+ require 'sunspot/mongoid'
2
+ require 'rails'
3
+
4
+ module Sunspot::Mongoid
5
+ class Railtie < Rails::Railtie
6
+ rake_tasks do
7
+ load File.expand_path("../../../../tasks/sunspot_mongoid.rake", __FILE__)
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,66 @@
1
+ require 'sunspot'
2
+ require 'mongoid'
3
+ require 'sunspot/rails'
4
+
5
+ # == Examples:
6
+ #
7
+ # class Post
8
+ # include Mongoid::Document
9
+ # field :title
10
+ #
11
+ # include Sunspot::Mongoid
12
+ # searchable do
13
+ # text :title
14
+ # end
15
+ # end
16
+ #
17
+ module Sunspot
18
+ module Mongoid
19
+ def self.included(base)
20
+ base.class_eval do
21
+ extend Sunspot::Rails::Searchable::ActsAsMethods
22
+ extend Sunspot::Mongoid::ActsAsMethods
23
+ Sunspot::Adapters::DataAccessor.register(DataAccessor, base)
24
+ Sunspot::Adapters::InstanceAdapter.register(InstanceAdapter, base)
25
+ end
26
+ end
27
+
28
+ module ActsAsMethods
29
+ # ClassMethods isn't loaded until searchable is called so we need
30
+ # call it, then extend our own ClassMethods.
31
+ def searchable (opt = {}, &block)
32
+ super
33
+ extend ClassMethods
34
+ end
35
+ end
36
+
37
+ module ClassMethods
38
+ # The sunspot solr_index method is very dependent on ActiveRecord, so
39
+ # we'll change it to work more efficiently with Mongoid.
40
+ def solr_index(opt={})
41
+ all.each do |m|
42
+ Sunspot.index(m)
43
+ end
44
+ Sunspot.commit
45
+ end
46
+ end
47
+
48
+
49
+ class InstanceAdapter < Sunspot::Adapters::InstanceAdapter
50
+ def id
51
+ @instance.id
52
+ end
53
+ end
54
+
55
+ class DataAccessor < Sunspot::Adapters::DataAccessor
56
+ def load(id)
57
+ @clazz.find(id) rescue nil
58
+ end
59
+
60
+ def load_all(ids)
61
+ @clazz.where(:_id.in => ids.map { |id| BSON::ObjectId.from_string(id) })
62
+ end
63
+
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,2 @@
1
+ require 'sunspot/mongoid'
2
+ require 'sunspot/mongoid/railtie' if defined?(Rails)
@@ -0,0 +1,43 @@
1
+ # auto load rails tasks
2
+ require 'sunspot/rails/tasks'
3
+
4
+ # hack to remove 'sunspot:reindex' task and add our own
5
+ Rake::TaskManager.class_eval do
6
+ def remove_task(task_name)
7
+ @tasks.delete(task_name.to_s)
8
+ end
9
+ end
10
+
11
+ Rake.application.remove_task('sunspot:reindex')
12
+
13
+ # override the tasks that depends on active record
14
+ namespace :sunspot do
15
+
16
+ desc "Reindex all solr models that are located in your application's models directory. (Batch size ignored)"
17
+ # This task depends on the standard Rails file naming \
18
+ # conventions, in that the file name matches the defined class name. \
19
+ # By default the indexing system works in batches of 50 records, you can \
20
+ # set your own value for this by using the batch_size argument. You can \
21
+ # also optionally define a list of models to separated by a forward slash '/'
22
+ #
23
+ # $ rake sunspot:reindex # reindex all models
24
+ # $ rake sunspot:reindex[1000] # reindex in batches of 1000
25
+ # $ rake sunspot:reindex[false] # reindex without batching
26
+ # $ rake sunspot:reindex[,Post] # reindex only the Post model
27
+ # $ rake sunspot:reindex[1000,Post] # reindex only the Post model in
28
+ # # batchs of 1000
29
+ # $ rake sunspot:reindex[,Post+Author] # reindex Post and Author model
30
+ task :reindex, :batch_size, :models, :needs => :environment do |t, args|
31
+ unless args[:models]
32
+ all_files = Dir.glob(Rails.root.join('app', 'models', '*.rb'))
33
+ all_models = all_files.map { |path| File.basename(path, '.rb').camelize.constantize }
34
+ sunspot_models = all_models.select { |m| m.respond_to?(:searchable?) and m.searchable? }
35
+ else
36
+ sunspot_models = args[:models].split('+').map{|m| m.constantize}
37
+ end
38
+ sunspot_models.each do |model|
39
+ puts "Re-indexing #{model.name}"
40
+ model.solr_reindex
41
+ end
42
+ end
43
+ end
data/test/helper.rb ADDED
@@ -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
@@ -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
+ searchable 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
+ searchable(: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.searchable' do
34
+ mock(Sunspot).setup(Foo)
35
+ Foo.searchable
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
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: aq1018-sunspot_mongoid
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.4.0
6
+ platform: ruby
7
+ authors:
8
+ - jugyo
9
+ - aq1018
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+
14
+ date: 2011-02-15 00:00:00 -08:00
15
+ default_executable:
16
+ dependencies:
17
+ - !ruby/object:Gem::Dependency
18
+ name: mongoid
19
+ prerelease: false
20
+ requirement: &id001 !ruby/object:Gem::Requirement
21
+ none: false
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: "0"
26
+ type: :runtime
27
+ version_requirements: *id001
28
+ - !ruby/object:Gem::Dependency
29
+ name: sunspot
30
+ prerelease: false
31
+ requirement: &id002 !ruby/object:Gem::Requirement
32
+ none: false
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: 1.1.0
37
+ type: :runtime
38
+ version_requirements: *id002
39
+ - !ruby/object:Gem::Dependency
40
+ name: sunspot_rails
41
+ prerelease: false
42
+ requirement: &id003 !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 1.1.0
48
+ type: :runtime
49
+ version_requirements: *id003
50
+ - !ruby/object:Gem::Dependency
51
+ name: shoulda
52
+ prerelease: false
53
+ requirement: &id004 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ type: :development
60
+ version_requirements: *id004
61
+ - !ruby/object:Gem::Dependency
62
+ name: rr
63
+ prerelease: false
64
+ requirement: &id005 !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: "0"
70
+ type: :development
71
+ version_requirements: *id005
72
+ description: A Sunspot wrapper for Mongoid that is like sunspot_rails.
73
+ email: jugyo.org@gmail.com aq1018@gmail.com
74
+ executables: []
75
+
76
+ extensions: []
77
+
78
+ extra_rdoc_files:
79
+ - LICENSE
80
+ - README.md
81
+ files:
82
+ - .document
83
+ - LICENSE
84
+ - README.md
85
+ - Rakefile
86
+ - VERSION
87
+ - aq1018-sunspot_mongoid.gemspec
88
+ - examples/example.rb
89
+ - init.rb
90
+ - lib/sunspot/mongoid.rb
91
+ - lib/sunspot/mongoid/railtie.rb
92
+ - lib/sunspot_mongoid.rb
93
+ - tasks/sunspot_mongoid.rake
94
+ - test/helper.rb
95
+ - test/test_sunspot_mongoid.rb
96
+ has_rdoc: true
97
+ homepage: http://github.com/aq1018/sunspot_mongoid
98
+ licenses: []
99
+
100
+ post_install_message:
101
+ rdoc_options: []
102
+
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: "0"
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ none: false
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: "0"
117
+ requirements: []
118
+
119
+ rubyforge_project:
120
+ rubygems_version: 1.5.2
121
+ signing_key:
122
+ specification_version: 3
123
+ summary: A Sunspot wrapper for Mongoid.
124
+ test_files:
125
+ - examples/example.rb
126
+ - test/helper.rb
127
+ - test/test_sunspot_mongoid.rb