lib/thinking_sphinx/index/builder.rb in warp-thinking-sphinx-1.3.11 vs lib/thinking_sphinx/index/builder.rb in warp-thinking-sphinx-1.3.13
- old
+ new
@@ -30,19 +30,15 @@
index
end
def initialize(index, &block)
@index = index
- @source = ThinkingSphinx::Source.new(@index)
- @index.sources << @source
@explicit_source = false
self.instance_eval &block
- if @index.sources.any? { |source|
- source.fields.length == 0
- }
+ if no_fields?
raise "At least one field is necessary for an index"
end
end
def define_source(&block)
@@ -103,11 +99,11 @@
# indexes "age < 18", :as => :minor
#
def indexes(*args)
options = args.extract_options!
args.each do |columns|
- field = Field.new(@source, FauxColumn.coerce(columns), options)
+ field = Field.new(source, FauxColumn.coerce(columns), options)
add_sort_attribute field, options if field.sortable
add_facet_attribute field, options if field.faceted
end
end
@@ -149,22 +145,22 @@
# forget that Sphinx expects these values to be in radians.
#
def has(*args)
options = args.extract_options!
args.each do |columns|
- attribute = Attribute.new(@source, FauxColumn.coerce(columns), options)
+ attribute = Attribute.new(source, FauxColumn.coerce(columns), options)
add_facet_attribute attribute, options if attribute.faceted
end
end
def facet(*args)
options = args.extract_options!
options[:facet] = true
args.each do |columns|
- attribute = Attribute.new(@source, FauxColumn.coerce(columns), options)
+ attribute = Attribute.new(source, FauxColumn.coerce(columns), options)
add_facet_attribute attribute, options
end
end
@@ -174,21 +170,21 @@
#
# where "user_id = 10"
# where "parent_type = 'Article'", "created_at < NOW()"
#
def where(*args)
- @source.conditions += args
+ source.conditions += args
end
# Use this method to add some manual SQL strings to the GROUP BY
# clause. You can pass in as many strings as you'd like, they'll get
# joined together with commas later on.
#
# group_by "lat", "lng"
#
def group_by(*args)
- @source.groupings += args
+ source.groupings += args
end
# This is what to use to set properties on the index. Chief amongst
# those is the delta property - to allow automatic updates to your
# indexes as new models are added and edited - but also you can
@@ -249,14 +245,22 @@
FauxColumn.new(assoc, *args)
end
private
+ def source
+ @source ||= begin
+ source = ThinkingSphinx::Source.new(@index)
+ @index.sources << source
+ source
+ end
+ end
+
def set_single_property(key, value)
source_options = ThinkingSphinx::Configuration::SourceOptions
if source_options.include?(key.to_s)
- @source.options.merge! key => value
+ source.options.merge! key => value
else
@index.local_options.merge! key => value
end
end
@@ -270,17 +274,23 @@
end
def add_internal_attribute(property, options, suffix, crc = false)
return unless ThinkingSphinx::Facet.translate?(property)
- Attribute.new(@source,
+ Attribute.new(source,
property.columns.collect { |col| col.clone },
options.merge(
:type => property.is_a?(Field) ? :string : options[:type],
:as => property.unique_name.to_s.concat(suffix).to_sym,
:crc => crc
).except(:facet)
)
+ end
+
+ def no_fields?
+ @index.sources.empty? || @index.sources.any? { |source|
+ source.fields.length == 0
+ }
end
end
end
end