Skip to content

Commit

Permalink
Adds disabled configuration option
Browse files Browse the repository at this point in the history
  • Loading branch information
alindeman committed Oct 16, 2011
1 parent 8a2cc83 commit b2dfd0e
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 2 deletions.
2 changes: 2 additions & 0 deletions sunspot_rails/History.txt
Expand Up @@ -7,6 +7,8 @@
* `sunspot:reindex` rake task correctly handles namespaced models (João Gradim)
* `bind_address` parameter can be specified in `sunspot.yml` to bind Solr to a
specific interface (Sylvain Utard)
* `disabled` parameter can be specified in `sunspot.yml` to stub out the
connection to Solr (David B)

== 1.2.1 2010-12-28
* Decreased default reindexing batch size from 500 to 50
Expand Down
4 changes: 3 additions & 1 deletion sunspot_rails/lib/sunspot/rails.rb
Expand Up @@ -27,7 +27,9 @@ def reset
end

def build_session(configuration = self.configuration)
if configuration.has_master?
if configuration.disabled?
StubSessionProxy.new(Sunspot.session)
elsif configuration.has_master?
SessionProxy::MasterSlaveSessionProxy.new(
SessionProxy::ThreadLocalSessionProxy.new(master_config(configuration)),
SessionProxy::ThreadLocalSessionProxy.new(slave_config(configuration))
Expand Down
11 changes: 10 additions & 1 deletion sunspot_rails/lib/sunspot/rails/configuration.rb
Expand Up @@ -15,6 +15,7 @@ module Rails #:nodoc:
# max_memory: 1G
# solr_jar: /some/path/solr15/start.jar
# bind_address: 0.0.0.0
# disabled: false
# test:
# solr:
# hostname: localhost
Expand Down Expand Up @@ -246,7 +247,15 @@ def max_memory
def bind_address
@bind_address ||= user_configuration_from_key('solr', 'bind_address')
end


#
# Whether or not to disable Solr.
# Defaults to false.
#
def disabled?
@disabled ||= (user_configuration_from_key('disabled') || false)
end

private

#
Expand Down
14 changes: 14 additions & 0 deletions sunspot_rails/spec/configuration_spec.rb
Expand Up @@ -71,6 +71,10 @@
it "should handle the 'bind_address' property when not set" do
@config.bind_address.should be_nil
end

it "should handle the 'disabled' property when not set" do
@config.disabled?.should be_false
end
end

describe Sunspot::Rails::Configuration, "user provided sunspot.yml" do
Expand Down Expand Up @@ -124,6 +128,16 @@
end
end

describe Sunspot::Rails::Configuration, "with disabled: true in sunspot.yml" do
before(:each) do
::Rails.stub!(:env => 'config_disabled_test')
@config = Sunspot::Rails::Configuration.new
end

it "should handle the 'disabled' property when set" do
@config.disabled?.should be_true
end
end

describe Sunspot::Rails::Configuration, "with ENV['SOLR_URL'] overriding sunspot.yml" do
before(:all) do
Expand Down
2 changes: 2 additions & 0 deletions sunspot_rails/spec/rails_template/config/sunspot.yml
Expand Up @@ -18,3 +18,5 @@ config_test:
bind_address: 127.0.0.1
auto_commit_after_request: false
auto_commit_after_delete_request: true
config_disabled_test:
disabled: true
15 changes: 15 additions & 0 deletions sunspot_rails/spec/session_spec.rb
Expand Up @@ -10,6 +10,21 @@
it 'should not create a separate master/slave session if no master configured' do
end

context 'disabled' do
before do
Sunspot::Rails.reset
::Rails.stub!(:env).and_return("config_disabled_test")
end

after do
Sunspot::Rails.reset
end

it 'sets the session proxy as a stub' do
Sunspot::Rails.build_session.should be_a_kind_of(Sunspot::Rails::StubSessionProxy)
end
end

private

def with_configuration(options)
Expand Down

0 comments on commit b2dfd0e

Please sign in to comment.