Skip to content

Commit

Permalink
Merge branch 'association-filtered-faceting-v1' into association-filt…
Browse files Browse the repository at this point in the history
…ered-faceting-v2
  • Loading branch information
pixeltrix committed Jul 18, 2012
2 parents 8100a4a + c85940e commit ed22a0e
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 6 deletions.
1 change: 1 addition & 0 deletions HISTORY
@@ -1,4 +1,5 @@
Edge: Edge:
* Add support for association-filtered faceting (Andrew White).
* Cast PostgreSQL timestamps to their floored integers (instead of rounding up). * Cast PostgreSQL timestamps to their floored integers (instead of rounding up).
* Don't add array_accum to PostgreSQL v80311 (8.3.?) or newer. * Don't add array_accum to PostgreSQL v80311 (8.3.?) or newer.


Expand Down
19 changes: 13 additions & 6 deletions lib/thinking_sphinx/active_record/has_many_association.rb
Expand Up @@ -2,16 +2,23 @@ module ThinkingSphinx
module ActiveRecord module ActiveRecord
module HasManyAssociation module HasManyAssociation
def search(*args) def search(*args)
options = args.extract_options! @reflection.klass.search(*association_args(args))
end

def facets(*args)
@reflection.klass.facets(*association_args(args))
end

private

def association_args(args)
options = args.extract_options!
options[:with] ||= {} options[:with] ||= {}
options[:with].merge! default_filter options[:with].merge! default_filter


args << options args + [options]
@reflection.klass.search(*args)
end end


private

def attribute_for_foreign_key def attribute_for_foreign_key
foreign_key = @reflection.primary_key_name foreign_key = @reflection.primary_key_name
stack = [@reflection.options[:through]].compact stack = [@reflection.options[:through]].compact
Expand Down
74 changes: 74 additions & 0 deletions spec/thinking_sphinx/active_record/has_many_association_spec.rb
Expand Up @@ -41,6 +41,46 @@
end end
end end


describe "facets method" do
before :each do
Friendship.stub!(:facets => true)

@person = Person.find(:first)
@index = Friendship.sphinx_indexes.first
end

it "should raise an error if the required attribute doesn't exist" do
@index.stub!(:attributes => [])

lambda { @person.friendships.facets "test" }.should raise_error(RuntimeError)
end

it "should add a filter for the attribute into a normal facets call" do
Friendship.should_receive(:facets) do |query, options|
options[:with][:person_id].should == @person.id
end

@person.friendships.facets "test"
end

it "should add a filter for an aliased attribute into a normal facets call" do
@team = CricketTeam.new
@team.stub!(:id => 1)

Person.should_receive(:facets).with do |query, options|
options[:with][:team_id].should == @team.id
end

@team.people.facets "test"
end

it "should define indexes for the reflection class" do
Friendship.should_receive(:define_indexes)

@person.friendships.facets 'test'
end
end

describe "search method for has_many :through" do describe "search method for has_many :through" do
before :each do before :each do
Person.stub!(:search => true) Person.stub!(:search => true)
Expand Down Expand Up @@ -75,6 +115,40 @@
end end
end end


describe "facets method for has_many :through" do
before :each do
Person.stub!(:facets => true)

@person = Person.find(:first)
@index = Person.sphinx_indexes.first
end

it "should raise an error if the required attribute doesn't exist" do
@index.stub!(:attributes => [])

lambda { @person.friends.facets "test" }.should raise_error(RuntimeError)
end

it "should add a filter for the attribute into a normal facets call" do
Person.should_receive(:facets).with do |query, options|
options[:with][:friendly_ids].should == @person.id
end

@person.friends.facets "test"
end

it "should add a filter for an aliased attribute into a normal facets call" do
@team = FootballTeam.new
@team.stub!(:id => 1)

Person.should_receive(:facets).with do |query, options|
options[:with][:football_team_id].should == @team.id
end

@team.people.facets "test"
end
end

describe 'filtering sphinx scopes' do describe 'filtering sphinx scopes' do
before :each do before :each do
Friendship.stub!(:search => Friendship) Friendship.stub!(:search => Friendship)
Expand Down

0 comments on commit ed22a0e

Please sign in to comment.