binarylogic-searchlogic 2.0.0
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 +191 -0
- data/Rakefile +48 -0
- data/VERSION.yml +4 -0
- data/init.rb +1 -0
- data/lib/searchlogic/core_ext/object.rb +39 -0
- data/lib/searchlogic/core_ext/proc.rb +11 -0
- data/lib/searchlogic/named_scopes/associations.rb +144 -0
- data/lib/searchlogic/named_scopes/conditions.rb +215 -0
- data/lib/searchlogic/named_scopes/ordering.rb +53 -0
- data/lib/searchlogic/rails_helpers.rb +63 -0
- data/lib/searchlogic/search.rb +128 -0
- data/lib/searchlogic.rb +18 -0
- data/rails/init.rb +1 -0
- data/spec/core_ext/object_spec.rb +7 -0
- data/spec/core_ext/proc_spec.rb +9 -0
- data/spec/named_scopes/associations_spec.rb +124 -0
- data/spec/named_scopes/conditions_spec.rb +253 -0
- data/spec/named_scopes/ordering_spec.rb +23 -0
- data/spec/search_spec.rb +251 -0
- data/spec/spec_helper.rb +77 -0
- metadata +81 -0
@@ -0,0 +1,253 @@
|
|
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
|
+
end
|
19
|
+
|
20
|
+
it "should have does not equal" do
|
21
|
+
(5..7).each { |age| User.create(:age => age) }
|
22
|
+
User.age_does_not_equal(6).all.should == User.find_all_by_age([5,7])
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should have less than" do
|
26
|
+
(5..7).each { |age| User.create(:age => age) }
|
27
|
+
User.age_less_than(6).all.should == User.find_all_by_age(5)
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should have less than or equal to" do
|
31
|
+
(5..7).each { |age| User.create(:age => age) }
|
32
|
+
User.age_less_than_or_equal_to(6).all.should == User.find_all_by_age([5, 6])
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should have greater than" do
|
36
|
+
(5..7).each { |age| User.create(:age => age) }
|
37
|
+
User.age_greater_than(6).all.should == User.find_all_by_age(7)
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should have greater than or equal to" do
|
41
|
+
(5..7).each { |age| User.create(:age => age) }
|
42
|
+
User.age_greater_than_or_equal_to(6).all.should == User.find_all_by_age([6, 7])
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
context "wildcard conditions" do
|
47
|
+
it "should have like" do
|
48
|
+
%w(bjohnson thunt).each { |username| User.create(:username => username) }
|
49
|
+
User.username_like("john").all.should == User.find_all_by_username("bjohnson")
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should have begins with" do
|
53
|
+
%w(bjohnson thunt).each { |username| User.create(:username => username) }
|
54
|
+
User.username_begins_with("bj").all.should == User.find_all_by_username("bjohnson")
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should have ends with" do
|
58
|
+
%w(bjohnson thunt).each { |username| User.create(:username => username) }
|
59
|
+
User.username_ends_with("son").all.should == User.find_all_by_username("bjohnson")
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
context "boolean conditions" do
|
64
|
+
it "should have null" do
|
65
|
+
["bjohnson", nil].each { |username| User.create(:username => username) }
|
66
|
+
User.username_null.all.should == User.find_all_by_username(nil)
|
67
|
+
end
|
68
|
+
|
69
|
+
it "should have empty" do
|
70
|
+
["bjohnson", ""].each { |username| User.create(:username => username) }
|
71
|
+
User.username_empty.all.should == User.find_all_by_username("")
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
context "any and all conditions" do
|
76
|
+
it "should do nothing if no arguments are passed" do
|
77
|
+
User.username_equals_any.proxy_options.should == {}
|
78
|
+
end
|
79
|
+
|
80
|
+
it "should have equals any" do
|
81
|
+
%w(bjohnson thunt dgainor).each { |username| User.create(:username => username) }
|
82
|
+
User.username_equals_any("bjohnson", "thunt").all == User.find_all_by_username(["bjohnson", "thunt"])
|
83
|
+
end
|
84
|
+
|
85
|
+
it "should have equals all" do
|
86
|
+
%w(bjohnson thunt dainor).each { |username| User.create(:username => username) }
|
87
|
+
User.username_equals_all("bjohnson", "thunt").all == []
|
88
|
+
end
|
89
|
+
|
90
|
+
it "should have does not equal any" do
|
91
|
+
%w(bjohnson thunt dgainor).each { |username| User.create(:username => username) }
|
92
|
+
User.username_does_not_equal_any("bjohnson", "thunt").all == User.find_all_by_username("dgainor")
|
93
|
+
end
|
94
|
+
|
95
|
+
it "should have does not equal all" do
|
96
|
+
%w(bjohnson thunt dgainor).each { |username| User.create(:username => username) }
|
97
|
+
User.username_does_not_equal_all("bjohnson", "thunt").all == User.find_all_by_username("dgainor")
|
98
|
+
end
|
99
|
+
|
100
|
+
it "should have less than any" do
|
101
|
+
(5..7).each { |age| User.create(:age => age) }
|
102
|
+
User.age_less_than_any(7,6).all == User.find_all_by_age([5, 6])
|
103
|
+
end
|
104
|
+
|
105
|
+
it "should have less than all" do
|
106
|
+
(5..7).each { |age| User.create(:age => age) }
|
107
|
+
User.age_less_than_all(7,6).all == User.find_all_by_age(5)
|
108
|
+
end
|
109
|
+
|
110
|
+
it "should have less than or equal to any" do
|
111
|
+
(5..7).each { |age| User.create(:age => age) }
|
112
|
+
User.age_less_than_or_equal_to_any(7,6).all == User.find_all_by_age([5, 6, 7])
|
113
|
+
end
|
114
|
+
|
115
|
+
it "should have less than or equal to all" do
|
116
|
+
(5..7).each { |age| User.create(:age => age) }
|
117
|
+
User.age_less_than_or_equal_to_all(7,6).all == User.find_all_by_age([5, 6])
|
118
|
+
end
|
119
|
+
|
120
|
+
it "should have less than any" do
|
121
|
+
(5..7).each { |age| User.create(:age => age) }
|
122
|
+
User.age_greater_than_any(5,6).all == User.find_all_by_age([6, 7])
|
123
|
+
end
|
124
|
+
|
125
|
+
it "should have greater than all" do
|
126
|
+
(5..7).each { |age| User.create(:age => age) }
|
127
|
+
User.age_greater_than_all(5,6).all == User.find_all_by_age(7)
|
128
|
+
end
|
129
|
+
|
130
|
+
it "should have greater than or equal to any" do
|
131
|
+
(5..7).each { |age| User.create(:age => age) }
|
132
|
+
User.age_greater_than_or_equal_to_any(5,6).all == User.find_all_by_age([5, 6, 7])
|
133
|
+
end
|
134
|
+
|
135
|
+
it "should have greater than or equal to all" do
|
136
|
+
(5..7).each { |age| User.create(:age => age) }
|
137
|
+
User.age_greater_than_or_equal_to_all(5,6).all == User.find_all_by_age([6, 7])
|
138
|
+
end
|
139
|
+
|
140
|
+
it "should have like all" do
|
141
|
+
%w(bjohnson thunt dgainor).each { |username| User.create(:username => username) }
|
142
|
+
User.username_like_all("bjohnson", "thunt").all == []
|
143
|
+
User.username_like_all("n", "o").all == User.find_all_by_username(["bjohnson", "thunt"])
|
144
|
+
end
|
145
|
+
|
146
|
+
it "should have like any" do
|
147
|
+
%w(bjohnson thunt dgainor).each { |username| User.create(:username => username) }
|
148
|
+
User.username_like_all("bjohnson", "thunt").all == User.find_all_by_username(["bjohnson", "thunt"])
|
149
|
+
end
|
150
|
+
|
151
|
+
it "should have begins with all" do
|
152
|
+
%w(bjohnson thunt dgainor).each { |username| User.create(:username => username) }
|
153
|
+
User.username_begins_with_all("bjohnson", "thunt").all == []
|
154
|
+
end
|
155
|
+
|
156
|
+
it "should have begins with any" do
|
157
|
+
%w(bjohnson thunt dgainor).each { |username| User.create(:username => username) }
|
158
|
+
User.username_begins_with_any("bj", "th").all == User.find_all_by_username(["bjohnson", "thunt"])
|
159
|
+
end
|
160
|
+
|
161
|
+
it "should have ends with all" do
|
162
|
+
%w(bjohnson thunt dgainor).each { |username| User.create(:username => username) }
|
163
|
+
User.username_ends_with_all("n", "r").all == []
|
164
|
+
end
|
165
|
+
|
166
|
+
it "should have ends with any" do
|
167
|
+
%w(bjohnson thunt dgainor).each { |username| User.create(:username => username) }
|
168
|
+
User.username_ends_with_any("n", "r").all == User.find_all_by_username(["bjohnson", "dgainor"])
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
context "alias conditions" do
|
173
|
+
it "should have is" do
|
174
|
+
User.age_is(5).proxy_options.should == User.age_equals(5).proxy_options
|
175
|
+
end
|
176
|
+
|
177
|
+
it "should have eq" do
|
178
|
+
User.age_eq(5).proxy_options.should == User.age_equals(5).proxy_options
|
179
|
+
end
|
180
|
+
|
181
|
+
it "should have not_equal_to" do
|
182
|
+
User.age_not_equal_to(5).proxy_options.should == User.age_does_not_equal(5).proxy_options
|
183
|
+
end
|
184
|
+
|
185
|
+
it "should have is_not" do
|
186
|
+
# This is matching "not" first. How do you give priority in a regex? Because it's matching the
|
187
|
+
# 'not' condition and thinking the column is 'age_is'.
|
188
|
+
pending
|
189
|
+
User.age_is_not(5).proxy_options.should == User.age_does_not_equal(5).proxy_options
|
190
|
+
end
|
191
|
+
|
192
|
+
it "should have not" do
|
193
|
+
User.age_not(5).proxy_options.should == User.age_does_not_equal(5).proxy_options
|
194
|
+
end
|
195
|
+
|
196
|
+
it "should have ne" do
|
197
|
+
User.age_ne(5).proxy_options.should == User.age_does_not_equal(5).proxy_options
|
198
|
+
end
|
199
|
+
|
200
|
+
it "should have lt" do
|
201
|
+
User.age_lt(5).proxy_options.should == User.age_less_than(5).proxy_options
|
202
|
+
end
|
203
|
+
|
204
|
+
it "should have lte" do
|
205
|
+
User.age_lte(5).proxy_options.should == User.age_less_than_or_equal_to(5).proxy_options
|
206
|
+
end
|
207
|
+
|
208
|
+
it "should have gt" do
|
209
|
+
User.age_gt(5).proxy_options.should == User.age_greater_than(5).proxy_options
|
210
|
+
end
|
211
|
+
|
212
|
+
it "should have gte" do
|
213
|
+
User.age_gte(5).proxy_options.should == User.age_greater_than_or_equal_to(5).proxy_options
|
214
|
+
end
|
215
|
+
|
216
|
+
it "should have contains" do
|
217
|
+
User.username_contains(5).proxy_options.should == User.username_like(5).proxy_options
|
218
|
+
end
|
219
|
+
|
220
|
+
it "should have contains" do
|
221
|
+
User.username_includes(5).proxy_options.should == User.username_like(5).proxy_options
|
222
|
+
end
|
223
|
+
|
224
|
+
it "should have bw" do
|
225
|
+
User.username_bw(5).proxy_options.should == User.username_begins_with(5).proxy_options
|
226
|
+
end
|
227
|
+
|
228
|
+
it "should have ew" do
|
229
|
+
User.username_ew(5).proxy_options.should == User.username_ends_with(5).proxy_options
|
230
|
+
end
|
231
|
+
|
232
|
+
it "should have nil" do
|
233
|
+
User.username_nil.proxy_options.should == User.username_nil.proxy_options
|
234
|
+
end
|
235
|
+
end
|
236
|
+
|
237
|
+
context "searchlogic lambda" do
|
238
|
+
it "should be a string" do
|
239
|
+
User.username_like("test")
|
240
|
+
User.named_scope_options(:username_like).searchlogic_arg_type.should == :string
|
241
|
+
end
|
242
|
+
|
243
|
+
it "should be an integer" do
|
244
|
+
User.id_gt(10)
|
245
|
+
User.named_scope_options(:id_gt).searchlogic_arg_type.should == :integer
|
246
|
+
end
|
247
|
+
|
248
|
+
it "should be a float" do
|
249
|
+
Order.total_gt(10)
|
250
|
+
Order.named_scope_options(:total_gt).searchlogic_arg_type.should == :float
|
251
|
+
end
|
252
|
+
end
|
253
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
|
2
|
+
|
3
|
+
describe "Ordering" do
|
4
|
+
it "should be dynamically created and then cached" do
|
5
|
+
User.should_not respond_to(:ascend_by_username)
|
6
|
+
User.ascend_by_username
|
7
|
+
User.should respond_to(:ascend_by_username)
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should have ascending" do
|
11
|
+
%w(bjohnson thunt).each { |username| User.create(:username => username) }
|
12
|
+
User.ascend_by_username.all.should == User.all(:order => "username ASC")
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should have descending" do
|
16
|
+
%w(bjohnson thunt).each { |username| User.create(:username => username) }
|
17
|
+
User.descend_by_username.all.should == User.all(:order => "username DESC")
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should have order" do
|
21
|
+
User.order("ascend_by_username").proxy_options.should == User.ascend_by_username.proxy_options
|
22
|
+
end
|
23
|
+
end
|
data/spec/search_spec.rb
ADDED
@@ -0,0 +1,251 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + "/spec_helper")
|
2
|
+
|
3
|
+
describe "Search" do
|
4
|
+
context "implementation" do
|
5
|
+
it "should create a search proxy" do
|
6
|
+
User.search(:username => "joe").should be_kind_of(Searchlogic::Search)
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should create a search proxy using the same class" do
|
10
|
+
User.search.klass.should == User
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should pass on the current scope to the proxy" do
|
14
|
+
company = Company.create
|
15
|
+
user = company.users.create
|
16
|
+
search = company.users.search
|
17
|
+
search.current_scope.should == company.users.scope(:find)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
context "initialization" do
|
22
|
+
it "should require a class" do
|
23
|
+
lambda { Searchlogic::Search.new }.should raise_error(ArgumentError)
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should set the conditions" do
|
27
|
+
search = User.search(:username => "bjohnson")
|
28
|
+
search.conditions.should == {:username => "bjohnson"}
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
context "conditions" do
|
33
|
+
it "should set the conditions and be accessible individually" do
|
34
|
+
search = User.search
|
35
|
+
search.conditions = {:username => "bjohnson"}
|
36
|
+
search.username.should == "bjohnson"
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should set the conditions and allow string keys" do
|
40
|
+
search = User.search
|
41
|
+
search.conditions = {"username" => "bjohnson"}
|
42
|
+
search.username.should == "bjohnson"
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should ignore blank values" do
|
46
|
+
search = User.search
|
47
|
+
search.conditions = {"username" => ""}
|
48
|
+
search.username.should be_nil
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should ignore blank values in arrays" do
|
52
|
+
search = User.search
|
53
|
+
search.conditions = {"username_equals_any" => [""]}
|
54
|
+
search.username_equals_any.should be_blank
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
context "condition accessors" do
|
59
|
+
it "should allow setting exact columns individually" do
|
60
|
+
search = User.search
|
61
|
+
search.username = "bjohnson"
|
62
|
+
search.username.should == "bjohnson"
|
63
|
+
end
|
64
|
+
|
65
|
+
it "should allow setting local column conditions individually" do
|
66
|
+
search = User.search
|
67
|
+
search.username_gt = "bjohnson"
|
68
|
+
search.username_gt.should == "bjohnson"
|
69
|
+
end
|
70
|
+
|
71
|
+
it "should allow setting association conditions" do
|
72
|
+
search = User.search
|
73
|
+
search.orders_total_gt = 10
|
74
|
+
search.orders_total_gt.should == 10
|
75
|
+
end
|
76
|
+
|
77
|
+
it "should allow using custom conditions" do
|
78
|
+
User.named_scope(:four_year_olds, { :conditions => { :age => 4 } })
|
79
|
+
search = User.search
|
80
|
+
search.four_year_olds = true
|
81
|
+
search.four_year_olds.should == true
|
82
|
+
search.proxy_options.should == User.four_year_olds.proxy_options
|
83
|
+
end
|
84
|
+
|
85
|
+
it "should not merge conflicting conditions into one value" do
|
86
|
+
# This class should JUST be a proxy. It should not do anything more than that.
|
87
|
+
# A user would be allowed to call both named scopes if they wanted.
|
88
|
+
search = User.search
|
89
|
+
search.username_greater_than = "bjohnson1"
|
90
|
+
search.username_gt = "bjohnson2"
|
91
|
+
search.username_greater_than.should == "bjohnson1"
|
92
|
+
search.username_gt.should == "bjohnson2"
|
93
|
+
end
|
94
|
+
|
95
|
+
it "should allow setting custom conditions individually" do
|
96
|
+
User.named_scope(:four_year_olds, :conditions => {:age => 4})
|
97
|
+
search = User.search
|
98
|
+
search.four_year_olds = true
|
99
|
+
search.four_year_olds.should == true
|
100
|
+
end
|
101
|
+
|
102
|
+
it "should not allow setting conditions that are not scopes" do
|
103
|
+
search = User.search
|
104
|
+
lambda { search.unknown = true }.should raise_error(Searchlogic::Search::UnknownConditionError)
|
105
|
+
end
|
106
|
+
|
107
|
+
context "type casting" do
|
108
|
+
it "should be a Boolean given true" do
|
109
|
+
search = User.search
|
110
|
+
search.id_nil = true
|
111
|
+
search.id_nil.should == true
|
112
|
+
end
|
113
|
+
|
114
|
+
it "should be a Boolean given 'true'" do
|
115
|
+
search = User.search
|
116
|
+
search.id_nil = "true"
|
117
|
+
search.id_nil.should == true
|
118
|
+
end
|
119
|
+
|
120
|
+
it "should be a Boolean given '1'" do
|
121
|
+
search = User.search
|
122
|
+
search.id_nil = "1"
|
123
|
+
search.id_nil.should == true
|
124
|
+
end
|
125
|
+
|
126
|
+
it "should be a Boolean given false" do
|
127
|
+
search = User.search
|
128
|
+
search.id_nil = false
|
129
|
+
search.id_nil.should == false
|
130
|
+
end
|
131
|
+
|
132
|
+
it "should be a Boolean given 'false'" do
|
133
|
+
search = User.search
|
134
|
+
search.id_nil = "false"
|
135
|
+
search.id_nil.should == false
|
136
|
+
end
|
137
|
+
|
138
|
+
it "should be a Boolean given '0'" do
|
139
|
+
search = User.search
|
140
|
+
search.id_nil = "0"
|
141
|
+
search.id_nil.should == false
|
142
|
+
end
|
143
|
+
|
144
|
+
it "should be an Integer given 1" do
|
145
|
+
search = User.search
|
146
|
+
search.id_gt = 1
|
147
|
+
search.id_gt.should == 1
|
148
|
+
end
|
149
|
+
|
150
|
+
it "should be an Integer given '1'" do
|
151
|
+
search = User.search
|
152
|
+
search.id_gt = "1"
|
153
|
+
search.id_gt.should == 1
|
154
|
+
end
|
155
|
+
|
156
|
+
it "should be a Float given 1.0" do
|
157
|
+
search = Order.search
|
158
|
+
search.total_gt = 1.0
|
159
|
+
search.total_gt.should == 1.0
|
160
|
+
end
|
161
|
+
|
162
|
+
it "should be a Float given '1'" do
|
163
|
+
search = Order.search
|
164
|
+
search.total_gt = "1"
|
165
|
+
search.total_gt.should == 1.0
|
166
|
+
end
|
167
|
+
|
168
|
+
it "should be a Float given '1.5'" do
|
169
|
+
search = Order.search
|
170
|
+
search.total_gt = "1.5"
|
171
|
+
search.total_gt.should == 1.5
|
172
|
+
end
|
173
|
+
|
174
|
+
it "should be a Date given 'Jan 1, 2009'" do
|
175
|
+
search = Order.search
|
176
|
+
search.shipped_on_after = "Jan 1, 2009"
|
177
|
+
search.shipped_on_after.should == Date.parse("Jan 1, 2009")
|
178
|
+
end
|
179
|
+
|
180
|
+
it "should be a Time given 'Jan 1, 2009'" do
|
181
|
+
search = Order.search
|
182
|
+
search.created_at_after = "Jan 1, 2009"
|
183
|
+
search.created_at_after.should == Time.parse("Jan 1, 2009")
|
184
|
+
end
|
185
|
+
|
186
|
+
it "should be a Time given 'Jan 1, 2009 9:33AM'" do
|
187
|
+
search = Order.search
|
188
|
+
search.created_at_after = "Jan 1, 2009 9:33AM"
|
189
|
+
search.created_at_after.should == Time.parse("Jan 1, 2009 9:33AM")
|
190
|
+
end
|
191
|
+
|
192
|
+
it "should be an Array and cast it's values given ['1', '2', '3']" do
|
193
|
+
search = Order.search
|
194
|
+
search.id_equals_any = ["1", "2", "3"]
|
195
|
+
search.id_equals_any.should == [1, 2, 3]
|
196
|
+
end
|
197
|
+
|
198
|
+
it "should type cast association conditions" do
|
199
|
+
search = User.search
|
200
|
+
search.orders_total_gt = "10"
|
201
|
+
search.orders_total_gt.should == 10
|
202
|
+
end
|
203
|
+
|
204
|
+
it "should type cast deep association conditions" do
|
205
|
+
search = Company.search
|
206
|
+
search.users_orders_total_gt = "10"
|
207
|
+
search.users_orders_total_gt.should == 10
|
208
|
+
end
|
209
|
+
end
|
210
|
+
end
|
211
|
+
|
212
|
+
context "taking action" do
|
213
|
+
it "should return all when not given any conditions" do
|
214
|
+
3.times { User.create }
|
215
|
+
User.search.all.length.should == 3
|
216
|
+
end
|
217
|
+
|
218
|
+
it "should implement the current scope based on an association" do
|
219
|
+
User.create
|
220
|
+
company = Company.create
|
221
|
+
user = company.users.create
|
222
|
+
company.users.search.all.should == [user]
|
223
|
+
end
|
224
|
+
|
225
|
+
it "should implement the current scope based on a named scope" do
|
226
|
+
User.named_scope(:four_year_olds, :conditions => {:age => 4})
|
227
|
+
(3..5).each { |age| User.create(:age => age) }
|
228
|
+
User.four_year_olds.search.all.should == User.find_all_by_age(4)
|
229
|
+
end
|
230
|
+
|
231
|
+
it "should call named scopes for conditions" do
|
232
|
+
User.search(:age_less_than => 5).proxy_options.should == User.age_less_than(5).proxy_options
|
233
|
+
end
|
234
|
+
|
235
|
+
it "should alias exact column names to use equals" do
|
236
|
+
User.search(:username => "joe").proxy_options.should == User.username_equals("joe").proxy_options
|
237
|
+
end
|
238
|
+
|
239
|
+
it "should recognize conditions with a value of true where the named scope has an arity of 0" do
|
240
|
+
User.search(:username_nil => true).proxy_options.should == User.username_nil.proxy_options
|
241
|
+
end
|
242
|
+
|
243
|
+
it "should ignore conditions with a value of false where the named scope has an arity of 0" do
|
244
|
+
User.search(:username_nil => false).proxy_options.should == {}
|
245
|
+
end
|
246
|
+
|
247
|
+
it "should recognize the order condition" do
|
248
|
+
User.search(:order => "ascend_by_username").proxy_options.should == User.ascend_by_username.proxy_options
|
249
|
+
end
|
250
|
+
end
|
251
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
require 'spec'
|
2
|
+
require 'rubygems'
|
3
|
+
require 'ruby-debug'
|
4
|
+
require 'activerecord'
|
5
|
+
|
6
|
+
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :dbfile => ":memory:")
|
7
|
+
ActiveRecord::Base.configurations = true
|
8
|
+
|
9
|
+
ActiveRecord::Schema.verbose = false
|
10
|
+
ActiveRecord::Schema.define(:version => 1) do
|
11
|
+
create_table :companies do |t|
|
12
|
+
t.datetime :created_at
|
13
|
+
t.datetime :updated_at
|
14
|
+
end
|
15
|
+
|
16
|
+
create_table :users do |t|
|
17
|
+
t.datetime :created_at
|
18
|
+
t.datetime :updated_at
|
19
|
+
t.integer :company_id
|
20
|
+
t.string :username
|
21
|
+
t.integer :age
|
22
|
+
end
|
23
|
+
|
24
|
+
create_table :orders do |t|
|
25
|
+
t.datetime :created_at
|
26
|
+
t.datetime :updated_at
|
27
|
+
t.integer :user_id
|
28
|
+
t.date :shipped_on
|
29
|
+
t.float :taxes
|
30
|
+
t.float :total
|
31
|
+
end
|
32
|
+
|
33
|
+
create_table :line_items do |t|
|
34
|
+
t.datetime :created_at
|
35
|
+
t.datetime :updated_at
|
36
|
+
t.integer :order_id
|
37
|
+
t.float :price
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
42
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
43
|
+
require 'searchlogic'
|
44
|
+
|
45
|
+
Spec::Runner.configure do |config|
|
46
|
+
config.before(:each) do
|
47
|
+
class Company < ActiveRecord::Base
|
48
|
+
has_many :users, :dependent => :destroy
|
49
|
+
end
|
50
|
+
|
51
|
+
class User < ActiveRecord::Base
|
52
|
+
belongs_to :company
|
53
|
+
has_many :orders, :dependent => :destroy
|
54
|
+
end
|
55
|
+
|
56
|
+
class Order < ActiveRecord::Base
|
57
|
+
belongs_to :user
|
58
|
+
has_many :line_items, :dependent => :destroy
|
59
|
+
end
|
60
|
+
|
61
|
+
class LineItem < ActiveRecord::Base
|
62
|
+
belongs_to :order
|
63
|
+
end
|
64
|
+
|
65
|
+
Company.destroy_all
|
66
|
+
User.destroy_all
|
67
|
+
Order.destroy_all
|
68
|
+
LineItem.destroy_all
|
69
|
+
end
|
70
|
+
|
71
|
+
config.after(:each) do
|
72
|
+
Object.send(:remove_const, :Company)
|
73
|
+
Object.send(:remove_const, :User)
|
74
|
+
Object.send(:remove_const, :Order)
|
75
|
+
Object.send(:remove_const, :LineItem)
|
76
|
+
end
|
77
|
+
end
|
metadata
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: binarylogic-searchlogic
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 2.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- binarylogic
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-06-15 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: bjohnson@binarylogic.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- LICENSE
|
24
|
+
- README.rdoc
|
25
|
+
files:
|
26
|
+
- .gitignore
|
27
|
+
- LICENSE
|
28
|
+
- README.rdoc
|
29
|
+
- Rakefile
|
30
|
+
- VERSION.yml
|
31
|
+
- init.rb
|
32
|
+
- lib/searchlogic.rb
|
33
|
+
- lib/searchlogic/core_ext/object.rb
|
34
|
+
- lib/searchlogic/core_ext/proc.rb
|
35
|
+
- lib/searchlogic/named_scopes/associations.rb
|
36
|
+
- lib/searchlogic/named_scopes/conditions.rb
|
37
|
+
- lib/searchlogic/named_scopes/ordering.rb
|
38
|
+
- lib/searchlogic/rails_helpers.rb
|
39
|
+
- lib/searchlogic/search.rb
|
40
|
+
- rails/init.rb
|
41
|
+
- spec/core_ext/object_spec.rb
|
42
|
+
- spec/core_ext/proc_spec.rb
|
43
|
+
- spec/named_scopes/associations_spec.rb
|
44
|
+
- spec/named_scopes/conditions_spec.rb
|
45
|
+
- spec/named_scopes/ordering_spec.rb
|
46
|
+
- spec/search_spec.rb
|
47
|
+
- spec/spec_helper.rb
|
48
|
+
has_rdoc: false
|
49
|
+
homepage: http://github.com/binarylogic/searchlogic
|
50
|
+
post_install_message:
|
51
|
+
rdoc_options:
|
52
|
+
- --charset=UTF-8
|
53
|
+
require_paths:
|
54
|
+
- lib
|
55
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: "0"
|
60
|
+
version:
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: "0"
|
66
|
+
version:
|
67
|
+
requirements: []
|
68
|
+
|
69
|
+
rubyforge_project: test
|
70
|
+
rubygems_version: 1.2.0
|
71
|
+
signing_key:
|
72
|
+
specification_version: 3
|
73
|
+
summary: TODO
|
74
|
+
test_files:
|
75
|
+
- spec/core_ext/object_spec.rb
|
76
|
+
- spec/core_ext/proc_spec.rb
|
77
|
+
- spec/named_scopes/associations_spec.rb
|
78
|
+
- spec/named_scopes/conditions_spec.rb
|
79
|
+
- spec/named_scopes/ordering_spec.rb
|
80
|
+
- spec/search_spec.rb
|
81
|
+
- spec/spec_helper.rb
|