lib/thinking_sphinx/field.rb in DrMark-thinking-sphinx-0.9.9 vs lib/thinking_sphinx/field.rb in DrMark-thinking-sphinx-1.1.6

- old
+ new

@@ -6,11 +6,12 @@ # One key thing to remember - if you're using the field manually to # generate SQL statements, you'll need to set the base model, and all the # associations. Which can get messy. Use Index.link!, it really helps. # class Field - attr_accessor :alias, :columns, :sortable, :associations, :model, :infixes, :prefixes + attr_accessor :alias, :columns, :sortable, :associations, :model, :infixes, + :prefixes, :faceted # To create a new field, you'll need to pass in either a single Column # or an array of them, and some (optional) options. The columns are # references to the data that will make up the field. # @@ -56,14 +57,15 @@ @columns = Array(columns) @associations = {} raise "Cannot define a field with no columns. Maybe you are trying to index a field with a reserved name (id, name). You can fix this error by using a symbol rather than a bare name (:id instead of id)." if @columns.empty? || @columns.any? { |column| !column.respond_to?(:__stack) } - @alias = options[:as] - @sortable = options[:sortable] || false - @infixes = options[:infixes] || false - @prefixes = options[:prefixes] || false + @alias = options[:as] + @sortable = options[:sortable] || false + @infixes = options[:infixes] || false + @prefixes = options[:prefixes] || false + @faceted = options[:facet] || false end # Get the part of the SELECT clause related to this field. Don't forget # to set your model and associations first though. # @@ -73,14 +75,14 @@ def to_select_sql clause = @columns.collect { |column| column_with_prefix(column) }.join(', ') - clause = concatenate(clause) if concat_ws? - clause = group_concatenate(clause) if is_many? + clause = adapter.concatenate(clause) if concat_ws? + clause = adapter.group_concatenate(clause) if is_many? - "#{cast_to_string clause } AS #{quote_column(unique_name)}" + "#{adapter.cast_to_string clause } AS #{quote_column(unique_name)}" end # Get the part of the GROUP BY clause related to this field - if one is # needed. If not, all you'll get back is nil. The latter will happen if # there's multiple data values (read: a has_many or has_and_belongs_to_many @@ -108,43 +110,20 @@ else @alias end end - private - - def concatenate(clause) - case @model.connection.class.name - when "ActiveRecord::ConnectionAdapters::MysqlAdapter" - "CONCAT_WS(' ', #{clause})" - when "ActiveRecord::ConnectionAdapters::PostgreSQLAdapter" - clause.split(', ').join(" || ' ' || ") - else - clause - end + def to_facet + return nil unless @faceted + + ThinkingSphinx::Facet.new(self) end - def group_concatenate(clause) - case @model.connection.class.name - when "ActiveRecord::ConnectionAdapters::MysqlAdapter" - "GROUP_CONCAT(#{clause} SEPARATOR ' ')" - when "ActiveRecord::ConnectionAdapters::PostgreSQLAdapter" - "array_to_string(array_accum(#{clause}), ' ')" - else - clause - end - end + private - def cast_to_string(clause) - case @model.connection.class.name - when "ActiveRecord::ConnectionAdapters::MysqlAdapter" - "CAST(#{clause} AS CHAR)" - when "ActiveRecord::ConnectionAdapters::PostgreSQLAdapter" - clause - else - clause - end + def adapter + @adapter ||= @model.sphinx_database_adapter end def quote_column(column) @model.connection.quote_column_name(column) end @@ -154,20 +133,11 @@ # associations. # def concat_ws? @columns.length > 1 || multiple_associations? end - - # Checks the association tree for each column - if they're all the same, - # returns false. - # - def multiple_sources? - first = associations[@columns.first] - - !@columns.all? { |col| associations[col] == first } - end - + # Checks whether any column requires multiple associations (which only # happens for polymorphic situations). # def multiple_associations? associations.any? { |col,assocs| assocs.length > 1 } @@ -195,12 +165,8 @@ # Could there be more than one value related to the parent record? If so, # then this will return true. If not, false. It's that simple. # def is_many? associations.values.flatten.any? { |assoc| assoc.is_many? } - end - - def is_string? - columns.all? { |col| col.is_string? } end end end