technoweenie / can_search

Build common named scopes automatically, and provide a simple way to merge them with a single #search call.

This URL has Read+Write access

can_search / spec / like_query_scope_spec.rb
100644 63 lines (50 sloc) 2.018 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
require File.dirname(__FILE__) + '/spec_helper'
 
module CanSearch
  describe "all LikeQuery Scopes", :shared => true do
    include CanSearchSpecHelper
 
    it "instantiates like query scope" do
      Record.search_scopes[@scope.name].should == @scope
    end
 
    it "creates named_scope" do
      Record.scopes[@scope.named_scope].should_not be_nil
    end
 
    it "filters records by full name" do
      compare_records Record.search(:name => "day"), [:day]
    end
    
    it "filters records by name that doesn't match" do
      compare_records Record.search(:name => "aye"), []
    end
    
    it "doesn't filter records if the specified parameter is nil" do
     compare_records Record.search(:name => nil), [:default, :day, :week_1, :week_2, :biweek_1, :biweek_2, :month_1, :month_2, :archive ]
    end
  end
 
  describe LikeQueryScope do
    describe "(LikeQuery Scope with no options)" do
      before do
        Record.can_search do
          scoped_by :name, :scope => :like
        end
        @scope = LikeQueryScope.new(Record, :name, :attribute => :name, :scope => :like_query, :named_scope => :like_name)
      end
      
      it "filters records by partial name" do
        compare_records Record.search(:name => "ay"), [:day]
      end
      
      it "filters multiple records by partial name" do
        compare_records Record.search(:name => "biweek"), [:biweek_1, :biweek_2]
      end
 
      it_should_behave_like "all LikeQuery Scopes"
    end
    
    describe "(LikeQuery Scope with a format option for exact match)" do
       before do
         Record.can_search do
           scoped_by :name, :scope => :like, :format => "%s"
         end
         @scope = LikeQueryScope.new(Record, :name, :attribute => :name, :scope => :like_query, :named_scope => :like_name, :format => "%s")
       end
       
       it "filters records using the format" do
         compare_records Record.search(:name => "ay"), []
       end
 
       it_should_behave_like "all LikeQuery Scopes"
     end
  end
end