add only:species filter

This commit is contained in:
Emi Matchu 2010-06-08 17:25:27 -04:00
parent 820cbcb5ce
commit 10f6d721e1
2 changed files with 21 additions and 7 deletions

View file

@ -65,18 +65,22 @@ class Item < ActiveRecord::Base
def narrow(scope) def narrow(scope)
items = Table(:objects) items = Table(:objects)
if @property == 'species' if @property == 'species' || @property == 'only'
species = Species.find_by_name(self) species = Species.find_by_name(self)
raise ArgumentError, "Species \"#{self.humanize}\" does not exist" unless species raise ArgumentError, "Species \"#{self.humanize}\" does not exist" unless species
# TODO: add a many-to-many table to handle this relationship, if # TODO: add a many-to-many table to handle this relationship, if
# performance becomes an issue # performance becomes an issue
ids = items[:species_support_ids] ids = items[:species_support_ids]
if @property == 'species'
condition = ids.eq('').or(ids.matches_any( condition = ids.eq('').or(ids.matches_any(
species.id, species.id,
"#{species.id},%", "#{species.id},%",
"%,#{species.id},%", "%,#{species.id},%",
"%,#{species.id}" "%,#{species.id}"
)) ))
else
condition = items[:species_support_ids].eq(species.id.to_s)
end
elsif @property == 'description' || @property.blank? elsif @property == 'description' || @property.blank?
column = @property == 'description' ? :description : :name column = @property == 'description' ? :description : :name
condition = items[column].matches("%#{self}%") condition = items[column].matches("%#{self}%")

View file

@ -143,6 +143,16 @@ describe Item do
Item.search('-species:aisha').count.should == 1 Item.search('-species:aisha').count.should == 1
end end
specify "should search by only:species" do
Factory.create :item, :species_support_ids => [1], :name => 'a'
Factory.create :item, :species_support_ids => [1,2], :name => 'b'
Factory.create :item, :species_support_ids => [], :name => 'c'
Item.search('only:acara').map(&:name).should == ['a']
Item.search('only:aisha').count.should == 0
Item.search('-only:acara').map(&:name).should == ['b', 'c']
Item.search('-only:aisha').map(&:name).should == ['a', 'b', 'c']
end
specify "should be able to negate word in search" do specify "should be able to negate word in search" do
query_should 'hat -blue', query_should 'hat -blue',
:return => [ :return => [