Skip to content

Commit

Permalink
continue moving configuration tasks to backend
Browse files Browse the repository at this point in the history
  • Loading branch information
moro committed Sep 3, 2008
1 parent 2230ae1 commit 6e42df6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
18 changes: 7 additions & 11 deletions lib/acts_as_searchable.rb
Expand Up @@ -117,8 +117,8 @@ def acts_as_searchable(options = {})
include ActsAsSearchable::InstanceMethods
include ActsAsSearchable::DirtyTracking

cattr_accessor :searchable_fields, :attributes_to_store, :if_changed, :search_backend, :estraier_node,
:fulltext_index_observing_fields
cattr_accessor :searchable_fields, :attributes_to_store, :if_changed,
:search_backend, :fulltext_index_observing_fields

self.searchable_fields = options[:searchable_fields] || [ :body ]
self.attributes_to_store = options[:attributes] || {}
Expand All @@ -143,7 +143,7 @@ def acts_as_searchable(options = {})
after_save :clear_changed_attributes
end

connect_backend(estraier_config)
connect_backend(configuration)
end

# Perform a fulltext search against the Hyper Estraier index.
Expand Down Expand Up @@ -210,20 +210,16 @@ def reindex!
find(:all).each { |r| r.update_index(true) }
end

protected

private
def connect_backend(backend_config = {}) #:nodoc:
node_prefix = backend_config['node'] || RAILS_ENV
self.estraier_node = node_prefix + '_' + self.table_name

self.search_backend = Backends.connect(self, backend_config)
end

def estraier_config #:nodoc:
configurations[RAILS_ENV]['estraier'] or {}
def configuration #:nodoc:
# configurations[RAILS_ENV]['estraier'] is for backward compatibility.
configurations[RAILS_ENV]['search'] || configurations[RAILS_ENV]['estraier'] || {}
end

private
def find_by_ids_scope(ids, options={})
return [] if ids.blank?
with_scope(:find=>{:conditions=>["#{table_name}.id IN (?)", ids]}) do
Expand Down
24 changes: 19 additions & 5 deletions lib/acts_as_searchable/backends/hyper_estraier.rb
Expand Up @@ -8,15 +8,24 @@ module Backends
class HyperEstraier
SYSTEM_ATTRIBUTES = %w( uri digest cdate mdate adate title author type lang genre size weight misc )

attr_reader :connection
attr_reader :connection, :node_name

DEFAULT_CONFIG = {
'host' => 'localhost',
'port' => 1978,
'user' => 'admin',
'password' => 'admin',
}.freeze

# FIXME use URI
def initialize(ar_class, host, port, user, password)
def initialize(ar_class, config = {})
@ar_class = ar_class
@connection = EstraierPure::Node.new
config = DEFAULT_CONFIG.merge(config)
self.node_name = calculate_node_name(config)

@connection.set_url("http://#{host}:#{port}/node/#{ar_class.estraier_node}")
@connection.set_auth(user, password)
@connection = EstraierPure::Node.new
@connection.set_url("http://#{config['host']}:#{config['port']}/node/#{self.node_name}")
@connection.set_auth(config['user'], config['password'])
end

def index
Expand Down Expand Up @@ -113,6 +122,11 @@ def benchmark(log, &block)
@ar_class.benchmark(log, &block)
end

def calculate_node_name(config)
node_prefix = config['node_prefix'] || config['node'] || RAILS_ENV
"#{node_prefix}_#{@ar_class.table_name}"
end

def attribute_name(attribute)
SYSTEM_ATTRIBUTES.include?(attribute.to_s) ? "@#{attribute}" : "#{attribute}"
end
Expand Down

0 comments on commit 6e42df6

Please sign in to comment.