Permalink
Browse files

Add fallback in case neither Bonsai nor general ElasticSearch is conf…

…igured
  • Loading branch information...
1 parent f915421 commit b4bd56d4e9103435122dca73e16aee5960909684 @carols10cents carols10cents committed Sep 30, 2012
Showing with 49 additions and 14 deletions.
  1. +4 −5 app/controllers/searches_controller.rb
  2. +19 −3 app/models/update.rb
  3. +5 −0 config/config.yml.sample
  4. +21 −6 test/acceptance/search_test.rb
@@ -4,12 +4,11 @@ def show
@updates = []
if params[:search]
set_params_page
- leading_char = '\b'
- if params[:search][0] == '#'
- leading_char = ''
- end
- @updates = Update.search(params[:search], load:true)
+ @updates = Update.search(
+ params[:search],
+ {:load => true}.merge(params)
+ )
set_pagination_buttons(@updates, :search => params[:search])
end
end
View
@@ -5,9 +5,25 @@ class Update
require 'cgi'
include MongoMapper::Document
- include Tire::Model::Search
- include Tire::Model::Callbacks
- index_name ELASTICSEARCH_INDEX_NAME
+ if ENV['BONSAI_INDEX_URL'] || ENV['ELASTICSEARCH_INDEX_URL']
+ include Tire::Model::Search
+ include Tire::Model::Callbacks
+ index_name ELASTICSEARCH_INDEX_NAME
+ else
+ # Fallback if elasticsearch is not enabled
+ def self.search(query, params = {})
+ leading_char = '\b'
+ if query[0] == '#'
+ leading_char = ''
+ end
+ self.where(:text => /#{leading_char}#{Regexp.quote(query)}\b/i).
+ paginate(
+ :page => params[:page],
+ :per_page => params[:per_page],
+ :order => :created_at.desc
+ )
+ end
+ end
# Determines what constitutes a username inside an update text
USERNAME_REGULAR_EXPRESSION = /(^|[ \t\n\r\f"'\(\[{]+)@([^ \t\n\r\f&?=@%\/\#]*[^ \t\n\r\f&?=@%\/\#.!:;,"'\]}\)])(?:@([^ \t\n\r\f&?=@%\/\#]*[^ \t\n\r\f&?=@%\/\#.!:;,"'\]}\)]))?/
View
@@ -12,6 +12,10 @@ development:
CONSUMER_KEY: asdf
CONSUMER_SECRET: asdf
+ # ElasticSearch configuration.
+ # If empty, a default regex search will be used.
+ ELASTICSEARCH_INDEX_URL:
+
#set this to the URL of a status blog or the like.
#if empty, the Blog footer link won't show.
BLOG_URL:
@@ -28,4 +32,5 @@ development:
test:
CONSUMER_KEY: asdf
CONSUMER_SECRET: asdf
+ ELASTICSEARCH_INDEX_URL:
BLOG_URL:
@@ -12,7 +12,11 @@ def search_for(query)
before do
@update_text = "These aren't the droids you're looking for!"
- Fabricate(:update, :text => @update_text)
+ log_in_as_some_user
+ VCR.use_cassette('publish_update') do
+ fill_in 'update-textarea', :with => @update_text
+ click_button :'update-button'
+ end
end
describe "logged in" do
@@ -87,13 +91,24 @@ def search_for(query)
assert has_link? "#hashtag"
end
- it "gets a match for words in the update out of order" do
- visit "/search"
+ # Not testing for BONSAI_INDEX_URL here since that is only for
+ # production on heroku.
+ if ENV['ELASTICSEARCH_INDEX_URL']
+ describe "with elasticsearch" do
+ it "gets a match for words in the update out of order" do
+ search_for("for looking")
- fill_in "q", :with => "for looking"
- click_button "Search"
+ assert_match @update_text, page.body
+ end
+ end
+ else
+ describe "without elasticsearch" do
+ it "does not get a match for words in the update out of order" do
+ search_for("for looking")
- assert_match @update_text, page.body
+ assert has_no_content? @update_text
+ end
+ end
end
end

0 comments on commit b4bd56d

Please sign in to comment.