Created
March 25, 2011 19:46
-
-
Save mark-ellul/887504 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Copyright (c) Henry Poydar | |
# Modified from couchrest-rails | |
# A Rails plugin for connecting to and working with CouchDB via CouchRest | |
# https://github.com/hpoydar/couchrest-rails/ | |
require 'sunspot_couch.rb' | |
begin | |
env = Rails.env.to_s || 'development' | |
couchdb_config = YAML::load(ERB.new(IO.read(Rails.root.to_s << '/config/couchdb.yml')).result)[env] | |
host = couchdb_config['host'] || 'localhost' | |
port = couchdb_config['port'] || '5984' | |
database = couchdb_config['database'] | |
username = couchdb_config['username'] | |
password = couchdb_config['password'] | |
ssl = couchdb_config['ssl'] || false | |
db_prefix = couchdb_config['database_prefix'] || %q{} | |
db_suffix = couchdb_config['database_suffix'] || %q{_} << Rails.env.to_s | |
host = 'localhost' if host == nil | |
port = '5984' if port == nil | |
ssl = false if ssl == nil | |
protocol = ssl ? 'https' : 'http' | |
authorized_host = (username.blank? && password.blank?) ? host : "#{CGI.escape(username)}:#{CGI.escape(password)}@#{host}" | |
rescue | |
raise "There was a problem with your config/couchdb.yml file. Check and make sure it's present and the syntax is correct." | |
else | |
COUCHDB_CONFIG = { | |
:host_path => "#{protocol}://#{authorized_host}:#{port}", | |
:db_prefix => "#{db_prefix}", | |
:db_suffix => "#{db_suffix}", | |
} | |
database_name = "#{db_prefix}#{database}#{db_suffix}" | |
COUCHDB_SERVER = CouchRest.new COUCHDB_CONFIG[:host_path] | |
COUCHDB_SERVER.default_database = database_name | |
end | |
# Set default database in all models | |
CouchRest::Model::Base.class_eval { use_database COUCHDB_SERVER.default_database } | |
Sunspot::Adapters::InstanceAdapter.register(Sunspot::Couch::Adapters::CouchInstanceAdapter, CouchRest::Model::Base) | |
Sunspot::Adapters::DataAccessor.register(Sunspot::Couch::Adapters::CouchDataAccessor, CouchRest::Model::Base) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment