spec/search_spec.rb in binarylogic-searchlogic-2.0.0 vs spec/search_spec.rb in binarylogic-searchlogic-2.0.1
- old
+ new
@@ -27,10 +27,21 @@
search = User.search(:username => "bjohnson")
search.conditions.should == {:username => "bjohnson"}
end
end
+ it "should clone properly" do
+ company = Company.create
+ user1 = company.users.create(:age => 5)
+ user2 = company.users.create(:age => 25)
+ search1 = company.users.search(:age_gt => 10)
+ search2 = search1.clone
+ search2.age_gt = 1
+ search2.all.should == User.all
+ search1.all.should == [user2]
+ end
+
context "conditions" do
it "should set the conditions and be accessible individually" do
search = User.search
search.conditions = {:username => "bjohnson"}
search.username.should == "bjohnson"
@@ -66,10 +77,18 @@
search = User.search
search.username_gt = "bjohnson"
search.username_gt.should == "bjohnson"
end
+ it "should allow chainging conditions" do
+ user = User.create(:username => "bjohnson", :age => 20)
+ User.create(:username => "bjohnson", :age => 5)
+ search = User.search
+ search.username_equals("bjohnson").age_gt(10)
+ search.all.should == [user]
+ end
+
it "should allow setting association conditions" do
search = User.search
search.orders_total_gt = 10
search.orders_total_gt.should == 10
end
@@ -90,14 +109,21 @@
search.username_gt = "bjohnson2"
search.username_greater_than.should == "bjohnson1"
search.username_gt.should == "bjohnson2"
end
- it "should allow setting custom conditions individually" do
+ it "should allow setting custom conditions individually with an arity of 0" do
User.named_scope(:four_year_olds, :conditions => {:age => 4})
search = User.search
search.four_year_olds = true
search.four_year_olds.should == true
+ end
+
+ it "should allow setting custom conditions individually with an arity of 1" do
+ User.named_scope(:username_should_be, lambda { |u| {:conditions => {:username => u}} })
+ search = User.search
+ search.username_should_be = "bjohnson"
+ search.username_should_be.should == "bjohnson"
end
it "should not allow setting conditions that are not scopes" do
search = User.search
lambda { search.unknown = true }.should raise_error(Searchlogic::Search::UnknownConditionError)
\ No newline at end of file