Skip to content

Instantly share code, notes, and snippets.

@cartoonROR
Forked from cameronpriest/gist:1164400
Created August 23, 2011 08:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cartoonROR/1164637 to your computer and use it in GitHub Desktop.
Save cartoonROR/1164637 to your computer and use it in GitHub Desktop.
class BookDetail < CouchRest::Model::Base
include Sunspot::Rails::Searchable
use_database "bookdetail"
property :name, String
property :detail_name, String
searchable do
text :detail_name
end
timestamps!
end
class BooksController < ApplicationController
def search
@books = BookDetail.search do
keywords params[:query]
end.results
respond_to do |format|
format.html { render :action => "index" }
format.xml { render :xml => @books }
end
end
end
# 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)
module Sunspot
module Couch
module Adapters
class CouchInstanceAdapter < Sunspot::Adapters::InstanceAdapter
def id
@instance.id
end
end
class CouchDataAccessor < Sunspot::Adapters::DataAccessor
def load(id)
@clazz.find(id)
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment