binarylogic-search 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/rails/init.rb ADDED
@@ -0,0 +1 @@
1
+ require "searchlogic"
@@ -0,0 +1,7 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
2
+
3
+ describe "Object" do
4
+ it "should accept and pass the argument to the searchlogic_arg_type" do
5
+ searchlogic_lambda(:integer) {}.searchlogic_arg_type.should == :integer
6
+ end
7
+ end
@@ -0,0 +1,9 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
2
+
3
+ describe "Proc" do
4
+ it "should have a searchlogic_arg_type accessor" do
5
+ p = Proc.new {}
6
+ p.searchlogic_arg_type = :integer
7
+ p.searchlogic_arg_type.should == :integer
8
+ end
9
+ end
@@ -0,0 +1,124 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
2
+
3
+ describe "Associations" do
4
+ before(:each) do
5
+ @users_join_sql = ["LEFT OUTER JOIN \"users\" ON users.company_id = companies.id"]
6
+ @orders_join_sql = ["LEFT OUTER JOIN \"users\" ON users.company_id = companies.id", "LEFT OUTER JOIN \"orders\" ON orders.user_id = users.id"]
7
+ end
8
+
9
+ it "should create a named scope" do
10
+ Company.users_username_like("bjohnson").proxy_options.should == User.username_like("bjohnson").proxy_options.merge(:joins => @users_join_sql)
11
+ end
12
+
13
+ it "should create a deep named scope" do
14
+ Company.users_orders_total_greater_than(10).proxy_options.should == Order.total_greater_than(10).proxy_options.merge(:joins => @orders_join_sql)
15
+ end
16
+
17
+ it "should not allowed named scopes on non existent association columns" do
18
+ lambda { User.users_whatever_like("bjohnson") }.should raise_error(NoMethodError)
19
+ end
20
+
21
+ it "should not allowed named scopes on non existent deep association columns" do
22
+ lambda { User.users_orders_whatever_like("bjohnson") }.should raise_error(NoMethodError)
23
+ end
24
+
25
+ it "should allow named scopes to be called multiple times and reflect the value passed" do
26
+ Company.users_username_like("bjohnson").proxy_options.should == User.username_like("bjohnson").proxy_options.merge(:joins => @users_join_sql)
27
+ Company.users_username_like("thunt").proxy_options.should == User.username_like("thunt").proxy_options.merge(:joins => @users_join_sql)
28
+ end
29
+
30
+ it "should allow deep named scopes to be called multiple times and reflect the value passed" do
31
+ Company.users_orders_total_greater_than(10).proxy_options.should == Order.total_greater_than(10).proxy_options.merge(:joins => @orders_join_sql)
32
+ Company.users_orders_total_greater_than(20).proxy_options.should == Order.total_greater_than(20).proxy_options.merge(:joins => @orders_join_sql)
33
+ end
34
+
35
+ it "should have an arity of 1 if the underlying scope has an arity of 1" do
36
+ Company.users_orders_total_greater_than(10)
37
+ Company.named_scope_arity("users_orders_total_greater_than").should == Order.named_scope_arity("total_greater_than")
38
+ end
39
+
40
+ it "should have an arity of nil if the underlying scope has an arity of nil" do
41
+ Company.users_orders_total_null
42
+ Company.named_scope_arity("users_orders_total_null").should == Order.named_scope_arity("total_null")
43
+ end
44
+
45
+ it "should have an arity of -1 if the underlying scope has an arity of -1" do
46
+ Company.users_id_equals_any
47
+ Company.named_scope_arity("users_id_equals_any").should == User.named_scope_arity("id_equals_any")
48
+ end
49
+
50
+ it "should allow aliases" do
51
+ Company.users_username_contains("bjohnson").proxy_options.should == User.username_contains("bjohnson").proxy_options.merge(:joins => @users_join_sql)
52
+ end
53
+
54
+ it "should allow deep aliases" do
55
+ Company.users_orders_total_gt(10).proxy_options.should == Order.total_gt(10).proxy_options.merge(:joins => @orders_join_sql)
56
+ end
57
+
58
+ it "should allow ascending" do
59
+ Company.ascend_by_users_username.proxy_options.should == User.ascend_by_username.proxy_options.merge(:joins => @users_join_sql)
60
+ end
61
+
62
+ it "should allow descending" do
63
+ Company.descend_by_users_username.proxy_options.should == User.descend_by_username.proxy_options.merge(:joins => @users_join_sql)
64
+ end
65
+
66
+ it "should allow deep ascending" do
67
+ Company.ascend_by_users_orders_total.proxy_options.should == Order.ascend_by_total.proxy_options.merge(:joins => @orders_join_sql)
68
+ end
69
+
70
+ it "should allow deep descending" do
71
+ Company.descend_by_users_orders_total.proxy_options.should == Order.descend_by_total.proxy_options.merge(:joins => @orders_join_sql)
72
+ end
73
+
74
+ it "should include optional associations" do
75
+ Company.create
76
+ company = Company.create
77
+ user = company.users.create
78
+ order = user.orders.create(:total => 20, :taxes => 3)
79
+ Company.ascend_by_users_orders_total.all.should == Company.all
80
+ end
81
+
82
+ it "should not create the same join twice" do
83
+ company = Company.create
84
+ user = company.users.create
85
+ order = user.orders.create(:total => 20, :taxes => 3)
86
+ Company.users_orders_total_gt(10).users_orders_taxes_lt(5).ascend_by_users_orders_total.all.should == Company.all
87
+ end
88
+
89
+ it "should not create the same join twice when traveling through the duplicate join" do
90
+ Company.users_username_like("bjohnson").users_orders_total_gt(100).all.should == Company.all
91
+ end
92
+
93
+ it "should not create the same join twice when traveling through the duplicate join 2" do
94
+ Company.users_orders_total_gt(100).users_orders_line_items_price_gt(20).all.should == Company.all
95
+ end
96
+
97
+ it "should allow the use of :include when a join was created" do
98
+ company = Company.create
99
+ user = company.users.create
100
+ order = user.orders.create(:total => 20, :taxes => 3)
101
+ Company.users_orders_total_gt(10).users_orders_taxes_lt(5).ascend_by_users_orders_total.all(:include => :users).should == Company.all
102
+ end
103
+
104
+ it "should allow the use of deep :include when a join was created" do
105
+ company = Company.create
106
+ user = company.users.create
107
+ order = user.orders.create(:total => 20, :taxes => 3)
108
+ Company.users_orders_total_gt(10).users_orders_taxes_lt(5).ascend_by_users_orders_total.all(:include => {:users => :orders}).should == Company.all
109
+ end
110
+
111
+ it "should allow the use of :include when traveling through the duplicate join" do
112
+ company = Company.create
113
+ user = company.users.create(:username => "bjohnson")
114
+ order = user.orders.create(:total => 20, :taxes => 3)
115
+ Company.users_username_like("bjohnson").users_orders_taxes_lt(5).ascend_by_users_orders_total.all(:include => :users).should == Company.all
116
+ end
117
+
118
+ it "should allow the use of deep :include when traveling through the duplicate join" do
119
+ company = Company.create
120
+ user = company.users.create(:username => "bjohnson")
121
+ order = user.orders.create(:total => 20, :taxes => 3)
122
+ Company.ascend_by_users_orders_total.users_orders_taxes_lt(50).all(:include => {:users => :orders}).should == Company.all
123
+ end
124
+ end
@@ -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
@@ -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