Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Reorganise in an attempt to get the plugin working with Rails3

  • Loading branch information...
commit d53c3a01283ad01191e1b9cf306bf97c51aee6fd 1 parent 8a6f89f
@springbok springbok authored
View
15 init.rb
@@ -1,14 +1 @@
-# Following line causes error 134 in RubyMine during debug
-#$:.unshift "#{File.dirname(__FILE__)}/lib"
-# Replace with:
-libdir = "#{File.dirname(__FILE__)}/lib"
-$LOAD_PATH.unshift(libdir) unless $LOAD_PATH.include?(libdir)
-require 'sunspot'
-require 'composite_setup'
-require 'sunspot/dsl/fields'
-require 'sunspot/dsl/standard_query'
-require 'sunspot/type'
-require 'sunspot/field'
-require 'sunspot/setup'
-require 'sunspot/field_factory'
-require 'sunspot/indexer'
+require 'sunspot_cell'
View
34 lib/composite_setup.rb
@@ -1,34 +0,0 @@
-module Sunspot
- class CompositeSetup
-
- # Collection of all attachment fields configured for any of the enclosed types.
- #
- # === Returns
- #
- # Array:: Text fields configured for the enclosed types
- #
- def all_attachment_fields
- @attachment_fields ||= attachment_fields_hash.values.map { |set| set.to_a }.flatten
- end
-
- private
-
- # Return a hash of field names to atachment field objects, containing all fields
- # that are configured for any of the types enclosed.
- #
- # ==== Returns
- #
- # Hash:: Hash of field names to text field objects.
- #
- def attachment_fields_hash
- @attachment_fields_hash ||=
- setups.inject({}) do |hash, setup|
- setup.all_attachment_fields.each do |text_field|
- (hash[text_field.name] ||= Set.new) << text_field
- end
- hash
- end
- end
-
- end
-end
View
3  lib/sunspot.rb
@@ -1,3 +0,0 @@
-%w(rich_document).each do |filename|
- require File.join(File.dirname(__FILE__), 'sunspot', filename)
-end
View
16 lib/sunspot/dsl/fields.rb
@@ -1,16 +0,0 @@
-module Sunspot
- module DSL
- class Fields
-
- # Added an attachment field, the attachment filename is passed to Solr for
- # indexing by tiqa
-
- def attachment(*names)
- names.each do |name|
- @setup.add_attachment_field_factory(name)
- end
- end
-
- end
- end
-end
View
130 lib/sunspot/dsl/standard_query.rb
@@ -1,130 +0,0 @@
-module Sunspot
- module DSL #:nodoc:
- #
- # This class presents a DSL for constructing queries using the
- # Sunspot.search method. Methods of this class are available inside the
- # search block. Much of the DSL's functionality is implemented by this
- # class's superclasses, Sunspot::DSL::FieldQuery and Sunspot::DSL::Scope
- #
- # See Sunspot.search for usage examples
- #
- class StandardQuery < FieldQuery
- include Paginatable, Adjustable
-
- # Specify a phrase that should be searched as fulltext. Only +text+
- # fields are searched - see DSL::Fields.text
- #
- # Keyword search is executed using Solr's dismax handler, which strikes
- # a good balance between powerful and foolproof. In particular,
- # well-matched quotation marks can be used to group phrases, and the
- # + and - modifiers work as expected. All other special Solr boolean
- # syntax is escaped, and mismatched quotes are ignored entirely.
- #
- # This method can optionally take a block, which is evaluated by the
- # Fulltext DSL class, and exposes several powerful dismax features.
- #
- # ==== Parameters
- #
- # keywords<String>:: phrase to perform fulltext search on
- #
- # ==== Options
- #
- # :fields<Array>::
- # List of fields that should be searched for keywords. Defaults to all
- # fields configured for the types under search.
- # :highlight<Boolean,Array>::
- # If true, perform keyword highlighting on all searched fields. If an
- # array of field names, perform highlighting on the specified fields.
- # This can also be called from within the fulltext block.
- # :minimum_match<Integer>::
- # The minimum number of search terms that a result must match. By
- # default, all search terms must match; if the number of search terms
- # is less than this number, the default behavior applies.
- # :tie<Float>::
- # A tiebreaker coefficient for scores derived from subqueries that are
- # lower-scoring than the maximum score subquery. Typically a near-zero
- # value is useful. See
- # http://wiki.apache.org/solr/DisMaxRequestHandler#tie_.28Tie_breaker.29
- # for more information.
- # :query_phrase_slop<Integer>::
- # The number of words that can appear between the words in a
- # user-entered phrase (i.e., keywords in quotes) and still match. For
- # instance, in a search for "\"great pizza\"" with a phrase slop of 1,
- # "great pizza" and "great big pizza" will match, but "great monster of
- # a pizza" will not. Default behavior is a query phrase slop of zero.
- #
- def fulltext(keywords, options = {}, &block)
- if keywords && !(keywords.to_s =~ /^\s*$/)
- fulltext_query = @query.add_fulltext(keywords)
- if field_names = options.delete(:fields)
- Util.Array(field_names).each do |field_name|
- @setup.text_fields(field_name).each do |field|
- fulltext_query.add_fulltext_field(field, field.default_boost)
- end
- end
- end
- if minimum_match = options.delete(:minimum_match)
- fulltext_query.minimum_match = minimum_match.to_i
- end
- if tie = options.delete(:tie)
- fulltext_query.tie = tie.to_f
- end
- if query_phrase_slop = options.delete(:query_phrase_slop)
- fulltext_query.query_phrase_slop = query_phrase_slop.to_i
- end
- if highlight_field_names = options.delete(:highlight)
- if highlight_field_names == true
- fulltext_query.add_highlight
- else
- highlight_fields = []
- Util.Array(highlight_field_names).each do |field_name|
- highlight_fields.concat(@setup.text_fields(field_name))
- end
- fulltext_query.add_highlight(highlight_fields)
- end
- end
- if block && fulltext_query
- fulltext_dsl = Fulltext.new(fulltext_query, @setup)
- Util.instance_eval_or_call(
- fulltext_dsl,
- &block
- )
- end
- if !field_names && (!fulltext_dsl || !fulltext_dsl.fields_added?)
- @setup.all_text_fields.each do |field|
- unless fulltext_query.has_fulltext_field?(field)
- unless fulltext_dsl && fulltext_dsl.exclude_fields.include?(field.name)
- fulltext_query.add_fulltext_field(field, field.default_boost)
- end
- end
- end
- end
- if !field_names && (!fulltext_dsl || !fulltext_dsl.fields_added?)
- unless @setup.all_attachment_fields.empty?
- @setup.all_attachment_fields.each do |attachment_text_field|
- unless fulltext_dsl && fulltext_dsl.exclude_fields.include?(attachment_text_field.name)
- fulltext_query.add_fulltext_field(attachment_text_field, attachment_text_field.default_boost)
- end
- end
- end
- end
- end
- end
- alias_method :keywords, :fulltext
-
- def with(*args)
- case args.first
- when String, Symbol
- field_name = args[0]
- value = args.length > 1 ? args[1] : Scope::NONE
- if value == Scope::NONE
- return DSL::RestrictionWithNear.new(@setup.field(field_name.to_sym), @scope, @query, false)
- end
- end
-
- # else
- super
- end
- end
- end
-end
View
5 lib/sunspot/field.rb
@@ -1,5 +0,0 @@
-module Sunspot
- class Field
- attr_reader :default_boost
- end
-end
View
3  lib/sunspot/rich_document.rb
@@ -1,4 +1,5 @@
module Sunspot
+
class RichDocument < RSolr::Message::Document
include Enumerable
@@ -11,8 +12,6 @@ def contains_attachment?
return false
end
-
-
def add(connection)
params = {
:wt => :ruby
View
23 lib/sunspot/type.rb
@@ -1,23 +0,0 @@
-module Sunspot
- module Type
-
- # class <<self
-
- class AttachmentType < AbstractType
- def indexed_name(name)
- "#{name}_attachment"
- end
-
- def to_indexed(value)
- value if value
- end
-
- def cast(text)
- text
- end
- end
-
- # end
-
- end
-end
View
17 lib/sunspot_cell.rb
@@ -0,0 +1,17 @@
+require 'sunspot/rich_document'
+require 'sunspot_cell/composite_setup'
+Sunspot::CompositeSetup.send(:include, SunspotCell::CompositeSetup)
+require 'sunspot_cell/dsl/fields'
+Sunspot::DSL::Fields.send(:include, SunspotCell::DSL::Fields)
+require 'sunspot_cell/dsl/standard_query'
+Sunspot::DSL::StandardQuery.send(:include, SunspotCell::DSL::StandardQuery)
+require 'sunspot_cell/type'
+Sunspot::Type.send(:include, SunspotCell::Type)
+require 'sunspot_cell/field'
+Sunspot::AttributeField.send(:include, SunspotCell::Field)
+require 'sunspot_cell/setup'
+Sunspot::Setup.send(:include, SunspotCell::Setup)
+require 'sunspot_cell/field_factory'
+Sunspot::FieldFactory.send(:include, SunspotCell::FieldFactory)
+require 'sunspot_cell/indexer'
+Sunspot::Indexer.send(:include, SunspotCell::Indexer)
View
39 lib/sunspot_cell/composite_setup.rb
@@ -0,0 +1,39 @@
+module SunspotCell
+ module CompositeSetup
+
+ extend ActiveSupport::Concern
+
+ module InstanceMethods
+
+ # Collection of all attachment fields configured for any of the enclosed types.
+ #
+ # === Returns
+ #
+ # Array:: Text fields configured for the enclosed types
+ #
+ def all_attachment_fields
+ @attachment_fields ||= attachment_fields_hash.values.map { |set| set.to_a }.flatten
+ end
+
+ private
+
+ # Return a hash of field names to atachment field objects, containing all fields
+ # that are configured for any of the types enclosed.
+ #
+ # ==== Returns
+ #
+ # Hash:: Hash of field names to text field objects.
+ #
+ def attachment_fields_hash
+ @attachment_fields_hash ||=
+ setups.inject({}) do |hash, setup|
+ setup.all_attachment_fields.each do |text_field|
+ (hash[text_field.name] ||= Set.new) << text_field
+ end
+ hash
+ end
+ end
+
+ end
+ end
+end
View
21 lib/sunspot_cell/dsl/fields.rb
@@ -0,0 +1,21 @@
+module SunspotCell
+ module DSL
+ module Fields
+
+ extend ActiveSupport::Concern
+
+ module InstanceMethods
+
+ # Added an attachment field, the attachment filename is passed to Solr for
+ # indexing by tiqa
+
+ def attachment(*names)
+ names.each do |name|
+ @setup.add_attachment_field_factory(name)
+ end
+ end
+
+ end
+ end
+ end
+end
View
70 lib/sunspot_cell/dsl/standard_query.rb
@@ -0,0 +1,70 @@
+module SunspotCell
+ module DSL
+ module StandardQuery
+
+ extend ActiveSupport::Concern
+
+ module InstanceMethods
+
+ def fulltext(keywords, options = {}, &block)
+ if keywords && !(keywords.to_s =~ /^\s*$/)
+ fulltext_query = @query.add_fulltext(keywords)
+ if field_names = options.delete(:fields)
+ Util.Array(field_names).each do |field_name|
+ @setup.text_fields(field_name).each do |field|
+ fulltext_query.add_fulltext_field(field, field.default_boost)
+ end
+ end
+ end
+ if minimum_match = options.delete(:minimum_match)
+ fulltext_query.minimum_match = minimum_match.to_i
+ end
+ if tie = options.delete(:tie)
+ fulltext_query.tie = tie.to_f
+ end
+ if query_phrase_slop = options.delete(:query_phrase_slop)
+ fulltext_query.query_phrase_slop = query_phrase_slop.to_i
+ end
+ if highlight_field_names = options.delete(:highlight)
+ if highlight_field_names == true
+ fulltext_query.add_highlight
+ else
+ highlight_fields = []
+ Util.Array(highlight_field_names).each do |field_name|
+ highlight_fields.concat(@setup.text_fields(field_name))
+ end
+ fulltext_query.add_highlight(highlight_fields)
+ end
+ end
+ if block && fulltext_query
+ fulltext_dsl = Fulltext.new(fulltext_query, @setup)
+ Util.instance_eval_or_call(
+ fulltext_dsl,
+ &block
+ )
+ end
+ if !field_names && (!fulltext_dsl || !fulltext_dsl.fields_added?)
+ @setup.all_text_fields.each do |field|
+ unless fulltext_query.has_fulltext_field?(field)
+ unless fulltext_dsl && fulltext_dsl.exclude_fields.include?(field.name)
+ fulltext_query.add_fulltext_field(field, field.default_boost)
+ end
+ end
+ end
+ end
+ if !field_names && (!fulltext_dsl || !fulltext_dsl.fields_added?)
+ unless @setup.all_attachment_fields.empty?
+ @setup.all_attachment_fields.each do |attachment_text_field|
+ unless fulltext_dsl && fulltext_dsl.exclude_fields.include?(attachment_text_field.name)
+ fulltext_query.add_fulltext_field(attachment_text_field, attachment_text_field.default_boost)
+ end
+ end
+ end
+ end
+ end
+ end
+
+ end
+ end
+ end
+end
View
5 lib/sunspot_cell/field.rb
@@ -0,0 +1,5 @@
+module SunspotCell
+ module Field
+ attr_reader :default_boost
+ end
+end
View
2  lib/sunspot/field_factory.rb → lib/sunspot_cell/field_factory.rb
@@ -1,4 +1,4 @@
-module Sunspot
+module SunspotCell
module FieldFactory
class Attachment
View
16 lib/sunspot/indexer.rb → lib/sunspot_cell/indexer.rb
@@ -1,6 +1,6 @@
-module Sunspot
- class Indexer
-
+module SunspotCell
+ module Indexer
+
def add_documents(documents)
documents_arr = Util.Array(documents)
docs_attach = []
@@ -20,15 +20,15 @@ def add_documents(documents)
document.add(@connection)
end
end
- end
-
-
- def document_for(model)
+ end
+
+
+ def document_for_1(model)
Sunspot::RichDocument.new(
:id => Adapters::InstanceAdapter.adapt(model).index_id,
:type => Util.superclasses_for(model.class).map { |clazz| clazz.name }
)
end
-
+
end
end
View
29 lib/sunspot/setup.rb → lib/sunspot_cell/setup.rb
@@ -1,6 +1,6 @@
-module Sunspot
- class Setup
-
+module SunspotCell
+ module Setup
+
def initialize(clazz)
@class_object_id = clazz.object_id
@class_name = clazz.name
@@ -12,9 +12,7 @@ def initialize(clazz)
@dsl = DSL::Fields.new(self)
add_field_factory(:class, Type::ClassType.instance)
end
-
-
-
+
# Add field_factories for fulltext search on attachments
#
# ==== Parameters
@@ -28,11 +26,9 @@ def add_attachment_field_factory(name, options = {}, &block)
@attachment_field_factories_cache[field_factory.name] << field_factory
end
end
-
-
-
+
def text_fields(field_name)
- text_field =
+ text_field =
if field_factory = @text_field_factories_cache[field_name.to_sym]
field_factory.build
else
@@ -47,15 +43,11 @@ def text_fields(field_name)
end
[text_field]
end
-
-
-
+
def all_attachment_fields
attachment_field_factories.map { |field_factory| field_factory.build }
end
-
-
-
+
# Get the text field_factories associated with this setup as well as all inherited
# attachment field_factories
#
@@ -66,13 +58,12 @@ def all_attachment_fields
def attachment_field_factories
collection_from_inheritable_hash(:attachment_field_factories)
end
-
-
+
def all_field_factories
all_field_factories = []
all_field_factories.concat(field_factories).concat(text_field_factories).concat(dynamic_field_factories).concat(attachment_field_factories)
all_field_factories
end
-
+
end
end
View
19 lib/sunspot_cell/type.rb
@@ -0,0 +1,19 @@
+module SunspotCell
+ module Type
+
+ class AttachmentType < Sunspot::Type::AbstractType
+ def indexed_name(name)
+ "#{name}_attachment"
+ end
+
+ def to_indexed(value)
+ value if value
+ end
+
+ def cast(text)
+ text
+ end
+ end
+
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.