lazy-searchlogic 2.4.10
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +6 -0
- data/LICENSE +20 -0
- data/README.rdoc +308 -0
- data/Rakefile +35 -0
- data/VERSION.yml +5 -0
- data/init.rb +1 -0
- data/lib/searchlogic.rb +40 -0
- data/lib/searchlogic/active_record/consistency.rb +49 -0
- data/lib/searchlogic/active_record/named_scope_tools.rb +101 -0
- data/lib/searchlogic/core_ext/object.rb +41 -0
- data/lib/searchlogic/core_ext/proc.rb +11 -0
- data/lib/searchlogic/named_scopes/alias_scope.rb +67 -0
- data/lib/searchlogic/named_scopes/association_conditions.rb +131 -0
- data/lib/searchlogic/named_scopes/association_ordering.rb +44 -0
- data/lib/searchlogic/named_scopes/conditions.rb +227 -0
- data/lib/searchlogic/named_scopes/or_conditions.rb +141 -0
- data/lib/searchlogic/named_scopes/ordering.rb +48 -0
- data/lib/searchlogic/rails_helpers.rb +76 -0
- data/lib/searchlogic/search.rb +209 -0
- data/rails/init.rb +1 -0
- data/searchlogic.gemspec +86 -0
- data/spec/active_record/consistency_spec.rb +28 -0
- data/spec/core_ext/object_spec.rb +7 -0
- data/spec/core_ext/proc_spec.rb +9 -0
- data/spec/named_scopes/alias_scope_spec.rb +19 -0
- data/spec/named_scopes/association_conditions_spec.rb +188 -0
- data/spec/named_scopes/association_ordering_spec.rb +27 -0
- data/spec/named_scopes/conditions_spec.rb +319 -0
- data/spec/named_scopes/or_conditions_spec.rb +66 -0
- data/spec/named_scopes/ordering_spec.rb +34 -0
- data/spec/search_spec.rb +416 -0
- data/spec/spec_helper.rb +129 -0
- metadata +107 -0
data/rails/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "searchlogic"
|
data/searchlogic.gemspec
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{searchlogic}
|
8
|
+
s.version = "2.4.10"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Ben Johnson of Binary Logic"]
|
12
|
+
s.date = %q{2010-02-06}
|
13
|
+
s.description = %q{Searchlogic makes using ActiveRecord named scopes easier and less repetitive.}
|
14
|
+
s.email = %q{bjohnson@binarylogic.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".gitignore",
|
21
|
+
"LICENSE",
|
22
|
+
"README.rdoc",
|
23
|
+
"Rakefile",
|
24
|
+
"VERSION.yml",
|
25
|
+
"init.rb",
|
26
|
+
"lib/searchlogic.rb",
|
27
|
+
"lib/searchlogic/active_record/consistency.rb",
|
28
|
+
"lib/searchlogic/active_record/named_scope_tools.rb",
|
29
|
+
"lib/searchlogic/core_ext/object.rb",
|
30
|
+
"lib/searchlogic/core_ext/proc.rb",
|
31
|
+
"lib/searchlogic/named_scopes/alias_scope.rb",
|
32
|
+
"lib/searchlogic/named_scopes/association_conditions.rb",
|
33
|
+
"lib/searchlogic/named_scopes/association_ordering.rb",
|
34
|
+
"lib/searchlogic/named_scopes/conditions.rb",
|
35
|
+
"lib/searchlogic/named_scopes/or_conditions.rb",
|
36
|
+
"lib/searchlogic/named_scopes/ordering.rb",
|
37
|
+
"lib/searchlogic/rails_helpers.rb",
|
38
|
+
"lib/searchlogic/search.rb",
|
39
|
+
"rails/init.rb",
|
40
|
+
"searchlogic.gemspec",
|
41
|
+
"spec/active_record/consistency_spec.rb",
|
42
|
+
"spec/core_ext/object_spec.rb",
|
43
|
+
"spec/core_ext/proc_spec.rb",
|
44
|
+
"spec/named_scopes/alias_scope_spec.rb",
|
45
|
+
"spec/named_scopes/association_conditions_spec.rb",
|
46
|
+
"spec/named_scopes/association_ordering_spec.rb",
|
47
|
+
"spec/named_scopes/conditions_spec.rb",
|
48
|
+
"spec/named_scopes/or_conditions_spec.rb",
|
49
|
+
"spec/named_scopes/ordering_spec.rb",
|
50
|
+
"spec/search_spec.rb",
|
51
|
+
"spec/spec_helper.rb"
|
52
|
+
]
|
53
|
+
s.homepage = %q{http://github.com/binarylogic/searchlogic}
|
54
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
55
|
+
s.require_paths = ["lib"]
|
56
|
+
s.rubyforge_project = %q{searchlogic}
|
57
|
+
s.rubygems_version = %q{1.3.5}
|
58
|
+
s.summary = %q{Searchlogic makes using ActiveRecord named scopes easier and less repetitive.}
|
59
|
+
s.test_files = [
|
60
|
+
"spec/active_record/consistency_spec.rb",
|
61
|
+
"spec/core_ext/object_spec.rb",
|
62
|
+
"spec/core_ext/proc_spec.rb",
|
63
|
+
"spec/named_scopes/alias_scope_spec.rb",
|
64
|
+
"spec/named_scopes/association_conditions_spec.rb",
|
65
|
+
"spec/named_scopes/association_ordering_spec.rb",
|
66
|
+
"spec/named_scopes/conditions_spec.rb",
|
67
|
+
"spec/named_scopes/or_conditions_spec.rb",
|
68
|
+
"spec/named_scopes/ordering_spec.rb",
|
69
|
+
"spec/search_spec.rb",
|
70
|
+
"spec/spec_helper.rb"
|
71
|
+
]
|
72
|
+
|
73
|
+
if s.respond_to? :specification_version then
|
74
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
75
|
+
s.specification_version = 3
|
76
|
+
|
77
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
78
|
+
s.add_runtime_dependency(%q<activerecord>, [">= 2.0.0"])
|
79
|
+
else
|
80
|
+
s.add_dependency(%q<activerecord>, [">= 2.0.0"])
|
81
|
+
end
|
82
|
+
else
|
83
|
+
s.add_dependency(%q<activerecord>, [">= 2.0.0"])
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
|
2
|
+
|
3
|
+
describe "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,19 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
|
2
|
+
|
3
|
+
describe "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
|
+
end
|
@@ -0,0 +1,188 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
|
2
|
+
|
3
|
+
describe "Association Conditions" 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 include optional associations" do
|
79
|
+
pending # this is a problem with using inner joins and left outer joins
|
80
|
+
Company.create
|
81
|
+
company = Company.create
|
82
|
+
user = company.users.create
|
83
|
+
order = user.orders.create(:total => 20, :taxes => 3)
|
84
|
+
Company.ascend_by_users_orders_total.all.should == Company.all
|
85
|
+
end
|
86
|
+
|
87
|
+
it "should implement exclusive scoping" do
|
88
|
+
scope = Company.users_company_name_like("name").users_company_description_like("description")
|
89
|
+
scope.scope(:find)[:joins].should == [
|
90
|
+
"INNER JOIN \"users\" ON companies.id = users.company_id",
|
91
|
+
"INNER JOIN \"companies\" companies_users ON \"companies_users\".id = \"users\".company_id"
|
92
|
+
]
|
93
|
+
lambda { scope.all }.should_not raise_error
|
94
|
+
end
|
95
|
+
|
96
|
+
it "should not create the same join twice" do
|
97
|
+
scope = Company.users_orders_total_gt(10).users_orders_taxes_lt(5).ascend_by_users_orders_total
|
98
|
+
scope.scope(:find)[:joins].should == [
|
99
|
+
"INNER JOIN \"users\" ON companies.id = users.company_id",
|
100
|
+
"INNER JOIN \"orders\" ON orders.user_id = users.id"
|
101
|
+
]
|
102
|
+
lambda { scope.count }.should_not raise_error
|
103
|
+
end
|
104
|
+
|
105
|
+
it "should not create the same join twice when traveling through the duplicate join" do
|
106
|
+
scope = Company.users_username_like("bjohnson").users_orders_total_gt(100)
|
107
|
+
scope.scope(:find)[:joins].should == [
|
108
|
+
"INNER JOIN \"users\" ON companies.id = users.company_id",
|
109
|
+
"INNER JOIN \"orders\" ON orders.user_id = users.id"
|
110
|
+
]
|
111
|
+
lambda { scope.count }.should_not raise_error
|
112
|
+
end
|
113
|
+
|
114
|
+
it "should not create the same join twice when traveling through the deep duplicate join" do
|
115
|
+
scope = Company.users_orders_total_gt(100).users_orders_line_items_price_gt(20)
|
116
|
+
scope.scope(:find)[:joins].should == [
|
117
|
+
"INNER JOIN \"users\" ON companies.id = users.company_id",
|
118
|
+
"INNER JOIN \"orders\" ON orders.user_id = users.id",
|
119
|
+
"INNER JOIN \"line_items\" ON line_items.order_id = orders.id"
|
120
|
+
]
|
121
|
+
lambda { scope.all }.should_not raise_error
|
122
|
+
end
|
123
|
+
|
124
|
+
it "should allow the use of :include when a join was created" do
|
125
|
+
company = Company.create
|
126
|
+
user = company.users.create
|
127
|
+
order = user.orders.create(:total => 20, :taxes => 3)
|
128
|
+
Company.users_orders_total_gt(10).users_orders_taxes_lt(5).ascend_by_users_orders_total.all(:include => :users).should == Company.all
|
129
|
+
end
|
130
|
+
|
131
|
+
it "should allow the use of deep :include when a join was created" do
|
132
|
+
company = Company.create
|
133
|
+
user = company.users.create
|
134
|
+
order = user.orders.create(:total => 20, :taxes => 3)
|
135
|
+
Company.users_orders_total_gt(10).users_orders_taxes_lt(5).ascend_by_users_orders_total.all(:include => {:users => :orders}).should == Company.all
|
136
|
+
end
|
137
|
+
|
138
|
+
it "should allow the use of :include when traveling through the duplicate join" do
|
139
|
+
company = Company.create
|
140
|
+
user = company.users.create(:username => "bjohnson")
|
141
|
+
order = user.orders.create(:total => 20, :taxes => 3)
|
142
|
+
Company.users_username_like("bjohnson").users_orders_taxes_lt(5).ascend_by_users_orders_total.all(:include => :users).should == Company.all
|
143
|
+
end
|
144
|
+
|
145
|
+
it "should allow the use of deep :include when traveling through the duplicate join" do
|
146
|
+
company = Company.create
|
147
|
+
user = company.users.create(:username => "bjohnson")
|
148
|
+
order = user.orders.create(:total => 20, :taxes => 3)
|
149
|
+
Company.users_orders_taxes_lt(50).ascend_by_users_orders_total.all(:include => {:users => :orders}).should == Company.all
|
150
|
+
end
|
151
|
+
|
152
|
+
it "should automatically add string joins if the association condition is using strings" do
|
153
|
+
User.named_scope(:orders_big_id, :joins => User.inner_joins(:orders))
|
154
|
+
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 "]}
|
155
|
+
end
|
156
|
+
|
157
|
+
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
|
158
|
+
company = Company.create
|
159
|
+
user = company.users.create(:company_id => company.id)
|
160
|
+
company.users.company_id_eq(company.id).should == [user]
|
161
|
+
end
|
162
|
+
|
163
|
+
it "should sanitize the scope on a foreign model instead of passing the raw options back to the original" do
|
164
|
+
Company.named_scope(:users_count_10, :conditions => {:users_count => 10})
|
165
|
+
User.company_users_count_10.proxy_options.should == {:conditions => "\"companies\".\"users_count\" = 10", :joins => :company}
|
166
|
+
end
|
167
|
+
|
168
|
+
it "should delegate to polymorphic relationships" do
|
169
|
+
Audit.auditable_user_type_name_like("ben").proxy_options.should == {
|
170
|
+
:conditions => ["users.name LIKE ?", "%ben%"],
|
171
|
+
:joins => "INNER JOIN \"users\" ON \"users\".id = \"audits\".auditable_id AND \"audits\".auditable_type = 'User'"
|
172
|
+
}
|
173
|
+
end
|
174
|
+
|
175
|
+
it "should delegate to polymorphic relationships (with a lazy split on _type_)" do
|
176
|
+
Audit.auditable_user_type_some_type_id_like("ben").proxy_options.should == {
|
177
|
+
:conditions => ["users.some_type_id LIKE ?", "%ben%"],
|
178
|
+
:joins => "INNER JOIN \"users\" ON \"users\".id = \"audits\".auditable_id AND \"audits\".auditable_type = 'User'"
|
179
|
+
}
|
180
|
+
end
|
181
|
+
|
182
|
+
it "should deep delegate to polymorphic relationships" do
|
183
|
+
Audit.auditable_user_type_company_name_like("company").proxy_options.should == {
|
184
|
+
:conditions => ["companies.name LIKE ?", "%company%"],
|
185
|
+
:joins => ["INNER JOIN \"users\" ON \"users\".id = \"audits\".auditable_id AND \"audits\".auditable_type = 'User'", " INNER JOIN \"companies\" ON \"companies\".id = \"users\".company_id "]
|
186
|
+
}
|
187
|
+
end
|
188
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
|
2
|
+
|
3
|
+
describe "Association 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 "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 == 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 == []
|
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 == User.find_all_by_username("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 == 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 == 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 == 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 == 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 == 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 == 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 == 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 == 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 == 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 == []
|
187
|
+
User.username_like_all("n", "o").all == User.find_all_by_username(["bjohnson", "thunt"])
|
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_all("bjohnson", "thunt").all == 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 == []
|
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 == 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 == []
|
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 == 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 == 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 == 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_arg_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_arg_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_arg_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
|