obitum-searchlogic 2.4.28

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. data/.gitignore +6 -0
  2. data/LICENSE +20 -0
  3. data/README.rdoc +308 -0
  4. data/Rakefile +35 -0
  5. data/VERSION.yml +5 -0
  6. data/init.rb +1 -0
  7. data/lib/searchlogic.rb +56 -0
  8. data/lib/searchlogic/active_record/association_proxy.rb +19 -0
  9. data/lib/searchlogic/active_record/consistency.rb +49 -0
  10. data/lib/searchlogic/active_record/named_scope_tools.rb +101 -0
  11. data/lib/searchlogic/core_ext/object.rb +43 -0
  12. data/lib/searchlogic/core_ext/proc.rb +17 -0
  13. data/lib/searchlogic/named_scopes/alias_scope.rb +67 -0
  14. data/lib/searchlogic/named_scopes/association_conditions.rb +132 -0
  15. data/lib/searchlogic/named_scopes/association_ordering.rb +44 -0
  16. data/lib/searchlogic/named_scopes/conditions.rb +226 -0
  17. data/lib/searchlogic/named_scopes/or_conditions.rb +141 -0
  18. data/lib/searchlogic/named_scopes/ordering.rb +48 -0
  19. data/lib/searchlogic/rails_helpers.rb +79 -0
  20. data/lib/searchlogic/search.rb +26 -0
  21. data/lib/searchlogic/search/base.rb +26 -0
  22. data/lib/searchlogic/search/conditions.rb +58 -0
  23. data/lib/searchlogic/search/date_parts.rb +23 -0
  24. data/lib/searchlogic/search/implementation.rb +14 -0
  25. data/lib/searchlogic/search/method_missing.rb +123 -0
  26. data/lib/searchlogic/search/ordering.rb +10 -0
  27. data/lib/searchlogic/search/scopes.rb +19 -0
  28. data/lib/searchlogic/search/to_yaml.rb +31 -0
  29. data/lib/searchlogic/search/unknown_condition_error.rb +15 -0
  30. data/rails/init.rb +1 -0
  31. data/searchlogic.gemspec +98 -0
  32. data/spec/searchlogic/active_record/association_proxy_spec.rb +23 -0
  33. data/spec/searchlogic/active_record/consistency_spec.rb +28 -0
  34. data/spec/searchlogic/core_ext/object_spec.rb +9 -0
  35. data/spec/searchlogic/core_ext/proc_spec.rb +8 -0
  36. data/spec/searchlogic/named_scopes/alias_scope_spec.rb +23 -0
  37. data/spec/searchlogic/named_scopes/association_conditions_spec.rb +203 -0
  38. data/spec/searchlogic/named_scopes/association_ordering_spec.rb +27 -0
  39. data/spec/searchlogic/named_scopes/conditions_spec.rb +319 -0
  40. data/spec/searchlogic/named_scopes/or_conditions_spec.rb +66 -0
  41. data/spec/searchlogic/named_scopes/ordering_spec.rb +34 -0
  42. data/spec/searchlogic/search_spec.rb +497 -0
  43. data/spec/spec_helper.rb +132 -0
  44. metadata +136 -0
@@ -0,0 +1,23 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper")
2
+
3
+ describe "Searchlogic::ActiveRecord::AssociationProxy" do
4
+ it "should call location conditions" do
5
+ company = Company.create
6
+ user = company.users.create(:username => "bjohnson")
7
+ company.users.send(:username_like, "bjohnson").should == [user]
8
+ end
9
+
10
+ it "should call ordering conditions" do
11
+ company = Company.create
12
+ user = company.users.create(:username => "bjohnson")
13
+ company.users.send(:ascend_by_username).should == [user]
14
+ end
15
+
16
+ it "should call 'or' conditions" do
17
+ company = Company.create
18
+ user = company.users.create(:username => "bjohnson")
19
+ company.users.send(:username_or_some_type_id_like, "bjohnson").should == [user]
20
+ end
21
+
22
+ end
23
+
@@ -0,0 +1,28 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper")
2
+
3
+ describe Searchlogic::ActiveRecord::Consistency do
4
+ it "should merge joins with consistent conditions" do
5
+ user_group = UserGroup.create
6
+ user_group.users.user_groups_name_like("name").user_groups_id_gt(10).scope(:find)[:joins].should == [
7
+ "INNER JOIN \"user_groups_users\" ON \"user_groups_users\".user_id = \"users\".id",
8
+ "INNER JOIN \"user_groups\" ON \"user_groups\".id = \"user_groups_users\".user_group_id"
9
+ ]
10
+ end
11
+
12
+ it "should respect parenthesis when reordering conditions" do
13
+ joins = [
14
+ "INNER JOIN \"table\" ON (\"b\".user_id = \"a\".id)",
15
+ "INNER JOIN \"table\" ON (\"b\".id = \"a\".user_group_id)"
16
+ ]
17
+ ActiveRecord::Base.send(:merge_joins, joins).should == [
18
+ "INNER JOIN \"table\" ON \"a\".id = \"b\".user_id",
19
+ "INNER JOIN \"table\" ON \"a\".user_group_id = \"b\".id"
20
+ ]
21
+ end
22
+
23
+ it "shuold not convert joins to strings when delegating via associations" do
24
+ User.alias_scope :has_id_gt, lambda { User.id_gt(10).has_name.orders_id_gt(10) }
25
+ User.alias_scope :has_name, lambda { User.orders_created_at_after(Time.now).name_equals("ben").username_equals("ben") }
26
+ Company.users_has_id_gt.proxy_options[:joins].should == {:users=>[:orders]}
27
+ end
28
+ end
@@ -0,0 +1,9 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper")
2
+
3
+ describe Searchlogic::CoreExt::Object do
4
+ it "should accept and pass the argument to the searchlogic_options" do
5
+ proc = searchlogic_lambda(:integer, :test => :value) {}
6
+ proc.searchlogic_options[:type].should == :integer
7
+ proc.searchlogic_options[:test].should == :value
8
+ end
9
+ end
@@ -0,0 +1,8 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper")
2
+
3
+ describe Searchlogic::CoreExt::Proc do
4
+ it "should have a searchlogic_options accessor" do
5
+ p = Proc.new {}
6
+ p.searchlogic_options[:type] = :integer
7
+ end
8
+ end
@@ -0,0 +1,23 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper")
2
+
3
+ describe Searchlogic::NamedScopes::AliasScope do
4
+ before(:each) do
5
+ User.alias_scope :username_has, lambda { |value| User.username_like(value) }
6
+ end
7
+
8
+ it "should allow alias scopes" do
9
+ User.create(:username => "bjohnson")
10
+ User.create(:username => "thunt")
11
+ User.username_has("bjohnson").all.should == User.find_all_by_username("bjohnson")
12
+ end
13
+
14
+ it "should allow alias scopes from the search object" do
15
+ search = User.search
16
+ search.username_has = "bjohnson"
17
+ search.username_has.should == "bjohnson"
18
+ end
19
+
20
+ it "should inherit alias scopes from superclasses" do
21
+ Class.new(User).alias_scope?("username_has").should be_true
22
+ end
23
+ end
@@ -0,0 +1,203 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper")
2
+
3
+ describe Searchlogic::NamedScopes::AssociationConditions do
4
+ it "should create a named scope" do
5
+ Company.users_username_like("bjohnson").proxy_options.should == User.username_like("bjohnson").proxy_options.merge(:joins => :users)
6
+ end
7
+
8
+ it "should create a deep named scope" do
9
+ Company.users_orders_total_greater_than(10).proxy_options.should == Order.total_greater_than(10).proxy_options.merge(:joins => {:users => :orders})
10
+ end
11
+
12
+ it "should allow the use of foreign pre-existing named scopes" do
13
+ User.named_scope :uname, lambda { |value| {:conditions => ["users.username = ?", value]} }
14
+ Company.users_uname("bjohnson").proxy_options.should == User.uname("bjohnson").proxy_options.merge(:joins => :users)
15
+ end
16
+
17
+ it "should allow the use of deep foreign pre-existing named scopes" do
18
+ pending
19
+ Order.named_scope :big_id, :conditions => "orders.id > 100"
20
+ Company.users_orders_big_id.proxy_options.should == Order.big_id.proxy_options.merge(:joins => {:users => :orders})
21
+ end
22
+
23
+ it "should allow the use of foreign pre-existing alias scopes" do
24
+ User.alias_scope :username_has, lambda { |value| User.username_like(value) }
25
+ Company.users_username_has("bjohnson").proxy_options.should == User.username_has("bjohnson").proxy_options.merge(:joins => :users)
26
+ end
27
+
28
+ it "should not raise errors for scopes that don't return anything" do
29
+ User.alias_scope :blank_scope, lambda { |value| }
30
+ Company.users_blank_scope("bjohnson").proxy_options.should == {:joins => :users}
31
+ end
32
+
33
+ it "should ignore polymorphic associations" do
34
+ lambda { Fee.owner_created_at_gt(Time.now) }.should raise_error(NoMethodError)
35
+ end
36
+
37
+ it "should not allow named scopes on non existent association columns" do
38
+ lambda { User.users_whatever_like("bjohnson") }.should raise_error(NoMethodError)
39
+ end
40
+
41
+ it "should not allow named scopes on non existent deep association columns" do
42
+ lambda { User.users_orders_whatever_like("bjohnson") }.should raise_error(NoMethodError)
43
+ end
44
+
45
+ it "should allow named scopes to be called multiple times and reflect the value passed" do
46
+ Company.users_username_like("bjohnson").proxy_options.should == User.username_like("bjohnson").proxy_options.merge(:joins => :users)
47
+ Company.users_username_like("thunt").proxy_options.should == User.username_like("thunt").proxy_options.merge(:joins => :users)
48
+ end
49
+
50
+ it "should allow deep named scopes to be called multiple times and reflect the value passed" do
51
+ Company.users_orders_total_greater_than(10).proxy_options.should == Order.total_greater_than(10).proxy_options.merge(:joins => {:users => :orders})
52
+ Company.users_orders_total_greater_than(20).proxy_options.should == Order.total_greater_than(20).proxy_options.merge(:joins => {:users => :orders})
53
+ end
54
+
55
+ it "should have an arity of 1 if the underlying scope has an arity of 1" do
56
+ Company.users_orders_total_greater_than(10)
57
+ Company.named_scope_arity("users_orders_total_greater_than").should == Order.named_scope_arity("total_greater_than")
58
+ end
59
+
60
+ it "should have an arity of nil if the underlying scope has an arity of nil" do
61
+ Company.users_orders_total_null
62
+ Company.named_scope_arity("users_orders_total_null").should == Order.named_scope_arity("total_null")
63
+ end
64
+
65
+ it "should have an arity of -1 if the underlying scope has an arity of -1" do
66
+ Company.users_id_equals_any
67
+ Company.named_scope_arity("users_id_equals_any").should == User.named_scope_arity("id_equals_any")
68
+ end
69
+
70
+ it "should allow aliases" do
71
+ Company.users_username_contains("bjohnson").proxy_options.should == User.username_contains("bjohnson").proxy_options.merge(:joins => :users)
72
+ end
73
+
74
+ it "should allow deep aliases" do
75
+ Company.users_orders_total_gt(10).proxy_options.should == Order.total_gt(10).proxy_options.merge(:joins => {:users => :orders})
76
+ end
77
+
78
+ it "should copy over the named scope options" do
79
+ Order.user_whatever_at_equals(1)
80
+ Order.named_scope_options(:user_whatever_at_equals).searchlogic_options[:skip_conversion].should == true
81
+ end
82
+
83
+ it "should include optional associations" do
84
+ pending # this is a problem with using inner joins and left outer joins
85
+ Company.create
86
+ company = Company.create
87
+ user = company.users.create
88
+ order = user.orders.create(:total => 20, :taxes => 3)
89
+ Company.ascend_by_users_orders_total.all.should == Company.all
90
+ end
91
+
92
+ it "should implement exclusive scoping" do
93
+ scope = Company.users_company_name_like("name").users_company_description_like("description")
94
+ scope.scope(:find)[:joins].should == [
95
+ "INNER JOIN \"users\" ON companies.id = users.company_id",
96
+ "INNER JOIN \"companies\" companies_users ON \"companies_users\".id = \"users\".company_id"
97
+ ]
98
+ lambda { scope.all }.should_not raise_error
99
+ end
100
+
101
+ it "should not create the same join twice" do
102
+ scope = Company.users_orders_total_gt(10).users_orders_taxes_lt(5).ascend_by_users_orders_total
103
+ scope.scope(:find)[:joins].should == [
104
+ "INNER JOIN \"users\" ON companies.id = users.company_id",
105
+ "INNER JOIN \"orders\" ON orders.user_id = users.id"
106
+ ]
107
+ lambda { scope.count }.should_not raise_error
108
+ end
109
+
110
+ it "should not create the same join twice when traveling through the duplicate join" do
111
+ scope = Company.users_username_like("bjohnson").users_orders_total_gt(100)
112
+ scope.scope(:find)[:joins].should == [
113
+ "INNER JOIN \"users\" ON companies.id = users.company_id",
114
+ "INNER JOIN \"orders\" ON orders.user_id = users.id"
115
+ ]
116
+ lambda { scope.count }.should_not raise_error
117
+ end
118
+
119
+ it "should not create the same join twice when traveling through the deep duplicate join" do
120
+ scope = Company.users_orders_total_gt(100).users_orders_line_items_price_gt(20)
121
+ scope.scope(:find)[:joins].should == [
122
+ "INNER JOIN \"users\" ON companies.id = users.company_id",
123
+ "INNER JOIN \"orders\" ON orders.user_id = users.id",
124
+ "INNER JOIN \"line_items\" ON line_items.order_id = orders.id"
125
+ ]
126
+ lambda { scope.all }.should_not raise_error
127
+ end
128
+
129
+ it "should allow the use of :include when a join was created" do
130
+ company = Company.create
131
+ user = company.users.create
132
+ order = user.orders.create(:total => 20, :taxes => 3)
133
+ Company.users_orders_total_gt(10).users_orders_taxes_lt(5).ascend_by_users_orders_total.all(:include => :users).should == Company.all
134
+ end
135
+
136
+ it "should allow the use of deep :include when a join was created" do
137
+ company = Company.create
138
+ user = company.users.create
139
+ order = user.orders.create(:total => 20, :taxes => 3)
140
+ Company.users_orders_total_gt(10).users_orders_taxes_lt(5).ascend_by_users_orders_total.all(:include => {:users => :orders}).should == Company.all
141
+ end
142
+
143
+ it "should allow the use of :include when traveling through the duplicate join" do
144
+ company = Company.create
145
+ user = company.users.create(:username => "bjohnson")
146
+ order = user.orders.create(:total => 20, :taxes => 3)
147
+ Company.users_username_like("bjohnson").users_orders_taxes_lt(5).ascend_by_users_orders_total.all(:include => :users).should == Company.all
148
+ end
149
+
150
+ it "should allow the use of deep :include when traveling through the duplicate join" do
151
+ company = Company.create
152
+ user = company.users.create(:username => "bjohnson")
153
+ order = user.orders.create(:total => 20, :taxes => 3)
154
+ Company.users_orders_taxes_lt(50).ascend_by_users_orders_total.all(:include => {:users => :orders}).should == Company.all
155
+ end
156
+
157
+ it "should automatically add string joins if the association condition is using strings" do
158
+ User.named_scope(:orders_big_id, :joins => User.inner_joins(:orders))
159
+ Company.users_orders_big_id.proxy_options.should == {:joins=>[" INNER JOIN \"users\" ON users.company_id = companies.id ", " INNER JOIN \"orders\" ON orders.user_id = users.id "]}
160
+ end
161
+
162
+ it "should order the join statements ascending by the fieldnames so that we don't get double joins where the only difference is that the order of the fields is different" do
163
+ company = Company.create
164
+ user = company.users.create(:company_id => company.id)
165
+ company.users.company_id_eq(company.id).should == [user]
166
+ end
167
+
168
+ it "should sanitize the scope on a foreign model instead of passing the raw options back to the original" do
169
+ Company.named_scope(:users_count_10, :conditions => {:users_count => 10})
170
+ User.company_users_count_10.proxy_options.should == {:conditions => "\"companies\".\"users_count\" = 10", :joins => :company}
171
+ end
172
+
173
+ it "should delegate to polymorphic relationships" do
174
+ Audit.auditable_user_type_name_like("ben").proxy_options.should == {
175
+ :conditions => ["users.name LIKE ?", "%ben%"],
176
+ :joins => "INNER JOIN \"users\" ON \"users\".id = \"audits\".auditable_id AND \"audits\".auditable_type = 'User'"
177
+ }
178
+ end
179
+
180
+ it "should delegate to polymorphic relationships (with a lazy split on _type_)" do
181
+ Audit.auditable_user_type_some_type_id_like("ben").proxy_options.should == {
182
+ :conditions => ["users.some_type_id LIKE ?", "%ben%"],
183
+ :joins => "INNER JOIN \"users\" ON \"users\".id = \"audits\".auditable_id AND \"audits\".auditable_type = 'User'"
184
+ }
185
+ end
186
+
187
+ it "should deep delegate to polymorphic relationships" do
188
+ Audit.auditable_user_type_company_name_like("company").proxy_options.should == {
189
+ :conditions => ["companies.name LIKE ?", "%company%"],
190
+ :joins => ["INNER JOIN \"users\" ON \"users\".id = \"audits\".auditable_id AND \"audits\".auditable_type = 'User'", " INNER JOIN \"companies\" ON \"companies\".id = \"users\".company_id "]
191
+ }
192
+ end
193
+
194
+ it "should allow any on a has_many relationship" do
195
+ company1 = Company.create
196
+ user1 = company1.users.create
197
+ company2 = Company.create
198
+ user2 = company2.users.create
199
+ user3 = company2.users.create
200
+
201
+ Company.users_id_equals_any([user2.id, user3.id]).all(:select => "DISTINCT companies.*").should == [company2]
202
+ end
203
+ end
@@ -0,0 +1,27 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper")
2
+
3
+ describe Searchlogic::NamedScopes::Ordering do
4
+ it "should allow ascending" do
5
+ Company.ascend_by_users_username.proxy_options.should == User.ascend_by_username.proxy_options.merge(:joins => :users)
6
+ end
7
+
8
+ it "should allow descending" do
9
+ Company.descend_by_users_username.proxy_options.should == User.descend_by_username.proxy_options.merge(:joins => :users)
10
+ end
11
+
12
+ it "should allow deep ascending" do
13
+ Company.ascend_by_users_orders_total.proxy_options.should == Order.ascend_by_total.proxy_options.merge(:joins => {:users => :orders})
14
+ end
15
+
16
+ it "should allow deep descending" do
17
+ Company.descend_by_users_orders_total.proxy_options.should == Order.descend_by_total.proxy_options.merge(:joins => {:users => :orders})
18
+ end
19
+
20
+ it "should ascend with a belongs to" do
21
+ User.ascend_by_company_name.proxy_options.should == Company.ascend_by_name.proxy_options.merge(:joins => :company)
22
+ end
23
+
24
+ it "should work through #order" do
25
+ Company.order('ascend_by_users_username').proxy_options.should == Company.ascend_by_users_username.proxy_options
26
+ end
27
+ end
@@ -0,0 +1,319 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper")
2
+
3
+ describe Searchlogic::NamedScopes::Conditions do
4
+ it "should be dynamically created and then cached" do
5
+ User.should_not respond_to(:age_less_than)
6
+ User.age_less_than(5)
7
+ User.should respond_to(:age_less_than)
8
+ end
9
+
10
+ it "should not allow conditions on non columns" do
11
+ lambda { User.whatever_equals(2) }.should raise_error(NoMethodError)
12
+ end
13
+
14
+ context "comparison conditions" do
15
+ it "should have equals" do
16
+ (5..7).each { |age| User.create(:age => age) }
17
+ User.age_equals(6).all.should == User.find_all_by_age(6)
18
+ User.age_equals(5..6).all.should == User.find_all_by_age(5..6)
19
+ User.age_equals([5, 7]).all.should == User.find_all_by_age([5, 7])
20
+ end
21
+
22
+ it "should have does not equal" do
23
+ (5..7).each { |age| User.create(:age => age) }
24
+ User.age_does_not_equal(6).all.should == User.find_all_by_age([5,7])
25
+ end
26
+
27
+ it "should have less than" do
28
+ (5..7).each { |age| User.create(:age => age) }
29
+ User.age_less_than(6).all.should == User.find_all_by_age(5)
30
+ end
31
+
32
+ it "should have less than or equal to" do
33
+ (5..7).each { |age| User.create(:age => age) }
34
+ User.age_less_than_or_equal_to(6).all.should == User.find_all_by_age([5, 6])
35
+ end
36
+
37
+ it "should have greater than" do
38
+ (5..7).each { |age| User.create(:age => age) }
39
+ User.age_greater_than(6).all.should == User.find_all_by_age(7)
40
+ end
41
+
42
+ it "should have greater than or equal to" do
43
+ (5..7).each { |age| User.create(:age => age) }
44
+ User.age_greater_than_or_equal_to(6).all.should == User.find_all_by_age([6, 7])
45
+ end
46
+ end
47
+
48
+ context "wildcard conditions" do
49
+ it "should have like" do
50
+ %w(bjohnson thunt).each { |username| User.create(:username => username) }
51
+ User.username_like("john").all.should == User.find_all_by_username("bjohnson")
52
+ end
53
+
54
+ it "should have not like" do
55
+ %w(bjohnson thunt).each { |username| User.create(:username => username) }
56
+ User.username_not_like("john").all.should == User.find_all_by_username("thunt")
57
+ end
58
+
59
+ it "should have begins with" do
60
+ %w(bjohnson thunt).each { |username| User.create(:username => username) }
61
+ User.username_begins_with("bj").all.should == User.find_all_by_username("bjohnson")
62
+ end
63
+
64
+ it "should have not begin with" do
65
+ %w(bjohnson thunt).each { |username| User.create(:username => username) }
66
+ User.username_not_begin_with("bj").all.should == User.find_all_by_username("thunt")
67
+ end
68
+
69
+ it "should have ends with" do
70
+ %w(bjohnson thunt).each { |username| User.create(:username => username) }
71
+ User.username_ends_with("son").all.should == User.find_all_by_username("bjohnson")
72
+ end
73
+
74
+ it "should have not end with" do
75
+ %w(bjohnson thunt).each { |username| User.create(:username => username) }
76
+ User.username_not_end_with("son").all.should == User.find_all_by_username("thunt")
77
+ end
78
+ end
79
+
80
+ context "boolean conditions" do
81
+ it "should have scopes for boolean columns" do
82
+ female = User.create(:male => false)
83
+ male = User.create(:male => true)
84
+ User.male.all.should == [male]
85
+ User.not_male.all.should == [female]
86
+ end
87
+
88
+ it "should have null" do
89
+ ["bjohnson", nil].each { |username| User.create(:username => username) }
90
+ User.username_null.all.should == User.find_all_by_username(nil)
91
+ end
92
+
93
+ it "should have not null" do
94
+ ["bjohnson", nil].each { |username| User.create(:username => username) }
95
+ User.username_not_null.all.should == User.find_all_by_username("bjohnson")
96
+ end
97
+
98
+ it "should have empty" do
99
+ ["bjohnson", ""].each { |username| User.create(:username => username) }
100
+ User.username_empty.all.should == User.find_all_by_username("")
101
+ end
102
+
103
+ it "should have blank" do
104
+ ["bjohnson", "", nil].each { |username| User.create(:username => username) }
105
+ User.username_blank.all.should == [User.find_by_username(""), User.find_by_username(nil)]
106
+ end
107
+
108
+ it "should have not blank" do
109
+ ["bjohnson", "", nil].each { |username| User.create(:username => username) }
110
+ User.username_not_blank.all.should == User.find_all_by_username("bjohnson")
111
+ end
112
+ end
113
+
114
+ context "any and all conditions" do
115
+ it "should do nothing if no arguments are passed" do
116
+ User.username_equals_any.proxy_options.should == {}
117
+ end
118
+
119
+ it "should treat an array and multiple arguments the same" do
120
+ %w(bjohnson thunt dgainor).each { |username| User.create(:username => username) }
121
+ User.username_like_any("bjohnson", "thunt").should == User.username_like_any(["bjohnson", "thunt"])
122
+ end
123
+
124
+ it "should have equals any" do
125
+ %w(bjohnson thunt dgainor).each { |username| User.create(:username => username) }
126
+ User.username_equals_any("bjohnson", "thunt").all.should == User.find_all_by_username(["bjohnson", "thunt"])
127
+ end
128
+
129
+ it "should have equals all" do
130
+ %w(bjohnson thunt dainor).each { |username| User.create(:username => username) }
131
+ User.username_equals_all("bjohnson", "thunt").all.should == []
132
+ end
133
+
134
+ it "should have does not equal any" do
135
+ %w(bjohnson thunt dgainor).each { |username| User.create(:username => username) }
136
+ User.username_does_not_equal_any("bjohnson", "thunt").all.should == User.find_all_by_username(["bjohnson", "thunt", "dgainor"])
137
+ end
138
+
139
+ it "should have does not equal all" do
140
+ %w(bjohnson thunt dgainor).each { |username| User.create(:username => username) }
141
+ User.username_does_not_equal_all("bjohnson", "thunt").all.should == User.find_all_by_username("dgainor")
142
+ end
143
+
144
+ it "should have less than any" do
145
+ (5..7).each { |age| User.create(:age => age) }
146
+ User.age_less_than_any(7,6).all.should == User.find_all_by_age([5, 6])
147
+ end
148
+
149
+ it "should have less than all" do
150
+ (5..7).each { |age| User.create(:age => age) }
151
+ User.age_less_than_all(7,6).all.should == User.find_all_by_age(5)
152
+ end
153
+
154
+ it "should have less than or equal to any" do
155
+ (5..7).each { |age| User.create(:age => age) }
156
+ User.age_less_than_or_equal_to_any(7,6).all.should == User.find_all_by_age([5, 6, 7])
157
+ end
158
+
159
+ it "should have less than or equal to all" do
160
+ (5..7).each { |age| User.create(:age => age) }
161
+ User.age_less_than_or_equal_to_all(7,6).all.should == User.find_all_by_age([5, 6])
162
+ end
163
+
164
+ it "should have less than any" do
165
+ (5..7).each { |age| User.create(:age => age) }
166
+ User.age_greater_than_any(5,6).all.should == User.find_all_by_age([6, 7])
167
+ end
168
+
169
+ it "should have greater than all" do
170
+ (5..7).each { |age| User.create(:age => age) }
171
+ User.age_greater_than_all(5,6).all.should == User.find_all_by_age(7)
172
+ end
173
+
174
+ it "should have greater than or equal to any" do
175
+ (5..7).each { |age| User.create(:age => age) }
176
+ User.age_greater_than_or_equal_to_any(5,6).all.should == User.find_all_by_age([5, 6, 7])
177
+ end
178
+
179
+ it "should have greater than or equal to all" do
180
+ (5..7).each { |age| User.create(:age => age) }
181
+ User.age_greater_than_or_equal_to_all(5,6).all.should == User.find_all_by_age([6, 7])
182
+ end
183
+
184
+ it "should have like all" do
185
+ %w(bjohnson thunt dgainor).each { |username| User.create(:username => username) }
186
+ User.username_like_all("bjohnson", "thunt").all.should == []
187
+ User.username_like_all("n", "o").all.should == User.find_all_by_username(["bjohnson", "dgainor"])
188
+ end
189
+
190
+ it "should have like any" do
191
+ %w(bjohnson thunt dgainor).each { |username| User.create(:username => username) }
192
+ User.username_like_any("bjohnson", "thunt").all.should == User.find_all_by_username(["bjohnson", "thunt"])
193
+ end
194
+
195
+ it "should have begins with all" do
196
+ %w(bjohnson thunt dgainor).each { |username| User.create(:username => username) }
197
+ User.username_begins_with_all("bjohnson", "thunt").all.should == []
198
+ end
199
+
200
+ it "should have begins with any" do
201
+ %w(bjohnson thunt dgainor).each { |username| User.create(:username => username) }
202
+ User.username_begins_with_any("bj", "th").all.should == User.find_all_by_username(["bjohnson", "thunt"])
203
+ end
204
+
205
+ it "should have ends with all" do
206
+ %w(bjohnson thunt dgainor).each { |username| User.create(:username => username) }
207
+ User.username_ends_with_all("n", "r").all.should == []
208
+ end
209
+
210
+ it "should have ends with any" do
211
+ %w(bjohnson thunt dgainor).each { |username| User.create(:username => username) }
212
+ User.username_ends_with_any("n", "r").all.should == User.find_all_by_username(["bjohnson", "dgainor"])
213
+ end
214
+ end
215
+
216
+ context "alias conditions" do
217
+ it "should have is" do
218
+ User.age_is(5).proxy_options.should == User.age_equals(5).proxy_options
219
+ end
220
+
221
+ it "should have eq" do
222
+ User.age_eq(5).proxy_options.should == User.age_equals(5).proxy_options
223
+ end
224
+
225
+ it "should have not_equal_to" do
226
+ User.age_not_equal_to(5).proxy_options.should == User.age_does_not_equal(5).proxy_options
227
+ end
228
+
229
+ it "should have is_not" do
230
+ User.age_is_not(5).proxy_options.should == User.age_does_not_equal(5).proxy_options
231
+ end
232
+
233
+ it "should have not" do
234
+ User.age_not(5).proxy_options.should == User.age_does_not_equal(5).proxy_options
235
+ end
236
+
237
+ it "should have ne" do
238
+ User.age_ne(5).proxy_options.should == User.age_does_not_equal(5).proxy_options
239
+ end
240
+
241
+ it "should have lt" do
242
+ User.age_lt(5).proxy_options.should == User.age_less_than(5).proxy_options
243
+ end
244
+
245
+ it "should have lte" do
246
+ User.age_lte(5).proxy_options.should == User.age_less_than_or_equal_to(5).proxy_options
247
+ end
248
+
249
+ it "should have gt" do
250
+ User.age_gt(5).proxy_options.should == User.age_greater_than(5).proxy_options
251
+ end
252
+
253
+ it "should have gte" do
254
+ User.age_gte(5).proxy_options.should == User.age_greater_than_or_equal_to(5).proxy_options
255
+ end
256
+
257
+ it "should have contains" do
258
+ User.username_contains(5).proxy_options.should == User.username_like(5).proxy_options
259
+ end
260
+
261
+ it "should have contains" do
262
+ User.username_includes(5).proxy_options.should == User.username_like(5).proxy_options
263
+ end
264
+
265
+ it "should have bw" do
266
+ User.username_bw(5).proxy_options.should == User.username_begins_with(5).proxy_options
267
+ end
268
+
269
+ it "should have ew" do
270
+ User.username_ew(5).proxy_options.should == User.username_ends_with(5).proxy_options
271
+ end
272
+
273
+ it "should have nil" do
274
+ User.username_nil.proxy_options.should == User.username_nil.proxy_options
275
+ end
276
+ end
277
+
278
+ context "group conditions" do
279
+ it "should have in" do
280
+ (5..7).each { |age| User.create(:age => age) }
281
+ User.age_in([5,6]).all.should == User.find(:all, :conditions => ["users.age IN (?)", [5, 6]])
282
+ end
283
+
284
+ it "should have not_in" do
285
+ (5..7).each { |age| User.create(:age => age) }
286
+ User.age_not_in([5,6]).all.should == User.find(:all, :conditions => ["users.age NOT IN (?)", [5, 6]])
287
+ end
288
+ end
289
+
290
+ context "searchlogic lambda" do
291
+ it "should be a string" do
292
+ User.username_like("test")
293
+ User.named_scope_options(:username_like).searchlogic_options[:type].should == :string
294
+ end
295
+
296
+ it "should be an integer" do
297
+ User.id_gt(10)
298
+ User.named_scope_options(:id_gt).searchlogic_options[:type].should == :integer
299
+ end
300
+
301
+ it "should be a float" do
302
+ Order.total_gt(10)
303
+ Order.named_scope_options(:total_gt).searchlogic_options[:type].should == :float
304
+ end
305
+ end
306
+
307
+ it "should have priorty to columns over conflicting association conditions" do
308
+ Company.users_count_gt(10)
309
+ User.create
310
+ User.company_id_null.count.should == 1
311
+ User.company_id_not_null.count.should == 0
312
+ end
313
+
314
+ it "should fix bug for issue 26" do
315
+ count1 = User.id_ne(10).username_not_like("root").count
316
+ count2 = User.id_ne(10).username_not_like("root").count
317
+ count1.should == count2
318
+ end
319
+ end