goldstar-sunspot_rails 0.10.6.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. data/LICENSE +18 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.rdoc +236 -0
  4. data/Rakefile +19 -0
  5. data/VERSION.yml +4 -0
  6. data/dev_tasks/gemspec.rake +28 -0
  7. data/dev_tasks/rdoc.rake +7 -0
  8. data/dev_tasks/release.rake +4 -0
  9. data/dev_tasks/todo.rake +4 -0
  10. data/generators/sunspot/sunspot_generator.rb +9 -0
  11. data/generators/sunspot/templates/sunspot.yml +19 -0
  12. data/install.rb +1 -0
  13. data/lib/sunspot/rails/adapters.rb +54 -0
  14. data/lib/sunspot/rails/configuration.rb +169 -0
  15. data/lib/sunspot/rails/request_lifecycle.rb +18 -0
  16. data/lib/sunspot/rails/searchable.rb +305 -0
  17. data/lib/sunspot/rails/tasks.rb +47 -0
  18. data/lib/sunspot/rails.rb +17 -0
  19. data/rails/init.rb +11 -0
  20. data/spec/configuration_spec.rb +53 -0
  21. data/spec/mock_app/app/controllers/application.rb +10 -0
  22. data/spec/mock_app/app/controllers/application_controller.rb +10 -0
  23. data/spec/mock_app/app/controllers/posts_controller.rb +6 -0
  24. data/spec/mock_app/app/models/blog.rb +2 -0
  25. data/spec/mock_app/app/models/post.rb +5 -0
  26. data/spec/mock_app/app/models/post_with_auto.rb +9 -0
  27. data/spec/mock_app/config/boot.rb +110 -0
  28. data/spec/mock_app/config/database.yml +4 -0
  29. data/spec/mock_app/config/environment.rb +41 -0
  30. data/spec/mock_app/config/environments/development.rb +27 -0
  31. data/spec/mock_app/config/environments/test.rb +27 -0
  32. data/spec/mock_app/config/initializers/new_rails_defaults.rb +19 -0
  33. data/spec/mock_app/config/initializers/session_store.rb +15 -0
  34. data/spec/mock_app/config/routes.rb +43 -0
  35. data/spec/mock_app/config/sunspot.yml +14 -0
  36. data/spec/model_lifecycle_spec.rb +38 -0
  37. data/spec/model_spec.rb +278 -0
  38. data/spec/request_lifecycle_spec.rb +30 -0
  39. data/spec/schema.rb +14 -0
  40. data/spec/spec_helper.rb +32 -0
  41. metadata +222 -0
data/LICENSE ADDED
@@ -0,0 +1,18 @@
1
+ Permission is hereby granted, free of charge, to any person obtaining
2
+ a copy of this software and associated documentation files (the
3
+ 'Software'), to deal in the Software without restriction, including
4
+ without limitation the rights to use, copy, modify, merge, publish,
5
+ distribute, sublicense, and/or sell copies of the Software, and to
6
+ permit persons to whom the Software is furnished to do so, subject to
7
+ the following conditions:
8
+
9
+ The above copyright notice and this permission notice shall be
10
+ included in all copies or substantial portions of the Software.
11
+
12
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
13
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
15
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
16
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
17
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
18
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Mat Brown
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.rdoc ADDED
@@ -0,0 +1,236 @@
1
+ = Sunspot::Rails
2
+
3
+ == This branch has Goldstar specific hacks. I would steer clear!
4
+
5
+
6
+ Sunspot::Rails is a Rails plugin that provides drop-in integration of the
7
+ Sunspot[http://outoftime.github.com/sunspot] Solr search library with Rails. It
8
+ provides the following features:
9
+
10
+ * Configure Sunspot using config/sunspot.yml
11
+ * Extend ActiveRecord for easy index configuration, search, and indexing
12
+ * Automatically index ActiveRecord objects when they are saved, and remove them
13
+ from the index when they are destroyed (can be disabled)
14
+ * Automatically commit Solr changes at the end of each request
15
+ * Provide utility methods to find and fix orphaned documents and rebuild the
16
+ Solr index for a given class
17
+ * Provide rake tasks for starting and stopping the development Solr instance,
18
+ using the configuration in sunspot.yml
19
+
20
+ Sunspot::Rails has been tested with Rails versions 2.1, 2.2, and 2.3
21
+
22
+ == Installation
23
+
24
+ For recent versions of Rails, In your project's <code>config/environment.rb</code>, add the following gem dependencies:
25
+
26
+ config.gem 'outoftime-sunspot', :lib => 'sunspot',
27
+ :source => 'http://gems.github.com'
28
+ config.gem 'outoftime-sunspot_rails', :lib => 'sunspot/rails',
29
+ :source => 'http://gems.github.com'
30
+
31
+ Install the gems with:
32
+
33
+ rake gems:install
34
+
35
+ If you are using an older version of Rails that doesn't support plugins-as-gems, install the gems manually and install the plugin:
36
+
37
+ sudo gem install outoftime-sunspot outoftime-sunspot_rails --source=http://gems.github.com
38
+
39
+ script/plugin install git://github.com/outoftime/sunspot_rails.git
40
+
41
+ Generate the file <code>config/sunspot.yml</code>:
42
+
43
+ script/generate sunspot
44
+
45
+ Rails doesn't automatically load rake tasks from plugins installed as gems
46
+ (https://rails.lighthouseapp.com/projects/8994/tickets/59). If you installed
47
+ Sunspot::Rails as a gem, add the following line to your project's Rakefile:
48
+
49
+ require 'sunspot/rails/tasks'
50
+
51
+ If you wish to make modifications to the Solr schema, you can create a custom
52
+ Solr home in your project directory. In order to do so, create the directory
53
+ <code>RAILS_ROOT/solr/conf</code>, and copy in the contents of the Solr gem's
54
+ <code>solr/solr/conf</code> directory. If the files are in the right place,
55
+ Sunspot::Rails will detect them and tell Solr to use your local configurations.
56
+ Use caution when modifying <code>schema.xml</code> - Sunspot relies on the
57
+ field naming scheme in the packaged schema file.
58
+
59
+ To start up a Solr instance, issue the following:
60
+
61
+ rake sunspot:solr:start
62
+
63
+ Note that using the built-in Solr instance packaged with Sunspot is great for
64
+ development, but is not recommended for production. See the Sunspot
65
+ documentation for more information.
66
+
67
+ == Usage
68
+
69
+ === Setup
70
+
71
+ In order for an ActiveRecord model to be indexable and searchable, it must be
72
+ configured for search. For example:
73
+
74
+ class Post < ActiveRecord::Base
75
+ searchable do
76
+ text :title, :body
77
+ integer :blog_id
78
+ time :updated_at
79
+ string :sort_title do
80
+ title.downcase.sub(/^(an?|the) /, '')
81
+ end
82
+ end
83
+ end
84
+
85
+ See the documentation for Sunspot.setup for full details on what can go in the
86
+ configuration block.
87
+
88
+ === Indexing
89
+
90
+ By default, models are indexed whenever they are saved, and removed from the
91
+ index whenever they are destroyed. This behavior can be disabled:
92
+
93
+ class Post < ActiveRecord::Base
94
+ searchable :auto_index => false, :auto_remove => false do
95
+ # setup...
96
+ end
97
+ end
98
+
99
+ Note that <b>using the <code>:auto_remove</code> option is not recommended
100
+ </b>, as destroying an object without removing it from the index will
101
+ create an orphaned document in the index, which is a Bad Thing. Turning off
102
+ <code>:auto_index</code> is perfectly safe if you prefer to manage indexing manually
103
+ (perhaps using a background job).
104
+
105
+ If you have disabled lifecycle indexing hooks, you can invoke indexing
106
+ operations directly on your model:
107
+
108
+ post = Post.create
109
+ post.index
110
+ post.remove_from_index
111
+
112
+ === Committing
113
+
114
+ When data is changed in Solr, it is initially stored in memory and not made
115
+ available to the currently running searcher instance. Issuing a +commit+ to Solr
116
+ will cause it to write the changes to disk, and instantiate a new searcher
117
+ instance. This operation is fairly expensive, so rather than issuing a commit
118
+ every time a document is added or removed, Sunspot::Rails issues a commit at
119
+ the end of any request where data has been added to or removed from Solr. If
120
+ you need to immediately issue a commit, bang!-versions of the methods are
121
+ available:
122
+
123
+ post = Post.create
124
+ post.index!
125
+ # this is the same as...
126
+ post.index
127
+ Sunspot.commit
128
+
129
+ When writing tests outside of the context of a controller request, you will want
130
+ to use one of these two approaches.
131
+
132
+ === Searching
133
+
134
+ Do it like this:
135
+
136
+ Post.search do
137
+ with :blog_id, 1
138
+ with(:updated_at).greater_than(Time.now - 2.weeks)
139
+ order :sort_title, :asc
140
+ paginate :page => 1, :per_page => 15
141
+ end
142
+
143
+ See the documentation for <code>Sunspot.search</code> for all the options
144
+ available in the search block, and the information available in the result
145
+ block.
146
+
147
+ === Searching for IDs
148
+
149
+ In some situations, you may want to get the IDs for models returned by a search
150
+ without actually loading the models out of the database. For that, you can
151
+ call +search_ids+, using the same block format as #search. This will return an
152
+ array of IDs.
153
+
154
+
155
+ === Searching for multiple types
156
+
157
+ Sunspot is entirely agnostic about whether searches are for one or more types;
158
+ the only restriction is that columns used for restriction, ordering, etc. are
159
+ defined in the same way for all types being searched. Sunspot::Rails does not
160
+ provide any additional support for this, since there is not anything useful to
161
+ be added, so just use the interface provided by Sunspot:
162
+
163
+ Sunspot.search(Post, Comment) do
164
+ with :blog_id, 1
165
+ order :created_at, :asc
166
+ end
167
+
168
+ Be sure to check out the Sunspot documentation for all the details.
169
+
170
+ === Adding search functionality in mixins
171
+
172
+ Sunspot does not require that search setup for a given class happen all in one
173
+ place; it is perfectly acceptable to call the <code>Sunspot.setup</code> method
174
+ more than once. This capability is particularly useful for adding search
175
+ functionality in mixins. For instance, if you have a +Ratable+ module, you may
176
+ wish to add additional search fields for searchable classes that mix in that
177
+ module. For example:
178
+
179
+ module Ratable
180
+ def self.included(base)
181
+ if base.searchable?
182
+ base.searchable do
183
+ float :average_rating do
184
+ ratings.average(:value)
185
+ end
186
+ end
187
+ end
188
+ end
189
+ end
190
+
191
+ Note the use of <code>base.searchable?</code> - this ensures that only classes
192
+ that already have search enabled will have the additional configuration added.
193
+ The above pattern requires that the class be declared searchable before the
194
+ module is mixed in; other patterns (such as passing a :searchable option to an
195
+ acts_as_-style declaration) may be more flexible.
196
+
197
+ === Utility methods
198
+
199
+ If you need to completely reindex all of the instances of a given model class,
200
+ you can issue:
201
+
202
+ Post.reindex
203
+
204
+ If for some reason models get deleted from the database, but not from the index,
205
+ they will become index orphans - not a good situation. To get IDs that exist in
206
+ the index but not the database, you can use the +index_orphans+ method; to
207
+ remove those documents from the index, use +clean_index_orphans+. Note that
208
+ neither of these operations should be needed if Sunspot and Sunspot::Rails are
209
+ used as intended.
210
+
211
+ == Further Reading
212
+
213
+ Reading the {Sunspot documentation}[http://outoftime.github.com/sunspot/docs] is
214
+ highly recommended. Sunspot::Rails exists to wrap Sunspot with a Rails-friendly
215
+ API, but almost all of the functionality you use in Sunspot::Rails is
216
+ implemented in Sunspot.
217
+
218
+ Posts about Sunspot on my blog are available at http://outofti.me/tagged/sunspot
219
+
220
+ == Bugs
221
+
222
+ Please submit bug reports to
223
+ http://outoftime.lighthouseapp.com/projects/20339-sunspot
224
+
225
+ == Contributors
226
+
227
+ - Mat Brown (mat@patch.com)
228
+ - Peer Allan (peer.allan@gmail.com)
229
+ - Michael Moen (michael@underpantsgnome.com)
230
+ - Benjamin Krause (bk@benjaminkrause.com)
231
+ - Adam Salter (adam@codebright.net)
232
+ - Brandon Keepers (brandon@opensoul.org)
233
+
234
+ == License
235
+
236
+ Sunspot::Rails is distributed under the MIT License, copyright (c) 2009 Mat Brown
data/Rakefile ADDED
@@ -0,0 +1,19 @@
1
+ require 'rake'
2
+ require 'spec/rake/spectask'
3
+ require 'rake/rdoctask'
4
+
5
+ task :default => :spec
6
+
7
+ desc 'Run all specs'
8
+ Spec::Rake::SpecTask.new(:spec) do |t|
9
+ t.spec_files = FileList['spec/**/*_spec.rb']
10
+ t.spec_opts << '--color'
11
+ end
12
+
13
+ task :environment do
14
+ ENV['RAILS_ROOT'] ||= File.join(File.dirname(__FILE__), 'spec', 'mock_app')
15
+ ENV['RAILS_ENV'] ||= 'test'
16
+ require File.expand_path(File.join(ENV['RAILS_ROOT'], 'config', 'environment.rb'))
17
+ end
18
+
19
+ FileList['dev_tasks/*.rake', 'lib/sunspot/rails/tasks.rb'].each { |file| load(file) }
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :minor: 10
3
+ :patch: 6
4
+ :major: 0
@@ -0,0 +1,28 @@
1
+ begin
2
+ gem 'technicalpickles-jeweler', '~> 1.0'
3
+ require 'jeweler'
4
+ Jeweler::Tasks.new do |s|
5
+ s.name = 'sunspot_rails'
6
+ s.summary = 'Rails integration for the Sunspot Solr search library'
7
+ s.email = 'mat@patch.com'
8
+ s.homepage = 'http://github.com/outoftime/sunspot_rails'
9
+ s.description = 'Rails integration for the Sunspot Solr search library'
10
+ s.authors = ['Mat Brown', 'Peer Allan', 'Michael Moen', 'Benjamin Krause', 'Adam Salter', 'Brandon Keepers']
11
+ s.rubyforge_project = 'sunspot'
12
+ s.files = FileList['[A-Z]*',
13
+ '{lib,tasks,dev_tasks}/**/*',
14
+ 'generators/**/*',
15
+ 'install.rb',
16
+ 'MIT-LICENSE',
17
+ 'rails/*',
18
+ 'spec/*.rb',
19
+ 'spec/mock_app/{app,lib,db,vendor,config}/**/*',
20
+ 'spec/mock_app/{tmp,log,solr}']
21
+ s.add_dependency 'escape', '>= 0.0.4'
22
+ s.add_dependency 'outoftime-sunspot', '>= 0.8.2'
23
+ s.add_development_dependency 'rspec', '~> 1.2'
24
+ s.add_development_dependency 'rspec-rails', '~> 1.2'
25
+ s.add_development_dependency 'ruby-debug', '~> 0.10'
26
+ s.add_development_dependency 'technicalpickles-jeweler', '~> 1.0'
27
+ end
28
+ end
@@ -0,0 +1,7 @@
1
+ require 'rake/rdoctask'
2
+
3
+ Rake::RDocTask.new(:doc) do |rdoc|
4
+ rdoc.main = 'README.rdoc'
5
+ rdoc.rdoc_files.include('README.rdoc', 'lib/sunspot/rails/**/*.rb')
6
+ rdoc.rdoc_dir = 'doc'
7
+ end
@@ -0,0 +1,4 @@
1
+ namespace :release do
2
+ desc 'Release gem on RubyForge and GitHub'
3
+ task :all => [:release, :"rubyforge:release:gem"]
4
+ end
@@ -0,0 +1,4 @@
1
+ desc 'Show all TODO and related tags'
2
+ task :todo do
3
+ FileList['lib/**/*.rb'].egrep(/#.*(TODO|FIXME|XXX)/)
4
+ end
@@ -0,0 +1,9 @@
1
+ class SunspotGenerator < Rails::Generator::Base
2
+
3
+ def manifest
4
+ record do |m|
5
+ m.template 'sunspot.yml', 'config/sunspot.yml'
6
+ end
7
+ end
8
+
9
+ end
@@ -0,0 +1,19 @@
1
+ common: &common
2
+ solr:
3
+ hostname: localhost
4
+ port: 8983
5
+
6
+ production:
7
+ <<: *common
8
+ solr:
9
+ path: /solr/myindex
10
+
11
+ development:
12
+ <<: *common
13
+ solr:
14
+ port: 8982
15
+
16
+ test:
17
+ <<: *common
18
+ solr:
19
+ port: 8981
data/install.rb ADDED
@@ -0,0 +1 @@
1
+ # Install hook code here
@@ -0,0 +1,54 @@
1
+ module Sunspot #:nodoc:
2
+ module Rails #:nodoc:
3
+ #
4
+ # This module provides Sunspot Adapter implementations for ActiveRecord
5
+ # models.
6
+ #
7
+ module Adapters
8
+ class ActiveRecordInstanceAdapter < Sunspot::Adapters::InstanceAdapter
9
+ #
10
+ # Return the primary key for the adapted instance
11
+ #
12
+ # ==== Returns
13
+ #
14
+ # Integer:: Database ID of model
15
+ #
16
+ def id
17
+ @instance.id
18
+ end
19
+ end
20
+
21
+ class ActiveRecordDataAccessor < Sunspot::Adapters::DataAccessor
22
+ #
23
+ # Get one ActiveRecord instance out of the database by ID
24
+ #
25
+ # ==== Parameters
26
+ #
27
+ # id<String>:: Database ID of model to retreive
28
+ #
29
+ # ==== Returns
30
+ #
31
+ # ActiveRecord::Base:: ActiveRecord model
32
+ #
33
+ def load(id)
34
+ @clazz.find(id.to_i)
35
+ end
36
+
37
+ #
38
+ # Get a collection of ActiveRecord instances out of the database by ID
39
+ #
40
+ # ==== Parameters
41
+ #
42
+ # ids<Array>:: Database IDs of models to retrieve
43
+ #
44
+ # ==== Returns
45
+ #
46
+ # Array:: Collection of ActiveRecord models
47
+ #
48
+ def load_all(ids)
49
+ @clazz.find(ids.map { |id| id.to_i })
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,169 @@
1
+ module Sunspot #:nodoc:
2
+ module Rails #:nodoc:
3
+ #
4
+ # Sunspot::Rails is configured via the config/sunspot.yml file, which
5
+ # contains properties keyed by environment name. A sample sunspot.yml file
6
+ # would look like:
7
+ #
8
+ # development:
9
+ # solr:
10
+ # hostname: localhost
11
+ # port: 8982
12
+ # test:
13
+ # solr:
14
+ # hostname: localhost
15
+ # port: 8983
16
+ #
17
+ # production:
18
+ # solr:
19
+ # hostname: localhost
20
+ # port: 8983
21
+ # path: /solr/myindex
22
+ #
23
+ # Sunspot::Rails uses the configuration to set up the Solr connection, as
24
+ # well as for starting Solr with the appropriate port using the
25
+ # <code>rake sunspot:solr:start</code> task.
26
+ #
27
+ class Configuration
28
+ attr_writer :user_configuration
29
+ #
30
+ # The host name at which to connect to Solr. Default 'localhost'.
31
+ #
32
+ # ==== Returns
33
+ #
34
+ # String:: host name
35
+ #
36
+ def hostname
37
+ @hostname ||= (user_configuration_from_key('solr', 'hostname') || 'localhost')
38
+ end
39
+
40
+ #
41
+ # The port at which to connect to Solr. Default 8983.
42
+ #
43
+ # ==== Returns
44
+ #
45
+ # Integer:: port
46
+ #
47
+ def port
48
+ @port ||= (user_configuration_from_key('solr', 'port') || 8983).to_i
49
+ end
50
+
51
+ #
52
+ # The url path to the Solr servlet (useful if you are running multicore).
53
+ # Default '/solr'.
54
+ #
55
+ # ==== Returns
56
+ #
57
+ # String:: path
58
+ #
59
+ def path
60
+ @path ||= (user_configuration_from_key('solr', 'path') || '/solr')
61
+ end
62
+
63
+ #
64
+ # Should the solr index receive a commit after each http-request.
65
+ # Default true
66
+ #
67
+ # ==== Returns
68
+ #
69
+ # Boolean:: bool
70
+ #
71
+
72
+ def auto_commit_after_request?
73
+ @auto_commit_after_request ||=
74
+ user_configuration_from_key('auto_commit_after_request') != false
75
+ end
76
+
77
+ #
78
+ # The path to the Solr indexes. (Used by the rake tasks).
79
+ # Default RAILS_ROOT + '/solr/data/' + ENVIRONMENT
80
+ #
81
+ # ==== Returns
82
+ #
83
+ # String:: path
84
+ #
85
+ def data_path
86
+ @data_path ||=
87
+ if user_configuration.has_key?('solr')
88
+ "#{user_configuration['solr']['data_path'] || File.join(::Rails.root, 'solr', 'data', ::Rails.env)}"
89
+ end
90
+ end
91
+
92
+ #
93
+ # The path to the Solr pids
94
+ # Default RAILS_ROOT + '/solr/pids/' + ENVIRONMENT
95
+ #
96
+ # ==== Returns
97
+ #
98
+ # String:: path
99
+ #
100
+ def pid_path
101
+ @pids_path ||=
102
+ if user_configuration.has_key?('solr')
103
+ "#{user_configuration['solr']['pid_path'] || File.join(::Rails.root, 'solr', 'pids', ::Rails.env)}"
104
+ end
105
+ end
106
+
107
+ #
108
+ # The path to the Solr home directory
109
+ # Default nil (runs the solr with sunspot default settings).
110
+ #
111
+ # If you have a custom solr conf directory,
112
+ # change this to the directory above your solr conf files
113
+ #
114
+ # e.g. conf files in RAILS_ROOT/solr/conf
115
+ # solr_home: RAILS_ROOT/solr
116
+ #
117
+ # ==== Returns
118
+ #
119
+ # String:: path
120
+ #
121
+ def solr_home
122
+ @solr_home ||=
123
+ if user_configuration.has_key?('solr')
124
+ if user_configuration['solr']['solr_home'].present?
125
+ user_configuration['solr']['solr_home']
126
+ elsif %w(solrconfig schema).all? { |file| File.exist?(File.join(::Rails.root, 'solr', 'conf', "#{file}.xml")) }
127
+ File.join(::Rails.root, 'solr')
128
+ end
129
+ end
130
+ end
131
+
132
+ private
133
+
134
+ #
135
+ # return a specific key from the user configuration in config/sunspot.yml
136
+ #
137
+ # ==== Returns
138
+ #
139
+ #
140
+ def user_configuration_from_key( *keys )
141
+ keys.inject(user_configuration) do |hash, key|
142
+ hash[key] if hash
143
+ end
144
+ end
145
+
146
+ #
147
+ # Memoized hash of configuration options for the current Rails environment
148
+ # as specified in config/sunspot.yml
149
+ #
150
+ # ==== Returns
151
+ #
152
+ # Hash:: configuration options for current environment
153
+ #
154
+ def user_configuration
155
+ @user_configuration ||=
156
+ begin
157
+ path = File.join(::Rails.root, 'config', 'sunspot.yml')
158
+ if File.exist?(path)
159
+ File.open(path) do |file|
160
+ YAML.load(file)[::Rails.env]
161
+ end
162
+ else
163
+ {}
164
+ end
165
+ end
166
+ end
167
+ end
168
+ end
169
+ end
@@ -0,0 +1,18 @@
1
+ module Sunspot #:nodoc:
2
+ module Rails #:nodoc:
3
+ #
4
+ # This module adds an after_filter to ActionController::Base that commits
5
+ # the Sunspot session if any documents have been added, changed, or removed
6
+ # in the course of the request.
7
+ #
8
+ module RequestLifecycle
9
+ class <<self
10
+ def included(base) #:nodoc:
11
+ base.after_filter do
12
+ Sunspot.commit_if_dirty if Sunspot::Rails.configuration.auto_commit_after_request?
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end