by_star 0.6.0 → 0.6.1
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/by_star.gemspec +2 -2
- data/lib/vanilla.rb +14 -3
- data/spec/by_star_spec.rb +470 -460
- data/spec/fixtures/models.rb +7 -5
- data/spec/spec_helper.rb +13 -20
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.6.
|
1
|
+
0.6.1
|
data/by_star.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{by_star}
|
8
|
-
s.version = "0.6.
|
8
|
+
s.version = "0.6.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Ryan Bigg", "Mislav Marohni\304\207"]
|
12
|
-
s.date = %q{2010-01-
|
12
|
+
s.date = %q{2010-01-29}
|
13
13
|
s.description = %q{ActiveRecord extension for easier date scopes and time ranges}
|
14
14
|
s.email = %q{radarlistener@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
data/lib/vanilla.rb
CHANGED
@@ -102,7 +102,8 @@ module ByStar
|
|
102
102
|
def by_weekend(time=Time.zone.now, options = {}, &block)
|
103
103
|
time = parse(time)
|
104
104
|
start_time = time.beginning_of_weekend
|
105
|
-
|
105
|
+
end_time = (start_time + 1.day).end_of_day
|
106
|
+
by_star(start_time, end_time, options, &block)
|
106
107
|
end
|
107
108
|
|
108
109
|
|
@@ -181,7 +182,7 @@ module ByStar
|
|
181
182
|
def by_direction(condition, time, options = {}, &block)
|
182
183
|
field = connection.quote_table_name(table_name)
|
183
184
|
field << "." << connection.quote_column_name(options.delete(:field) || "created_at")
|
184
|
-
|
185
|
+
ensure_valid_options(options)
|
185
186
|
scoping = { :conditions => ["#{field} #{condition} ?", time.utc] }.merge(options)
|
186
187
|
with_scope(:find => scoping) do
|
187
188
|
scoped_by(block) do
|
@@ -194,10 +195,12 @@ module ByStar
|
|
194
195
|
def by_star(start_time, end_time, options = {}, &block)
|
195
196
|
start_time = parse(start_time)
|
196
197
|
end_time = parse(end_time)
|
198
|
+
|
197
199
|
|
198
200
|
raise ParseError, "End time is before start time, searching like this will return no results." if end_time < start_time
|
199
201
|
field = options.delete(:field)
|
200
|
-
|
202
|
+
ensure_valid_options(options)
|
203
|
+
|
201
204
|
scoping = { :conditions => conditions_for_range(start_time, end_time, field) }.merge(options)
|
202
205
|
with_scope(:find => scoping) do
|
203
206
|
scoped_by(block) do
|
@@ -205,6 +208,14 @@ module ByStar
|
|
205
208
|
end
|
206
209
|
end
|
207
210
|
end
|
211
|
+
|
212
|
+
def ensure_valid_options(options)
|
213
|
+
if respond_to?(:validate_find_options)
|
214
|
+
validate_find_options(options)
|
215
|
+
else
|
216
|
+
options.assert_valid_keys(ActiveRecord::SpawnMethods::VALID_FIND_OPTIONS)
|
217
|
+
end
|
218
|
+
end
|
208
219
|
|
209
220
|
alias :between :by_star
|
210
221
|
public :between
|
data/spec/by_star_spec.rb
CHANGED
@@ -1,617 +1,627 @@
|
|
1
1
|
require File.join(File.dirname(__FILE__), 'spec_helper')
|
2
2
|
require 'by_star'
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
4
|
+
for zone in ['UTC', 'Eastern Time (US & Canada)']
|
5
|
+
# Define time zone before loading test_helper
|
6
|
+
Time.zone = zone
|
7
|
+
ActiveRecord::Base.default_timezone = zone
|
8
|
+
|
9
|
+
YAML::load_file(File.dirname(__FILE__) + "/database.yml").each do |key, connection|
|
10
|
+
ActiveRecord::Base.establish_connection(connection)
|
11
|
+
load File.dirname(__FILE__) + "/fixtures/schema.rb"
|
12
|
+
load File.dirname(__FILE__) + "/fixtures/models.rb"
|
10
13
|
end
|
14
|
+
|
15
|
+
describe Post do
|
16
|
+
def stub_time(day=1, month=1, year=Time.zone.now.year, hour=0, minute=0)
|
17
|
+
stub = "#{day}-#{month}-#{year} #{hour}:#{minute}".to_time
|
18
|
+
Time.stub!(:now).and_return(stub)
|
19
|
+
Time.zone.stub!(:now).and_return(stub)
|
20
|
+
end
|
11
21
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
22
|
+
def range_test(&block)
|
23
|
+
(1..31).to_a.each do |d|
|
24
|
+
stub_time(d, 07, 2009, 05, 05)
|
25
|
+
block.call
|
26
|
+
end
|
16
27
|
end
|
17
|
-
end
|
18
28
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
29
|
+
def find(*args)
|
30
|
+
method = description_args.first.sub(' ', '_')
|
31
|
+
Post.send(method, *args)
|
32
|
+
end
|
23
33
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
34
|
+
def size(*args)
|
35
|
+
method = description_args.first.sub(' ', '_')
|
36
|
+
Post.send(method, *args).size
|
37
|
+
end
|
28
38
|
|
29
|
-
|
30
|
-
|
39
|
+
["mysql", "sqlite3"].each do |adapter|
|
40
|
+
ActiveRecord::Base.establish_connection(YAML::load_file(File.dirname(__FILE__) + "/database.yml")[adapter])
|
31
41
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
42
|
+
describe "by year" do
|
43
|
+
it "should be able to find all the posts in the current year" do
|
44
|
+
size.should eql(Post.count - 1)
|
45
|
+
end
|
36
46
|
|
37
|
-
|
38
|
-
|
39
|
-
|
47
|
+
it "should be able to find if given a string" do
|
48
|
+
size(Time.zone.now.year.to_s).should eql(Post.count - 1)
|
49
|
+
end
|
40
50
|
|
41
|
-
|
42
|
-
|
43
|
-
|
51
|
+
it "should be able to find a single post from last year" do
|
52
|
+
size(Time.zone.now.year-1).should eql(1)
|
53
|
+
end
|
44
54
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
+
it "should error when given an invalid year" do
|
56
|
+
# This is broken on 1.8.6 (and previous versions), any patchlevel after & before 111
|
57
|
+
major, minor, trivial = RUBY_VERSION.split(".").map(&:to_i)
|
58
|
+
if major == 1 && ((minor == 8 && trivial <= 6) || (minor <= 8)) && RUBY_PATCHLEVEL.to_i > 111
|
59
|
+
lambda { find(1901) }.should raise_error(ByStar::ParseError, "Invalid arguments detected, year may possibly be outside of valid range (1902-2039)")
|
60
|
+
lambda { find(2039) }.should raise_error(ByStar::ParseError, "Invalid arguments detected, year may possibly be outside of valid range (1902-2039)")
|
61
|
+
else
|
62
|
+
find(1456).should be_empty
|
63
|
+
find(1901).should be_empty
|
64
|
+
find(2039).should be_empty
|
65
|
+
end
|
55
66
|
end
|
56
|
-
end
|
57
67
|
|
58
|
-
|
59
|
-
|
60
|
-
|
68
|
+
it "should be able to use an alternative field (string)" do
|
69
|
+
Event.by_year(nil, :field => "start_time").size.should eql(8)
|
70
|
+
end
|
61
71
|
|
62
|
-
|
63
|
-
|
64
|
-
|
72
|
+
it "should be able to use an alternative field (symbol)" do
|
73
|
+
Event.by_year(nil, :field => :start_time).size.should eql(8)
|
74
|
+
end
|
65
75
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
76
|
+
it "should be able to use an alternative field (symbol) with directional searching" do
|
77
|
+
stub_time
|
78
|
+
Event.past(nil, :field => :start_time).size.should eql(1)
|
79
|
+
end
|
70
80
|
|
71
|
-
|
72
|
-
|
81
|
+
it "should be able to order the result set" do
|
82
|
+
find(Time.zone.now.year, :order => "created_at DESC").first.text.should eql("That's it!")
|
83
|
+
end
|
73
84
|
end
|
74
|
-
end
|
75
85
|
|
76
|
-
|
86
|
+
describe "by month" do
|
77
87
|
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
88
|
+
it "should be able to find posts for the current month" do
|
89
|
+
stub_time
|
90
|
+
size.should eql(10)
|
91
|
+
end
|
82
92
|
|
83
|
-
|
84
|
-
|
85
|
-
|
93
|
+
it "should be able to find a single post for January" do
|
94
|
+
size("January").should eql(10)
|
95
|
+
end
|
86
96
|
|
87
|
-
|
88
|
-
|
89
|
-
|
97
|
+
it "should be able to find two posts for the 2nd month" do
|
98
|
+
size(2).should eql(2)
|
99
|
+
end
|
90
100
|
|
91
|
-
|
92
|
-
|
93
|
-
|
101
|
+
it "should be able to find three posts for the 3rd month, using time instance" do
|
102
|
+
# Hack... if we're running this test during march there's going to be more posts than usual.
|
103
|
+
# This is due to the #today, #yesterday and #tomorrow methods.
|
94
104
|
|
95
|
-
|
96
|
-
|
105
|
+
size(Time.local(Time.zone.now.year, 3, 1)).should eql(3)
|
106
|
+
end
|
97
107
|
|
98
|
-
|
99
|
-
|
100
|
-
|
108
|
+
it "should be able to find a single post from January last year" do
|
109
|
+
size("January", :year => Time.zone.now.year - 1).should eql(1)
|
110
|
+
end
|
101
111
|
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
112
|
+
it "should fail when given incorrect months" do
|
113
|
+
lambda { find(0) }.should raise_error(ByStar::ParseError)
|
114
|
+
lambda { find(13) }.should raise_error(ByStar::ParseError)
|
115
|
+
lambda { find("Ryan") }.should raise_error(ByStar::ParseError)
|
116
|
+
lambda { find([1,2,3]) }.should raise_error(ByStar::ParseError)
|
117
|
+
end
|
108
118
|
|
109
|
-
|
110
|
-
|
111
|
-
|
119
|
+
it "should be able to take decimals" do
|
120
|
+
size(1.5).should eql(10)
|
121
|
+
end
|
112
122
|
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
123
|
+
it "should be able to use an alternative field" do
|
124
|
+
if Time.zone.now.month == 12
|
125
|
+
Event.by_month(nil, :field => "start_time").size.should eql(4)
|
126
|
+
else
|
127
|
+
stub_time(1, 12)
|
128
|
+
Event.by_month(nil, :field => "start_time").size.should eql(1)
|
129
|
+
end
|
119
130
|
end
|
120
|
-
end
|
121
131
|
|
122
|
-
|
123
|
-
|
124
|
-
|
132
|
+
it "should be able to specify the year as a string" do
|
133
|
+
size(1, :year => (Time.zone.now.year - 1).to_s).should eql(1)
|
134
|
+
end
|
125
135
|
|
126
|
-
|
136
|
+
end
|
127
137
|
|
128
|
-
|
138
|
+
describe "by fortnight" do
|
129
139
|
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
140
|
+
it "should be able to find posts in the current fortnight" do
|
141
|
+
stub_time
|
142
|
+
size.should eql(4)
|
143
|
+
end
|
134
144
|
|
135
|
-
|
136
|
-
|
137
|
-
|
145
|
+
it "should be able to find posts in the 1st fortnight" do
|
146
|
+
size(0).should eql(4)
|
147
|
+
end
|
138
148
|
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
149
|
+
it "should be able to find posts for a fortnight ago" do
|
150
|
+
stub_time
|
151
|
+
size(2.weeks.ago).should eql(0)
|
152
|
+
end
|
143
153
|
|
144
|
-
|
145
|
-
|
146
|
-
|
154
|
+
it "should raise an error when given an invalid argument" do
|
155
|
+
lambda { find(27) }.should raise_error(ByStar::ParseError, "by_fortnight takes only a Time or Date object, a Fixnum (less than or equal to 26) or a Chronicable string.")
|
156
|
+
end
|
147
157
|
|
148
|
-
|
149
|
-
|
150
|
-
|
158
|
+
it "should be able to use an alternative field" do
|
159
|
+
stub_time
|
160
|
+
Event.by_fortnight(nil, :field => "start_time").size.should eql(0)
|
161
|
+
end
|
151
162
|
end
|
152
|
-
end
|
153
163
|
|
154
|
-
|
164
|
+
describe "by week" do
|
155
165
|
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
166
|
+
it "should be able to find posts in the current week" do
|
167
|
+
stub_time
|
168
|
+
size.should eql(4)
|
169
|
+
end
|
160
170
|
|
161
|
-
|
162
|
-
|
163
|
-
|
171
|
+
it "should be able to find posts in the 1st week" do
|
172
|
+
size(0).should eql(4)
|
173
|
+
end
|
164
174
|
|
165
|
-
|
166
|
-
|
167
|
-
|
175
|
+
it "should be able to find posts in the 1st week of last year" do
|
176
|
+
size(0, :year => Time.zone.now.year-1).should eql(1)
|
177
|
+
end
|
168
178
|
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
179
|
+
it "should not find any posts from a week ago" do
|
180
|
+
stub_time
|
181
|
+
size(1.week.ago).should eql(0)
|
182
|
+
end
|
173
183
|
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
184
|
+
it "should be able to find posts by a given date" do
|
185
|
+
stub_time
|
186
|
+
size(1.week.ago.to_date).should eql(0)
|
187
|
+
end
|
178
188
|
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
189
|
+
it "should find, not size the posts for the current week" do
|
190
|
+
stub_time
|
191
|
+
find.map(&:text).include?("The 'Current' Week")
|
192
|
+
find.map(&:text).include?("Weekend of May")
|
193
|
+
end
|
184
194
|
|
185
|
-
|
186
|
-
|
187
|
-
|
195
|
+
it "should raise an error when given an invalid argument" do
|
196
|
+
lambda { find(54) }.should raise_error(ByStar::ParseError, "by_week takes only a Time or Date object, a Fixnum (less than or equal to 53) or a Chronicable string.")
|
197
|
+
end
|
188
198
|
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
199
|
+
it "should be able to use an alternative field" do
|
200
|
+
stub_time
|
201
|
+
Event.by_week(nil, :field => "start_time").size.should eql(0)
|
202
|
+
end
|
193
203
|
|
194
|
-
|
195
|
-
|
196
|
-
|
204
|
+
it "should find posts at the start of the year" do
|
205
|
+
size(0).should eql(4)
|
206
|
+
end
|
197
207
|
|
198
|
-
|
199
|
-
|
200
|
-
|
208
|
+
it "should find posts at the end of the year" do
|
209
|
+
size(Time.zone.now.end_of_year).should eql(1)
|
210
|
+
end
|
201
211
|
|
202
|
-
end
|
203
|
-
|
204
|
-
describe "by weekend" do
|
205
|
-
it "should be able to find the posts on the weekend of the 1st of January" do
|
206
|
-
stub_time
|
207
|
-
size.should eql(1)
|
208
212
|
end
|
213
|
+
|
214
|
+
describe "by weekend" do
|
215
|
+
it "should be able to find the posts on the weekend of the 1st of January" do
|
216
|
+
stub_time
|
217
|
+
size.should eql(1)
|
218
|
+
end
|
209
219
|
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
220
|
+
it "should be able to use an alternative field" do
|
221
|
+
year = Time.zone.now.year
|
222
|
+
stub_time(1, 8)
|
223
|
+
Event.by_weekend(nil, :field => "start_time").size.should eql(0)
|
224
|
+
end
|
214
225
|
end
|
215
|
-
end
|
216
226
|
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
227
|
+
describe "by current weekend" do
|
228
|
+
it "should work" do
|
229
|
+
range_test do
|
230
|
+
Post.by_current_weekend
|
231
|
+
end
|
221
232
|
end
|
222
233
|
end
|
223
|
-
end
|
224
234
|
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
235
|
+
describe "by current work week" do
|
236
|
+
it "should work" do
|
237
|
+
range_test do
|
238
|
+
Post.by_current_work_week
|
239
|
+
end
|
229
240
|
end
|
230
241
|
end
|
231
|
-
end
|
232
242
|
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
243
|
+
describe "by day" do
|
244
|
+
it "should be able to find a post for today" do
|
245
|
+
stub_time
|
246
|
+
size.should eql(3)
|
247
|
+
end
|
238
248
|
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
249
|
+
it "should be able to find a post by a given date" do
|
250
|
+
stub_time
|
251
|
+
size(Date.today).should eql(3)
|
252
|
+
end
|
243
253
|
|
244
|
-
|
245
|
-
|
254
|
+
it "should be able to use an alternative field" do
|
255
|
+
Event.by_day(Time.now - 1.day, :field => "start_time").size.should eql(1)
|
256
|
+
end
|
246
257
|
end
|
247
|
-
end
|
248
258
|
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
259
|
+
describe "today" do
|
260
|
+
it "should show the post for today" do
|
261
|
+
find.map(&:text).should include("Today's post")
|
262
|
+
end
|
253
263
|
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
264
|
+
it "should be able to use an alternative field" do
|
265
|
+
# Test may occur on an event day.
|
266
|
+
stub_time
|
267
|
+
Event.today(nil, :field => "start_time").size.should eql(0)
|
268
|
+
end
|
259
269
|
|
260
|
-
|
270
|
+
end
|
261
271
|
|
262
|
-
|
263
|
-
|
264
|
-
|
272
|
+
describe "tomorrow" do
|
273
|
+
it "should show the post for tomorrow" do
|
274
|
+
find.map(&:text).should include("Tomorrow's post")
|
275
|
+
end
|
265
276
|
end
|
266
|
-
end
|
267
277
|
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
278
|
+
describe "yesterday" do
|
279
|
+
it "should show the post for yesterday" do
|
280
|
+
find.map(&:text).should include("Yesterday's post")
|
281
|
+
end
|
272
282
|
|
273
|
-
|
274
|
-
|
275
|
-
|
283
|
+
it "should be able find yesterday, given a Date" do
|
284
|
+
find(Time.now).map(&:text).should include("Yesterday's post")
|
285
|
+
end
|
276
286
|
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
287
|
+
it "should be able to use an alternative field" do
|
288
|
+
# Test may occur on an event day.
|
289
|
+
stub_time
|
290
|
+
Event.yesterday(nil, :field => "start_time").size.should eql(0)
|
291
|
+
end
|
282
292
|
|
283
|
-
end
|
284
|
-
|
285
|
-
describe "tomorrow" do
|
286
|
-
it "should show the post for tomorrow" do
|
287
|
-
find.map(&:text).should include("Tomorrow's post")
|
288
293
|
end
|
294
|
+
|
295
|
+
describe "tomorrow" do
|
296
|
+
it "should show the post for tomorrow" do
|
297
|
+
find.map(&:text).should include("Tomorrow's post")
|
298
|
+
end
|
289
299
|
|
290
|
-
|
291
|
-
|
292
|
-
|
300
|
+
it "should be able find tomorrow, given a Date" do
|
301
|
+
find(Time.now).map(&:text).should include("Tomorrow's post")
|
302
|
+
end
|
293
303
|
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
304
|
+
it "should be able to use an alternative field" do
|
305
|
+
# Test may occur on an event day.
|
306
|
+
stub_time
|
307
|
+
Event.tomorrow(nil, :field => "start_time").size.should eql(0)
|
308
|
+
end
|
298
309
|
end
|
299
|
-
end
|
300
310
|
|
301
|
-
|
311
|
+
describe "past" do
|
302
312
|
|
303
|
-
|
304
|
-
|
305
|
-
|
313
|
+
before do
|
314
|
+
stub_time
|
315
|
+
end
|
306
316
|
|
307
|
-
|
308
|
-
|
309
|
-
|
317
|
+
it "should show the correct number of posts in the past" do
|
318
|
+
size.should eql(1)
|
319
|
+
end
|
310
320
|
|
311
|
-
|
312
|
-
|
313
|
-
|
321
|
+
it "should find for a given time" do
|
322
|
+
size(Time.zone.now - 2.days).should eql(1)
|
323
|
+
end
|
314
324
|
|
315
|
-
|
316
|
-
|
317
|
-
|
325
|
+
it "should find for a given date" do
|
326
|
+
size(Date.today - 2).should eql(1)
|
327
|
+
end
|
318
328
|
|
319
|
-
|
320
|
-
|
321
|
-
|
329
|
+
it "should find for a given string" do
|
330
|
+
size("next tuesday").should eql(5)
|
331
|
+
end
|
322
332
|
|
323
|
-
|
324
|
-
|
325
|
-
|
333
|
+
it "should be able to find all events before Ryan's birthday using a non-standard field" do
|
334
|
+
Event.past("01-01-#{Time.zone.now.year+2}".to_time, :field => "start_time").size.should eql(9)
|
335
|
+
end
|
326
336
|
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
337
|
+
it "should be able to order the find" do
|
338
|
+
stub_time(2,1)
|
339
|
+
find(Date.today, :order => "created_at ASC").first.text.should eql("Last year")
|
340
|
+
find(Date.today, :order => "created_at DESC").first.text.should eql("post 1-0")
|
341
|
+
end
|
332
342
|
|
333
|
-
end
|
334
|
-
|
335
|
-
describe "future" do
|
336
|
-
before do
|
337
|
-
stub_time
|
338
343
|
end
|
344
|
+
|
345
|
+
describe "future" do
|
346
|
+
before do
|
347
|
+
stub_time
|
348
|
+
end
|
339
349
|
|
340
|
-
|
341
|
-
|
342
|
-
|
350
|
+
it "should show the correct number of posts in the future" do
|
351
|
+
size.should eql(85)
|
352
|
+
end
|
343
353
|
|
344
|
-
|
345
|
-
|
346
|
-
|
354
|
+
it "should find for a given date" do
|
355
|
+
size(Date.today - 2).should eql(88)
|
356
|
+
end
|
347
357
|
|
348
|
-
|
349
|
-
|
350
|
-
|
358
|
+
it "should find for a given string" do
|
359
|
+
size("next tuesday").should eql(84)
|
360
|
+
end
|
351
361
|
|
352
|
-
|
353
|
-
|
354
|
-
|
362
|
+
it "should be able to find all events before Dad's birthday using a non-standard field" do
|
363
|
+
# TODO: This will change in May. Figure out how to fix.
|
364
|
+
Event.past("05-07-#{Time.zone.now.year}".to_time, :field => "start_time").size.should eql(5)
|
365
|
+
end
|
355
366
|
end
|
356
|
-
end
|
357
367
|
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
368
|
+
describe "as of" do
|
369
|
+
it "should be able to find posts as of 2 weeks ago" do
|
370
|
+
stub_time
|
371
|
+
Post.as_of_2_weeks_ago.size.should eql(3)
|
372
|
+
end
|
363
373
|
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
374
|
+
it "should be able to find posts as of 2 weeks before a given time" do
|
375
|
+
stub_time
|
376
|
+
Post.as_of_2_weeks_ago(Time.zone.now + 1.month).size.should eql(12)
|
377
|
+
end
|
368
378
|
|
369
|
-
|
370
|
-
|
371
|
-
|
379
|
+
it "should error if given a date in the past far enough back" do
|
380
|
+
lambda { Post.as_of_6_weeks_ago(Time.zone.now - 2.months) }.should raise_error(ByStar::ParseError, "End time is before start time, searching like this will return no results.")
|
381
|
+
end
|
372
382
|
|
373
|
-
|
374
|
-
|
383
|
+
it "should not do anything if given an invalid date" do
|
384
|
+
lambda { Post.as_of_ryans_birthday }.should raise_error(ByStar::ParseError, "Chronic couldn't work out \"Ryans birthday\"; please be more precise.")
|
385
|
+
end
|
375
386
|
end
|
376
|
-
end
|
377
387
|
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
388
|
+
describe "between" do
|
389
|
+
it "should find posts between last tuesday and next tuesday" do
|
390
|
+
stub_time
|
391
|
+
size("last tuesday", "next tuesday").should eql(4)
|
392
|
+
end
|
383
393
|
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
394
|
+
it "should find between two times" do
|
395
|
+
stub_time
|
396
|
+
size(Time.zone.now - 5.days, Time.zone.now + 5.days).should eql(4)
|
397
|
+
end
|
388
398
|
|
389
|
-
|
390
|
-
|
391
|
-
|
399
|
+
it "should find between two dates" do
|
400
|
+
stub_time
|
401
|
+
size(Date.today, Date.today + 5).should eql(4)
|
402
|
+
end
|
392
403
|
end
|
393
|
-
end
|
394
404
|
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
405
|
+
describe "up to" do
|
406
|
+
it "should be able to find posts up to 2 weeks from now" do
|
407
|
+
stub_time
|
408
|
+
Post.up_to_6_weeks_from_now.size.should eql(12)
|
409
|
+
end
|
400
410
|
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
411
|
+
it "should be able to find posts up to 2 weeks from a given time" do
|
412
|
+
stub_time
|
413
|
+
Post.up_to_6_weeks_from_now(Time.zone.now - 1.month).size.should eql(12)
|
414
|
+
end
|
405
415
|
|
406
|
-
|
407
|
-
|
408
|
-
|
416
|
+
it "should error if given a date in the past" do
|
417
|
+
lambda { Post.up_to_6_weeks_from_now(Time.zone.now + 2.months) }.should raise_error(ByStar::ParseError, "End time is before start time, searching like this will return no results.")
|
418
|
+
end
|
409
419
|
|
410
|
-
|
411
|
-
|
412
|
-
|
420
|
+
it "should not do anything if given an invalid date" do
|
421
|
+
lambda { Post.up_to_ryans_birthday }.should raise_error(ByStar::ParseError, "Chronic couldn't work out \"Ryans birthday\"; please be more precise.")
|
422
|
+
end
|
413
423
|
|
414
|
-
end
|
415
|
-
|
416
|
-
# Because we override method_missing, we ensure that it still works as it should with this test.
|
417
|
-
describe "method_missing" do
|
418
|
-
it "should still work" do
|
419
|
-
Post.find_by_text("Today's post").should_not be_nil
|
420
424
|
end
|
425
|
+
|
426
|
+
# Because we override method_missing, we ensure that it still works as it should with this test.
|
427
|
+
describe "method_missing" do
|
428
|
+
it "should still work" do
|
429
|
+
Post.find_by_text("Today's post").should_not be_nil
|
430
|
+
end
|
421
431
|
|
422
|
-
|
423
|
-
|
432
|
+
it "should raise a proper NoMethodError" do
|
433
|
+
lambda { Post.idontexist }.should raise_error(NoMethodError, %r(^undefined method `idontexist'))
|
434
|
+
end
|
424
435
|
end
|
425
|
-
end
|
426
436
|
|
427
|
-
|
428
|
-
|
429
|
-
|
437
|
+
describe "named_scopes" do
|
438
|
+
it "should be compatible" do
|
439
|
+
Event.secret.by_year(nil, :field => "start_time").size.should eql(1)
|
440
|
+
end
|
430
441
|
end
|
431
|
-
end
|
432
442
|
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
|
443
|
+
describe "joins" do
|
444
|
+
it "should not have ambiguous column names" do
|
445
|
+
lambda { Post.by_month do
|
446
|
+
{ :joins => :tags }
|
447
|
+
end }.should_not raise_error
|
448
|
+
end
|
438
449
|
end
|
439
|
-
end
|
440
450
|
|
441
451
|
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
|
450
|
-
|
452
|
+
describe "nested find" do
|
453
|
+
|
454
|
+
it "should be able to find posts after right now" do
|
455
|
+
stub_time
|
456
|
+
Post.by_current_work_week.size.should eql(3)
|
457
|
+
Post.by_current_work_week do
|
458
|
+
{ :conditions => ["created_at > ?", Time.now] }
|
459
|
+
end.size.should eql(0)
|
460
|
+
end
|
451
461
|
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
|
456
|
-
|
462
|
+
it "should be able to find a single post from last year with the tag 'ruby'" do
|
463
|
+
Post.by_year(Time.zone.now.year - 1) do
|
464
|
+
{ :include => :tags, :conditions => ["tags.name = ?", 'ruby'] }
|
465
|
+
end.size.should eql(1)
|
466
|
+
end
|
457
467
|
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
|
462
|
-
|
468
|
+
it "should be able to find a single post from January last year with the tag 'ruby'" do
|
469
|
+
Post.by_month("January", :year => Time.zone.now.year - 1) do
|
470
|
+
{ :include => :tags, :conditions => ["tags.name = ?", 'ruby'] }
|
471
|
+
end.size.should eql(1)
|
472
|
+
end
|
463
473
|
|
464
|
-
|
465
|
-
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
|
474
|
+
it "should be able to find a single post from the current fortnight with the tag 'may2'" do
|
475
|
+
stub_time
|
476
|
+
Post.by_fortnight do
|
477
|
+
{ :include => :tags, :conditions => ["tags.name = ?", 'may2'] }
|
478
|
+
end.size.should eql(1)
|
479
|
+
end
|
470
480
|
|
471
|
-
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
|
476
|
-
|
481
|
+
it "should be able to find a single post from the current week with the tag 'may2'" do
|
482
|
+
stub_time
|
483
|
+
Post.by_week do
|
484
|
+
{ :include => :tags, :conditions => ["tags.name = ?", 'may2'] }
|
485
|
+
end.size.should eql(1)
|
486
|
+
end
|
477
487
|
|
478
|
-
|
479
|
-
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
|
488
|
+
it "should be able to find a single post from the current weekend with the tag 'weekend'" do
|
489
|
+
stub_time
|
490
|
+
Post.by_weekend do
|
491
|
+
{ :include => :tags, :conditions => ["tags.name = ?", 'weekend'] }
|
492
|
+
end.size.should eql(1)
|
493
|
+
end
|
484
494
|
|
485
|
-
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
|
495
|
+
it "should be able to find a single post from the current day with the tag 'today'" do
|
496
|
+
Post.by_day do
|
497
|
+
{ :include => :tags, :conditions => ["tags.name = ?", 'today'] }
|
498
|
+
end.size.should eql(1)
|
499
|
+
end
|
490
500
|
|
491
|
-
|
492
|
-
|
493
|
-
|
494
|
-
|
495
|
-
|
501
|
+
it "should be able to find a single post from yesterday with the tag 'yesterday'" do
|
502
|
+
Post.yesterday do
|
503
|
+
{ :include => :tags, :conditions => ["tags.name = ?", 'yesterday'] }
|
504
|
+
end.size.should eql(1)
|
505
|
+
end
|
496
506
|
|
497
507
|
|
498
|
-
|
499
|
-
|
500
|
-
|
501
|
-
|
502
|
-
|
508
|
+
it "should be able to find a single post from tomorrow with the tag 'tomorrow'" do
|
509
|
+
Post.tomorrow do
|
510
|
+
{ :include => :tags, :conditions => ["tags.name = ?", 'tomorrow'] }
|
511
|
+
end.size.should eql(1)
|
512
|
+
end
|
503
513
|
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
|
508
|
-
|
514
|
+
it "should be able to find a single post from the past with the tag 'yesterday'" do
|
515
|
+
Post.past do
|
516
|
+
{ :include => :tags, :conditions => ["tags.name = ?", 'yesterday'] }
|
517
|
+
end.size.should eql(1)
|
518
|
+
end
|
509
519
|
|
510
|
-
|
511
|
-
|
512
|
-
|
513
|
-
|
514
|
-
|
520
|
+
it "should be able to find a single post from the future with the tag 'tomorrow'" do
|
521
|
+
Post.future do
|
522
|
+
{ :include => :tags, :conditions => ["tags.name = ?", 'tomorrow'] }
|
523
|
+
end.size.should eql(1)
|
524
|
+
end
|
515
525
|
|
516
|
-
|
517
|
-
|
518
|
-
|
519
|
-
|
526
|
+
it "should work when block is empty" do
|
527
|
+
stub_time
|
528
|
+
Post.future { }.size.should eql(85)
|
529
|
+
end
|
520
530
|
|
521
|
-
|
522
|
-
|
523
|
-
|
531
|
+
it "should be able to find a single post from the future with the tag 'tomorrow' (redux)" do
|
532
|
+
Post.future(Time.zone.now, :include => :tags, :conditions => ["tags.name = ?", 'tomorrow']).size.should eql(1)
|
533
|
+
end
|
524
534
|
|
525
|
-
|
535
|
+
end
|
526
536
|
|
527
|
-
|
528
|
-
|
529
|
-
|
530
|
-
|
531
|
-
|
532
|
-
|
537
|
+
describe "Calculations" do
|
538
|
+
describe "Sum" do
|
539
|
+
describe "by year" do
|
540
|
+
it "current year" do
|
541
|
+
stub_time
|
542
|
+
Invoice.sum_by_year(:value).should eql(374000)
|
543
|
+
end
|
533
544
|
end
|
534
|
-
end
|
535
545
|
|
536
|
-
|
537
|
-
|
538
|
-
|
539
|
-
|
546
|
+
describe "by month" do
|
547
|
+
it "current month" do
|
548
|
+
stub_time
|
549
|
+
Invoice.sum_by_month(:value).should eql(11000)
|
550
|
+
end
|
540
551
|
end
|
541
552
|
end
|
542
|
-
end
|
543
553
|
|
544
|
-
|
545
|
-
|
546
|
-
|
547
|
-
|
548
|
-
|
554
|
+
describe "Count" do
|
555
|
+
describe "by year" do
|
556
|
+
it "current year" do
|
557
|
+
Invoice.count_by_year.should eql(79)
|
558
|
+
end
|
549
559
|
|
550
|
-
|
551
|
-
|
552
|
-
|
560
|
+
it "using a field" do
|
561
|
+
Invoice.count_by_year(:number).should eql(Invoice.by_year.size-1)
|
562
|
+
end
|
553
563
|
|
554
|
-
|
555
|
-
|
556
|
-
|
564
|
+
it "different year" do
|
565
|
+
Invoice.count_by_year(:all, 2008)
|
566
|
+
end
|
557
567
|
|
558
|
-
|
559
|
-
|
560
|
-
|
561
|
-
|
568
|
+
it "current year with the given tag" do
|
569
|
+
Post.count_by_year do
|
570
|
+
{ :include => :tags, :conditions => ["tags.name = ?", 'tomorrow'] }
|
571
|
+
end.should eql(1)
|
572
|
+
end
|
562
573
|
end
|
563
|
-
end
|
564
574
|
|
565
|
-
|
566
|
-
|
567
|
-
|
568
|
-
|
575
|
+
describe "by month" do
|
576
|
+
it "current month" do
|
577
|
+
Invoice.count_by_month
|
578
|
+
end
|
569
579
|
|
570
|
-
|
571
|
-
|
572
|
-
|
580
|
+
it "using a field" do
|
581
|
+
Invoice.count_by_month(:number).should eql(Invoice.by_month.size-1)
|
582
|
+
end
|
573
583
|
|
574
|
-
|
575
|
-
|
576
|
-
|
577
|
-
|
584
|
+
it "different month" do
|
585
|
+
stub_time
|
586
|
+
Invoice.count_by_month(:all, 9)
|
587
|
+
end
|
578
588
|
|
579
|
-
|
580
|
-
|
581
|
-
|
582
|
-
|
583
|
-
|
589
|
+
it "current month with the given tag" do
|
590
|
+
Post.count_by_month(:all, Time.zone.now) do
|
591
|
+
{ :include => :tags, :conditions => ["tags.name = ?", 'tomorrow'] }
|
592
|
+
end.should eql(1)
|
593
|
+
end
|
584
594
|
|
585
|
-
|
586
|
-
|
587
|
-
|
595
|
+
it "current month with blank block" do
|
596
|
+
stub_time
|
597
|
+
Post.count_by_month(:all, Time.zone.now) { }.should eql(10)
|
598
|
+
end
|
588
599
|
end
|
589
600
|
end
|
590
|
-
end
|
591
601
|
|
592
|
-
|
602
|
+
end
|
593
603
|
|
594
|
-
|
595
|
-
|
596
|
-
|
597
|
-
|
598
|
-
|
604
|
+
describe "edge cases" do
|
605
|
+
# This method previously generated sql like: `day_entries`.`spent_at`.`spent_at`.`spent_at`.`spent_at`
|
606
|
+
# Which is *obviously* incorrect and #omg worthy.
|
607
|
+
it "should not spam the field name when using a different field" do
|
608
|
+
Invoice.first.day_entries.between((Time.zone.now - 3.days).to_date, Time.zone.now.to_date, :field => "spent_at")
|
609
|
+
end
|
599
610
|
end
|
600
|
-
end
|
601
611
|
|
602
|
-
|
603
|
-
|
604
|
-
|
605
|
-
|
612
|
+
describe Time do
|
613
|
+
it "should work out the beginning of a weekend (Friday 3pm)" do
|
614
|
+
range_test do
|
615
|
+
Time.now.beginning_of_weekend.strftime("%A %I:%M%p").should eql("Friday 03:00PM")
|
616
|
+
end
|
606
617
|
end
|
607
|
-
end
|
608
618
|
|
609
|
-
|
610
|
-
|
611
|
-
|
619
|
+
it "should work out the end of a weekend (Monday 3am)" do
|
620
|
+
range_test do
|
621
|
+
Time.now.end_of_weekend.strftime("%A %I:%M%p").should eql("Monday 03:00AM")
|
622
|
+
end
|
612
623
|
end
|
613
624
|
end
|
614
625
|
end
|
615
626
|
end
|
616
|
-
|
617
627
|
end
|