gojee-sunspot-rails 2.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. data/.gitignore +7 -0
  2. data/.rspec +1 -0
  3. data/History.txt +74 -0
  4. data/LICENSE +18 -0
  5. data/MIT-LICENSE +20 -0
  6. data/README.rdoc +265 -0
  7. data/Rakefile +17 -0
  8. data/TODO +8 -0
  9. data/dev_tasks/rdoc.rake +24 -0
  10. data/dev_tasks/release.rake +4 -0
  11. data/dev_tasks/spec.rake +107 -0
  12. data/dev_tasks/todo.rake +4 -0
  13. data/gemfiles/rails-2.3.14 +12 -0
  14. data/gemfiles/rails-3.0.11 +12 -0
  15. data/gemfiles/rails-3.1.3 +12 -0
  16. data/gemfiles/rails-3.2.1 +12 -0
  17. data/generators/sunspot/sunspot_generator.rb +9 -0
  18. data/generators/sunspot/templates/sunspot.yml +20 -0
  19. data/install.rb +1 -0
  20. data/lib/generators/sunspot_rails.rb +9 -0
  21. data/lib/generators/sunspot_rails/install/install_generator.rb +13 -0
  22. data/lib/generators/sunspot_rails/install/templates/config/sunspot.yml +19 -0
  23. data/lib/sunspot/rails.rb +69 -0
  24. data/lib/sunspot/rails/adapters.rb +83 -0
  25. data/lib/sunspot/rails/configuration.rb +376 -0
  26. data/lib/sunspot/rails/init.rb +5 -0
  27. data/lib/sunspot/rails/log_subscriber.rb +33 -0
  28. data/lib/sunspot/rails/railtie.rb +36 -0
  29. data/lib/sunspot/rails/railties/controller_runtime.rb +36 -0
  30. data/lib/sunspot/rails/request_lifecycle.rb +36 -0
  31. data/lib/sunspot/rails/searchable.rb +491 -0
  32. data/lib/sunspot/rails/server.rb +114 -0
  33. data/lib/sunspot/rails/solr_instrumentation.rb +19 -0
  34. data/lib/sunspot/rails/solr_logging.rb +62 -0
  35. data/lib/sunspot/rails/spec_helper.rb +26 -0
  36. data/lib/sunspot/rails/stub_session_proxy.rb +142 -0
  37. data/lib/sunspot/rails/tasks.rb +84 -0
  38. data/lib/sunspot_rails.rb +12 -0
  39. data/spec/configuration_spec.rb +209 -0
  40. data/spec/model_lifecycle_spec.rb +63 -0
  41. data/spec/model_spec.rb +601 -0
  42. data/spec/rails_template/app/controllers/application_controller.rb +10 -0
  43. data/spec/rails_template/app/controllers/posts_controller.rb +6 -0
  44. data/spec/rails_template/app/models/author.rb +8 -0
  45. data/spec/rails_template/app/models/blog.rb +12 -0
  46. data/spec/rails_template/app/models/location.rb +2 -0
  47. data/spec/rails_template/app/models/photo_post.rb +2 -0
  48. data/spec/rails_template/app/models/post.rb +11 -0
  49. data/spec/rails_template/app/models/post_with_auto.rb +10 -0
  50. data/spec/rails_template/app/models/post_with_default_scope.rb +11 -0
  51. data/spec/rails_template/config/boot.rb +127 -0
  52. data/spec/rails_template/config/preinitializer.rb +22 -0
  53. data/spec/rails_template/config/routes.rb +9 -0
  54. data/spec/rails_template/config/sunspot.yml +24 -0
  55. data/spec/rails_template/db/schema.rb +27 -0
  56. data/spec/request_lifecycle_spec.rb +61 -0
  57. data/spec/schema.rb +27 -0
  58. data/spec/searchable_spec.rb +12 -0
  59. data/spec/server_spec.rb +33 -0
  60. data/spec/session_spec.rb +57 -0
  61. data/spec/shared_examples/indexed_after_save.rb +8 -0
  62. data/spec/shared_examples/not_indexed_after_save.rb +8 -0
  63. data/spec/spec_helper.rb +48 -0
  64. data/spec/stub_session_proxy_spec.rb +122 -0
  65. data/sunspot_rails.gemspec +43 -0
  66. metadata +229 -0
@@ -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,107 @@
1
+ require 'fileutils'
2
+
3
+ namespace :spec do
4
+ def rails_app_path(version)
5
+ File.join(File.dirname(__FILE__), "..", "tmp", "rails_#{version.gsub(".", "_")}_app")
6
+ end
7
+
8
+ def gemfile_path(version)
9
+ File.join(File.dirname(__FILE__), "..", "gemfiles", "rails-#{version}")
10
+ end
11
+
12
+ def vendor_path(version)
13
+ File.expand_path("vendor/bundle", rails_app_path(version))
14
+ end
15
+
16
+ def rails_template_path
17
+ File.join(File.dirname(__FILE__), "..", "spec", "rails_template")
18
+ end
19
+
20
+ task :run_with_rails => [:set_gemfile, :generate_rails_app, :setup_rails_app, :run]
21
+
22
+ task :set_gemfile do
23
+ version = ENV['VERSION']
24
+
25
+ ENV['BUNDLE_PATH'] = vendor_path(version)
26
+ ENV['BUNDLE_GEMFILE'] = gemfile_path(version)
27
+
28
+ unless File.exist?(ENV['BUNDLE_PATH'])
29
+ puts "Installing gems for Rails #{version} (this will only be done once)..."
30
+ system("bundle install #{ENV['BUNDLE_ARGS']}") || exit(1)
31
+ end
32
+ end
33
+
34
+ task :generate_rails_app do
35
+ version = ENV['VERSION']
36
+ app_path = rails_app_path(version)
37
+
38
+ unless File.exist?(File.expand_path("config/environment.rb", app_path))
39
+ rails_cmd = "bundle exec rails _#{version}_"
40
+
41
+ puts "Generating Rails #{version} application..."
42
+ if version.start_with?("2")
43
+ system("#{rails_cmd} \"#{app_path}\" --force") || exit(1)
44
+ elsif version.start_with?("3")
45
+ system("#{rails_cmd} new \"#{app_path}\" --force --skip-git --skip-javascript --skip-gemfile --skip-sprockets") || exit(1)
46
+ end
47
+ end
48
+ end
49
+
50
+ task :setup_rails_app do
51
+ version = ENV['VERSION']
52
+ app_path = rails_app_path(version)
53
+
54
+ FileUtils.cp_r File.join(rails_template_path, "."), app_path
55
+ end
56
+
57
+ task :run do
58
+ version = ENV['VERSION']
59
+
60
+ ENV['BUNDLE_GEMFILE'] = gemfile_path(version)
61
+ ENV['RAILS_ROOT'] = rails_app_path(version)
62
+
63
+ spec_command = if version.start_with?("2")
64
+ "spec"
65
+ elsif version.start_with?("3")
66
+ "rspec"
67
+ end
68
+
69
+ system "bundle exec #{spec_command} #{ENV['SPEC'] || 'spec/*_spec.rb'} --color"
70
+ end
71
+ end
72
+
73
+ def rails_all_versions
74
+ versions = []
75
+ Dir.glob(File.join(File.dirname(__FILE__), "..", "gemfiles", "rails-*")).each do |gemfile|
76
+ if !gemfile.end_with?(".lock") && gemfile =~ /rails-([0-9.]+)/
77
+ versions << $1
78
+ end
79
+ end
80
+
81
+ versions
82
+ end
83
+
84
+ def reenable_spec_tasks
85
+ Rake::Task.tasks.each do |task|
86
+ if task.name =~ /spec:/
87
+ task.reenable
88
+ end
89
+ end
90
+ end
91
+
92
+ desc 'Run spec suite in all Rails versions'
93
+ task :spec do
94
+ versions = if ENV['RAILS']
95
+ ENV['RAILS'].split(",")
96
+ else
97
+ rails_all_versions
98
+ end
99
+
100
+ versions.each do |version|
101
+ puts "Running specs against Rails #{version}..."
102
+
103
+ ENV['VERSION'] = version
104
+ reenable_spec_tasks
105
+ Rake::Task['spec:run_with_rails'].invoke
106
+ end
107
+ 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,12 @@
1
+ source :rubygems
2
+
3
+ gem 'rails', '2.3.14'
4
+ gem 'sqlite3-ruby', '~> 1.3.1'
5
+
6
+ gem 'sunspot', :path => File.expand_path('../../../sunspot', __FILE__)
7
+ gem 'sunspot_solr', :path => File.expand_path('../../../sunspot_solr', __FILE__)
8
+ gem 'sunspot_rails', :path => File.expand_path('../..', __FILE__)
9
+
10
+ group :test do
11
+ gem 'rspec-rails', '~> 1.3.4'
12
+ end
@@ -0,0 +1,12 @@
1
+ source :rubygems
2
+
3
+ gem 'rails', '3.0.11'
4
+ gem 'sqlite3-ruby', '~> 1.3.1'
5
+
6
+ gem 'sunspot', :path => File.expand_path('../../../sunspot', __FILE__)
7
+ gem 'sunspot_solr', :path => File.expand_path('../../../sunspot_solr', __FILE__)
8
+ gem 'sunspot_rails', :path => File.expand_path('../..', __FILE__)
9
+
10
+ group :test do
11
+ gem 'rspec-rails', '~> 2.8.1'
12
+ end
@@ -0,0 +1,12 @@
1
+ source :rubygems
2
+
3
+ gem 'rails', '3.1.3'
4
+ gem 'sqlite3-ruby', '~> 1.3.1'
5
+
6
+ gem 'sunspot', :path => File.expand_path('../../../sunspot', __FILE__)
7
+ gem 'sunspot_solr', :path => File.expand_path('../../../sunspot_solr', __FILE__)
8
+ gem 'sunspot_rails', :path => File.expand_path('../..', __FILE__)
9
+
10
+ group :test do
11
+ gem 'rspec-rails', '~> 2.8.1'
12
+ end
@@ -0,0 +1,12 @@
1
+ source :rubygems
2
+
3
+ gem 'rails', '3.2.1'
4
+ gem 'sqlite3-ruby', '~> 1.3.1'
5
+
6
+ gem 'sunspot', :path => File.expand_path('../../../sunspot', __FILE__)
7
+ gem 'sunspot_solr', :path => File.expand_path('../../../sunspot_solr', __FILE__)
8
+ gem 'sunspot_rails', :path => File.expand_path('../..', __FILE__)
9
+
10
+ group :test do
11
+ gem 'rspec-rails', '~> 2.8.1'
12
+ 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,20 @@
1
+ production:
2
+ solr:
3
+ hostname: localhost
4
+ port: 8983
5
+ log_level: WARNING
6
+ # read_timeout: 2
7
+ # open_timeout: 0.5
8
+
9
+ development:
10
+ solr:
11
+ hostname: localhost
12
+ port: 8982
13
+ log_level: INFO
14
+
15
+ test:
16
+ solr:
17
+ hostname: localhost
18
+ port: 8981
19
+ log_level: WARNING
20
+
data/install.rb ADDED
@@ -0,0 +1 @@
1
+ # Install hook code here
@@ -0,0 +1,9 @@
1
+ module SunspotRails
2
+ module Generators
3
+ class Base < Rails::Generators::NamedBase
4
+ def self.source_root
5
+ @_sunspot_rails_source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'sunspot_rails', generator_name, 'templates'))
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,13 @@
1
+ module SunspotRails
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+ def self.source_root
5
+ @source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
6
+ end
7
+ def copy_config_file
8
+ template 'config/sunspot.yml'
9
+ end
10
+ end
11
+ end
12
+ end
13
+
@@ -0,0 +1,19 @@
1
+ production:
2
+ solr:
3
+ hostname: localhost
4
+ port: 8983
5
+ log_level: WARNING
6
+ # read_timeout: 2
7
+ # open_timeout: 0.5
8
+
9
+ development:
10
+ solr:
11
+ hostname: localhost
12
+ port: 8982
13
+ log_level: INFO
14
+
15
+ test:
16
+ solr:
17
+ hostname: localhost
18
+ port: 8981
19
+ log_level: WARNING
@@ -0,0 +1,69 @@
1
+ require 'sunspot'
2
+ require File.join(File.dirname(__FILE__), 'rails', 'configuration')
3
+ require File.join(File.dirname(__FILE__), 'rails', 'adapters')
4
+ require File.join(File.dirname(__FILE__), 'rails', 'request_lifecycle')
5
+ require File.join(File.dirname(__FILE__), 'rails', 'searchable')
6
+
7
+ module Sunspot #:nodoc:
8
+ module Rails #:nodoc:
9
+ autoload :SolrInstrumentation, File.join(File.dirname(__FILE__), 'rails', 'solr_instrumentation')
10
+ autoload :StubSessionProxy, File.join(File.dirname(__FILE__), 'rails', 'stub_session_proxy')
11
+ begin
12
+ require 'sunspot_solr'
13
+ autoload :Server, File.join(File.dirname(__FILE__), 'rails', 'server')
14
+ rescue LoadError => e
15
+ # We're fine
16
+ end
17
+
18
+ class <<self
19
+ attr_writer :configuration
20
+
21
+ def configuration
22
+ @configuration ||= Sunspot::Rails::Configuration.new
23
+ end
24
+
25
+ def reset
26
+ @configuration = nil
27
+ end
28
+
29
+ def build_session(configuration = self.configuration)
30
+ if configuration.disabled?
31
+ StubSessionProxy.new(Sunspot.session)
32
+ elsif configuration.has_master?
33
+ SessionProxy::MasterSlaveSessionProxy.new(
34
+ SessionProxy::ThreadLocalSessionProxy.new(master_config(configuration)),
35
+ SessionProxy::ThreadLocalSessionProxy.new(slave_config(configuration))
36
+ )
37
+ else
38
+ SessionProxy::ThreadLocalSessionProxy.new(slave_config(configuration))
39
+ end
40
+ end
41
+
42
+ private
43
+
44
+ def master_config(sunspot_rails_configuration)
45
+ config = Sunspot::Configuration.build
46
+ config.solr.url = URI::HTTP.build(
47
+ :host => sunspot_rails_configuration.master_hostname,
48
+ :port => sunspot_rails_configuration.master_port,
49
+ :path => sunspot_rails_configuration.master_path
50
+ ).to_s
51
+ config.solr.read_timeout = sunspot_rails_configuration.read_timeout
52
+ config.solr.open_timeout = sunspot_rails_configuration.open_timeout
53
+ config
54
+ end
55
+
56
+ def slave_config(sunspot_rails_configuration)
57
+ config = Sunspot::Configuration.build
58
+ config.solr.url = URI::HTTP.build(
59
+ :host => sunspot_rails_configuration.hostname,
60
+ :port => sunspot_rails_configuration.port,
61
+ :path => sunspot_rails_configuration.path
62
+ ).to_s
63
+ config.solr.read_timeout = sunspot_rails_configuration.read_timeout
64
+ config.solr.open_timeout = sunspot_rails_configuration.open_timeout
65
+ config
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,83 @@
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
+ # options for the find
23
+ attr_accessor :include, :select
24
+
25
+ #
26
+ # Set the fields to select from the database. This will be passed
27
+ # to ActiveRecord.
28
+ #
29
+ # ==== Parameters
30
+ #
31
+ # value<Mixed>:: String of comma-separated columns or array of columns
32
+ #
33
+ def select=(value)
34
+ value = value.join(', ') if value.respond_to?(:join)
35
+ @select = value
36
+ end
37
+
38
+ #
39
+ # Get one ActiveRecord instance out of the database by ID
40
+ #
41
+ # ==== Parameters
42
+ #
43
+ # id<String>:: Database ID of model to retreive
44
+ #
45
+ # ==== Returns
46
+ #
47
+ # ActiveRecord::Base:: ActiveRecord model
48
+ #
49
+ def load(id)
50
+ @clazz.first(options_for_find.merge(
51
+ :conditions => { @clazz.primary_key => id}
52
+ ))
53
+ end
54
+
55
+ #
56
+ # Get a collection of ActiveRecord instances out of the database by ID
57
+ #
58
+ # ==== Parameters
59
+ #
60
+ # ids<Array>:: Database IDs of models to retrieve
61
+ #
62
+ # ==== Returns
63
+ #
64
+ # Array:: Collection of ActiveRecord models
65
+ #
66
+ def load_all(ids)
67
+ @clazz.all(options_for_find.merge(
68
+ :conditions => { @clazz.primary_key => ids.map { |id| id }}
69
+ ))
70
+ end
71
+
72
+ private
73
+
74
+ def options_for_find
75
+ options = {}
76
+ options[:include] = @include unless @include.blank?
77
+ options[:select] = @select unless @select.blank?
78
+ options
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,376 @@
1
+ require 'erb'
2
+
3
+ module Sunspot #:nodoc:
4
+ module Rails #:nodoc:
5
+ #
6
+ # Sunspot::Rails is configured via the config/sunspot.yml file, which
7
+ # contains properties keyed by environment name. A sample sunspot.yml file
8
+ # would look like:
9
+ #
10
+ # development:
11
+ # solr:
12
+ # hostname: localhost
13
+ # port: 8982
14
+ # min_memory: 512M
15
+ # max_memory: 1G
16
+ # solr_jar: /some/path/solr15/start.jar
17
+ # bind_address: 0.0.0.0
18
+ # disabled: false
19
+ # test:
20
+ # solr:
21
+ # hostname: localhost
22
+ # port: 8983
23
+ # log_level: OFF
24
+ # open_timeout: 0.5
25
+ # read_timeout: 2
26
+ # production:
27
+ # solr:
28
+ # hostname: localhost
29
+ # port: 8983
30
+ # path: /solr/myindex
31
+ # log_level: WARNING
32
+ # solr_home: /some/path
33
+ # open_timeout: 0.5
34
+ # read_timeout: 2
35
+ # master_solr:
36
+ # hostname: localhost
37
+ # port: 8982
38
+ # path: /solr
39
+ # auto_commit_after_request: true
40
+ #
41
+ # Sunspot::Rails uses the configuration to set up the Solr connection, as
42
+ # well as for starting Solr with the appropriate port using the
43
+ # <code>rake sunspot:solr:start</code> task.
44
+ #
45
+ # If the <code>master_solr</code> configuration is present, Sunspot will use
46
+ # the Solr instance specified here for all write operations, and the Solr
47
+ # configured under <code>solr</code> for all read operations.
48
+ #
49
+ class Configuration
50
+ attr_writer :user_configuration
51
+ #
52
+ # The host name at which to connect to Solr. Default 'localhost'.
53
+ #
54
+ # ==== Returns
55
+ #
56
+ # String:: host name
57
+ #
58
+ def hostname
59
+ unless defined?(@hostname)
60
+ @hostname = solr_url.host if solr_url
61
+ @hostname ||= user_configuration_from_key('solr', 'hostname')
62
+ @hostname ||= default_hostname
63
+ end
64
+ @hostname
65
+ end
66
+
67
+ #
68
+ # The port at which to connect to Solr.
69
+ # Defaults to 8981 in test, 8982 in development and 8983 in production.
70
+ #
71
+ # ==== Returns
72
+ #
73
+ # Integer:: port
74
+ #
75
+ def port
76
+ unless defined?(@port)
77
+ @port = solr_url.port if solr_url
78
+ @port ||= user_configuration_from_key('solr', 'port')
79
+ @port ||= default_port
80
+ @port = @port.to_i
81
+ end
82
+ @port
83
+ end
84
+
85
+ #
86
+ # The url path to the Solr servlet (useful if you are running multicore).
87
+ # Default '/solr'.
88
+ #
89
+ # ==== Returns
90
+ #
91
+ # String:: path
92
+ #
93
+ def path
94
+ unless defined?(@path)
95
+ @path = solr_url.path if solr_url
96
+ @path ||= user_configuration_from_key('solr', 'path')
97
+ @path ||= default_path
98
+ end
99
+ @path
100
+ end
101
+
102
+ #
103
+ # The host name at which to connect to the master Solr instance. Defaults
104
+ # to the 'hostname' configuration option.
105
+ #
106
+ # ==== Returns
107
+ #
108
+ # String:: host name
109
+ #
110
+ def master_hostname
111
+ @master_hostname ||= (user_configuration_from_key('master_solr', 'hostname') || hostname)
112
+ end
113
+
114
+ #
115
+ # The port at which to connect to the master Solr instance. Defaults to
116
+ # the 'port' configuration option.
117
+ #
118
+ # ==== Returns
119
+ #
120
+ # Integer:: port
121
+ #
122
+ def master_port
123
+ @master_port ||= (user_configuration_from_key('master_solr', 'port') || port).to_i
124
+ end
125
+
126
+ #
127
+ # The path to the master Solr servlet (useful if you are running multicore).
128
+ # Defaults to the value of the 'path' configuration option.
129
+ #
130
+ # ==== Returns
131
+ #
132
+ # String:: path
133
+ #
134
+ def master_path
135
+ @master_path ||= (user_configuration_from_key('master_solr', 'path') || path)
136
+ end
137
+
138
+ #
139
+ # True if there is a master Solr instance configured, otherwise false.
140
+ #
141
+ # ==== Returns
142
+ #
143
+ # Boolean:: bool
144
+ #
145
+ def has_master?
146
+ @has_master = !!user_configuration_from_key('master_solr')
147
+ end
148
+
149
+ #
150
+ # The default log_level that should be passed to solr. You can
151
+ # change the individual log_levels in the solr admin interface.
152
+ # Default 'INFO'.
153
+ #
154
+ # ==== Returns
155
+ #
156
+ # String:: log_level
157
+ #
158
+ def log_level
159
+ @log_level ||= (user_configuration_from_key('solr', 'log_level') || 'INFO')
160
+ end
161
+
162
+ #
163
+ # Should the solr index receive a commit after each http-request.
164
+ # Default true
165
+ #
166
+ # ==== Returns
167
+ #
168
+ # Boolean: auto_commit_after_request?
169
+ #
170
+ def auto_commit_after_request?
171
+ @auto_commit_after_request ||=
172
+ user_configuration_from_key('auto_commit_after_request') != false
173
+ end
174
+
175
+ #
176
+ # As for #auto_commit_after_request? but only for deletes
177
+ # Default false
178
+ #
179
+ # ==== Returns
180
+ #
181
+ # Boolean: auto_commit_after_delete_request?
182
+ #
183
+ def auto_commit_after_delete_request?
184
+ @auto_commit_after_delete_request ||=
185
+ (user_configuration_from_key('auto_commit_after_delete_request') || false)
186
+ end
187
+
188
+
189
+ #
190
+ # The log directory for solr logfiles
191
+ #
192
+ # ==== Returns
193
+ #
194
+ # String:: log_dir
195
+ #
196
+ def log_file
197
+ @log_file ||= (user_configuration_from_key('solr', 'log_file') || default_log_file_location )
198
+ end
199
+
200
+ def data_path
201
+ @data_path ||= user_configuration_from_key('solr', 'data_path') || File.join(::Rails.root, 'solr', 'data', ::Rails.env)
202
+ end
203
+
204
+ def pid_dir
205
+ @pid_dir ||= user_configuration_from_key('solr', 'pid_dir') || File.join(::Rails.root, 'solr', 'pids', ::Rails.env)
206
+ end
207
+
208
+
209
+ #
210
+ # The solr home directory. Sunspot::Rails expects this directory
211
+ # to contain a config, data and pids directory. See
212
+ # Sunspot::Rails::Server.bootstrap for more information.
213
+ #
214
+ # ==== Returns
215
+ #
216
+ # String:: solr_home
217
+ #
218
+ def solr_home
219
+ @solr_home ||=
220
+ if user_configuration_from_key('solr', 'solr_home')
221
+ user_configuration_from_key('solr', 'solr_home')
222
+ else
223
+ File.join(::Rails.root, 'solr')
224
+ end
225
+ end
226
+
227
+ #
228
+ # Solr start jar
229
+ #
230
+ def solr_jar
231
+ @solr_jar ||= user_configuration_from_key('solr', 'solr_jar')
232
+ end
233
+
234
+ #
235
+ # Minimum java heap size for Solr instance
236
+ #
237
+ def min_memory
238
+ @min_memory ||= user_configuration_from_key('solr', 'min_memory')
239
+ end
240
+
241
+ #
242
+ # Maximum java heap size for Solr instance
243
+ #
244
+ def max_memory
245
+ @max_memory ||= user_configuration_from_key('solr', 'max_memory')
246
+ end
247
+
248
+ #
249
+ # Interface on which to run Solr
250
+ #
251
+ def bind_address
252
+ @bind_address ||= user_configuration_from_key('solr', 'bind_address')
253
+ end
254
+
255
+ def multicore
256
+ @multicore ||= !!user_configuration_from_key('solr', 'multicore')
257
+ end
258
+
259
+ def read_timeout
260
+ @read_timeout ||= user_configuration_from_key('solr', 'read_timeout')
261
+ end
262
+
263
+ def open_timeout
264
+ @open_timeout ||= user_configuration_from_key('solr', 'open_timeout')
265
+ end
266
+
267
+ #
268
+ # Whether or not to disable Solr.
269
+ # Defaults to false.
270
+ #
271
+ def disabled?
272
+ @disabled ||= (user_configuration_from_key('disabled') || false)
273
+ end
274
+
275
+ private
276
+
277
+ #
278
+ # Logging in rails_root/log as solr_<environment>.log as a
279
+ # default.
280
+ #
281
+ # ===== Returns
282
+ #
283
+ # String:: default_log_file_location
284
+ #
285
+ def default_log_file_location
286
+ File.join(::Rails.root, 'log', "solr_" + ::Rails.env + ".log")
287
+ end
288
+
289
+ #
290
+ # return a specific key from the user configuration in config/sunspot.yml
291
+ #
292
+ # ==== Returns
293
+ #
294
+ # Mixed:: requested_key or nil
295
+ #
296
+ def user_configuration_from_key( *keys )
297
+ keys.inject(user_configuration) do |hash, key|
298
+ hash[key] if hash
299
+ end
300
+ end
301
+
302
+ #
303
+ # Memoized hash of configuration options for the current Rails environment
304
+ # as specified in config/sunspot.yml
305
+ #
306
+ # ==== Returns
307
+ #
308
+ # Hash:: configuration options for current environment
309
+ #
310
+ def user_configuration
311
+ @user_configuration ||=
312
+ begin
313
+ path = File.join(::Rails.root, 'config', 'sunspot.yml')
314
+ if File.exist?(path)
315
+ File.open(path) do |file|
316
+ processed = ERB.new(file.read).result
317
+ YAML.load(processed)[::Rails.env]
318
+ end
319
+ else
320
+ {}
321
+ end
322
+ end
323
+ end
324
+
325
+ protected
326
+
327
+ #
328
+ # When a specific hostname, port and path aren't provided in the
329
+ # sunspot.yml file, look for a key named 'url', then check the
330
+ # environment, then fall back to a sensible localhost default.
331
+ #
332
+
333
+ def solr_url
334
+ if ENV['SOLR_URL'] || ENV['WEBSOLR_URL']
335
+ URI.parse(ENV['SOLR_URL'] || ENV['WEBSOLR_URL'])
336
+ end
337
+ end
338
+
339
+ def default_hostname
340
+ 'localhost'
341
+ end
342
+
343
+ def default_port
344
+ { 'test' => 8981,
345
+ 'development' => 8982,
346
+ 'production' => 8983
347
+ }[::Rails.env] || 8983
348
+ end
349
+
350
+ def default_path
351
+ '/solr'
352
+ end
353
+
354
+ end
355
+
356
+ class ExtendedConfiguration < Sunspot::Rails::Configuration
357
+ def initialize( configuration_hash )
358
+ @supplied_configuration = configuration_hash
359
+ end
360
+ protected
361
+ def solr_url
362
+ unless @cached_solr_url
363
+ @cached_solr_url = super
364
+ @cached_solr_url.path = @supplied_configuration["path"] if @cached_solr_url # fold in the path constructed from the multicore session
365
+ end
366
+ @cached_solr_url
367
+ end
368
+
369
+ private
370
+ def user_configuration
371
+ @user_configuration ||= @supplied_configuration || super
372
+ end
373
+
374
+ end
375
+ end
376
+ end