lib/thinking_sphinx/index/builder.rb in dpickett-thinking-sphinx-1.1.4 vs lib/thinking_sphinx/index/builder.rb in dpickett-thinking-sphinx-1.1.12
- old
+ new
@@ -85,26 +85,20 @@
# indexes "age < 18", :as => :minor
#
def indexes(*args)
options = args.extract_options!
args.each do |columns|
- fields << Field.new(FauxColumn.coerce(columns), options)
+ field = Field.new(FauxColumn.coerce(columns), options)
+ fields << field
- if fields.last.sortable || fields.last.faceted
- attributes << Attribute.new(
- fields.last.columns.collect { |col| col.clone },
- options.merge(
- :type => :string,
- :as => fields.last.unique_name.to_s.concat("_sort").to_sym
- ).except(:facet)
- )
- end
+ add_sort_attribute field, options if field.sortable
+ add_facet_attribute field, options if field.faceted
end
end
alias_method :field, :indexes
alias_method :includes, :indexes
-
+
# This is the method to add attributes to your index (hence why it is
# aliased as 'attribute'). The syntax is the same as #indexes, so use
# that as starting point, but keep in mind the following points.
#
# An attribute can have an alias (the :as option), but it is always
@@ -140,21 +134,27 @@
# forget that Sphinx expects these values to be in radians.
#
def has(*args)
options = args.extract_options!
args.each do |columns|
- attributes << Attribute.new(FauxColumn.coerce(columns), options)
+ attribute = Attribute.new(FauxColumn.coerce(columns), options)
+ attributes << attribute
+
+ add_facet_attribute attribute, options if attribute.faceted
end
end
alias_method :attribute, :has
def facet(*args)
options = args.extract_options!
options[:facet] = true
args.each do |columns|
- attributes << Attribute.new(FauxColumn.coerce(columns), options)
+ attribute = Attribute.new(FauxColumn.coerce(columns), options)
+ attributes << attribute
+
+ add_facet_attribute attribute, options
end
end
# Use this method to add some manual SQL conditions for your index
# request. You can pass in as many strings as you like, they'll get
@@ -198,11 +198,21 @@
#
# set_property :latitude_attr => "lt", :longitude_attr => "lg"
#
# Please don't forget to add a boolean field named 'delta' to your
# model's database table if enabling the delta index for it.
+ # Valid options for the delta property are:
#
+ # true
+ # false
+ # :default
+ # :delayed
+ # :datetime
+ #
+ # You can also extend ThinkingSphinx::Deltas::DefaultDelta to implement
+ # your own handling for delta indexing.
+
def set_property(*args)
options = args.extract_options!
if options.empty?
@properties[args[0]] = args[1]
else
@@ -222,11 +232,32 @@
# that clash with method names in the Builder class (ie: properties,
# fields, attributes).
#
# Example: indexes assoc(:properties).column
#
- def assoc(assoc)
- FauxColumn.new(method)
+ def assoc(assoc, *args)
+ FauxColumn.new(assoc, *args)
+ end
+
+ private
+
+ def add_sort_attribute(field, options)
+ add_internal_attribute field, options, "_sort"
+ end
+
+ def add_facet_attribute(resource, options)
+ add_internal_attribute resource, options, "_facet", true
+ end
+
+ def add_internal_attribute(resource, options, suffix, crc = false)
+ @attributes << Attribute.new(
+ resource.columns.collect { |col| col.clone },
+ options.merge(
+ :type => resource.is_a?(Field) ? :string : options[:type],
+ :as => resource.unique_name.to_s.concat(suffix).to_sym,
+ :crc => crc
+ ).except(:facet)
+ )
end
end
end
end
end