Skip to content

Commit

Permalink
Merge branch 'v1'
Browse files Browse the repository at this point in the history
Conflicts:
	HISTORY
  • Loading branch information
pat committed May 10, 2012
2 parents 115d291 + 64ee2b2 commit 6bf8874
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 15 deletions.
1 change: 1 addition & 0 deletions HISTORY
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Edge:
* Can explicitly specify available types for STI tables instead of automatically discovering them with "SELECT DISTINCT type FROM <table>" (Cedric Maion).
* STI fix when generating WHERE clauses for sql_query.
* Don't try to run rake tasks for Capistrano if there's no Rakefile - eg. on fresh deploys (Nathan Smith).
* Populate search results before comparing with #==.
Expand Down
1 change: 1 addition & 0 deletions README.textile
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,4 @@ Since I first released this library, there's been quite a few people who have su
* Andrew Hunter
* Simon Hürlimann
* Nathan Smith
* Cedric Maion
6 changes: 5 additions & 1 deletion lib/thinking_sphinx/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def self.included(base)
extend ThinkingSphinx::ActiveRecord::ClassMethods

class << self
attr_accessor :sphinx_index_blocks
attr_accessor :sphinx_index_blocks, :sphinx_types

def set_sphinx_primary_key(attribute)
@sphinx_primary_key_attribute = attribute
Expand Down Expand Up @@ -56,6 +56,10 @@ def sphinx_index_options
sphinx_indexes.last.options
end

def set_sphinx_types(types)
@sphinx_types = types
end

# Generate a unique CRC value for the model's name, to use to
# determine which Sphinx documents belong to which AR records.
#
Expand Down
28 changes: 14 additions & 14 deletions lib/thinking_sphinx/source/sql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ module SQL
# the version filtered for delta values, send through :delta => true in
# the options. Won't do much though if the index isn't set up to support a
# delta sibling.
#
#
# Examples:
#
#
# source.to_sql
# source.to_sql(:delta => true)
#
Expand All @@ -32,10 +32,10 @@ def to_sql(options={})
# Simple helper method for the query range SQL - which is a statement that
# returns minimum and maximum id values. These can be filtered by delta -
# so pass in :delta => true to get the delta version of the SQL.
#
#
def to_sql_query_range(options={})
return nil if @index.options[:disable_range]

min_statement = adapter.convert_nulls(
"MIN(#{quote_column(@model.primary_key_for_sphinx)})", 1
)
Expand All @@ -54,7 +54,7 @@ def to_sql_query_range(options={})

# Simple helper method for the query info SQL - which is a statement that
# returns the single row for a corresponding id.
#
#
def to_sql_query_info(offset)
"SELECT * FROM #{@model.quoted_table_name} WHERE " +
"#{quote_column(@model.primary_key_for_sphinx)} = (($id - #{offset}) / #{ThinkingSphinx.context.indexed_models.size})"
Expand All @@ -64,7 +64,7 @@ def sql_select_clause(offset)
unique_id_expr = ThinkingSphinx.unique_id_expression(adapter, offset)

(
["#{@model.quoted_table_name}.#{quote_column(@model.primary_key_for_sphinx)} #{unique_id_expr} AS #{quote_column(@model.primary_key_for_sphinx)} "] +
["#{@model.quoted_table_name}.#{quote_column(@model.primary_key_for_sphinx)} #{unique_id_expr} AS #{quote_column(@model.primary_key_for_sphinx)} "] +
@fields.collect { |field| field.to_select_sql } +
@attributes.collect { |attribute| attribute.to_select_sql }
).compact.join(", ")
Expand Down Expand Up @@ -92,7 +92,7 @@ def sql_group_clause
end

(
["#{@model.quoted_table_name}.#{quote_column(@model.primary_key_for_sphinx)}"] +
["#{@model.quoted_table_name}.#{quote_column(@model.primary_key_for_sphinx)}"] +
@fields.collect { |field| field.to_group_sql }.compact +
@attributes.collect { |attribute| attribute.to_group_sql }.compact +
@groupings + internal_groupings
Expand All @@ -118,18 +118,18 @@ def quote_column(column)
def crc_column
if @model.table_exists? &&
@model.column_names.include?(@model.inheritance_column)

types = types_to_crcs
return @model.to_crc32.to_s if types.empty?

adapter.case(adapter.convert_nulls(
adapter.quote_with_table(@model.inheritance_column)),
types, @model.to_crc32)
else
@model.to_crc32.to_s
end
end

def internal_class_column
if @model.table_exists? &&
@model.column_names.include?(@model.inheritance_column)
Expand All @@ -138,14 +138,14 @@ def internal_class_column
"'#{@model.name}'"
end
end

def type_values
@model.connection.select_values <<-SQL
@model.sphinx_types.presence or @model.connection.select_values <<-SQL
SELECT DISTINCT #{@model.inheritance_column}
FROM #{@model.table_name}
SQL
end

def types_to_crcs
type_values.compact.inject({}) { |hash, type|
hash[type] = type.to_crc32
Expand All @@ -154,4 +154,4 @@ def types_to_crcs
end
end
end
end
end
19 changes: 19 additions & 0 deletions spec/thinking_sphinx/active_record_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,25 @@
end
end

describe '#types_for_sphinx' do
before :each do
@person = Person.find(:first)
end

after :each do
Person.set_sphinx_types nil
end

it "should return nil by default" do
@person.sphinx_types.should == nil
end

it "should return the specified value" do
Person.set_sphinx_types %w(Person Parent)
@person.sphinx_types.should == %w(Person Parent)
end
end

describe '.sphinx_index_names' do
it "should return the core index" do
Alpha.define_index { indexes :name }
Expand Down

0 comments on commit 6bf8874

Please sign in to comment.