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 CHANGED
@@ -1 +1 @@
1
- 0.6.0
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.0"
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-18}
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
- by_star(start_time, (start_time + 1.day).end_of_day, options, &block)
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
- validate_find_options(options)
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
- validate_find_options(options)
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
- describe Post do
5
-
6
- def stub_time(day=1, month=1, year=Time.zone.now.year, hour=0, minute=0)
7
- stub = "#{day}-#{month}-#{year} #{hour}:#{minute}".to_time
8
- Time.stub!(:now).and_return(stub)
9
- Time.zone.stub!(:now).and_return(stub)
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
- def range_test(&block)
13
- (1..31).to_a.each do |d|
14
- stub_time(d, 07, 2009, 05, 05)
15
- block.call
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
- def find(*args)
20
- method = description_args.first.sub(' ', '_')
21
- Post.send(method, *args)
22
- end
29
+ def find(*args)
30
+ method = description_args.first.sub(' ', '_')
31
+ Post.send(method, *args)
32
+ end
23
33
 
24
- def size(*args)
25
- method = description_args.first.sub(' ', '_')
26
- Post.send(method, *args).size
27
- end
34
+ def size(*args)
35
+ method = description_args.first.sub(' ', '_')
36
+ Post.send(method, *args).size
37
+ end
28
38
 
29
- ["mysql", "sqlite3"].each do |adapter|
30
- ActiveRecord::Base.establish_connection(YAML::load_file(File.dirname(__FILE__) + "/database.yml")[adapter])
39
+ ["mysql", "sqlite3"].each do |adapter|
40
+ ActiveRecord::Base.establish_connection(YAML::load_file(File.dirname(__FILE__) + "/database.yml")[adapter])
31
41
 
32
- describe "by year" do
33
- it "should be able to find all the posts in the current year" do
34
- size.should eql(Post.count - 1)
35
- end
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
- it "should be able to find if given a string" do
38
- size(Time.zone.now.year.to_s).should eql(Post.count - 1)
39
- end
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
- it "should be able to find a single post from last year" do
42
- size(Time.zone.now.year-1).should eql(1)
43
- end
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
- it "should error when given an invalid year" do
46
- # This is broken on 1.8.6 (and previous versions), any patchlevel after & before 111
47
- major, minor, trivial = RUBY_VERSION.split(".").map(&:to_i)
48
- if major == 1 && ((minor == 8 && trivial <= 6) || (minor <= 8)) && RUBY_PATCHLEVEL.to_i > 111
49
- lambda { find(1901) }.should raise_error(ByStar::ParseError, "Invalid arguments detected, year may possibly be outside of valid range (1902-2039)")
50
- lambda { find(2039) }.should raise_error(ByStar::ParseError, "Invalid arguments detected, year may possibly be outside of valid range (1902-2039)")
51
- else
52
- find(1456).should be_empty
53
- find(1901).should be_empty
54
- find(2039).should be_empty
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
- it "should be able to use an alternative field (string)" do
59
- Event.by_year(nil, :field => "start_time").size.should eql(8)
60
- end
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
- it "should be able to use an alternative field (symbol)" do
63
- Event.by_year(nil, :field => :start_time).size.should eql(8)
64
- end
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
- it "should be able to use an alternative field (symbol) with directional searching" do
67
- stub_time
68
- Event.past(nil, :field => :start_time).size.should eql(1)
69
- end
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
- it "should be able to order the result set" do
72
- find(Time.zone.now.year, :order => "created_at DESC").first.text.should eql("That's it!")
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
- describe "by month" do
86
+ describe "by month" do
77
87
 
78
- it "should be able to find posts for the current month" do
79
- stub_time
80
- size.should eql(10)
81
- end
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
- it "should be able to find a single post for January" do
84
- size("January").should eql(10)
85
- end
93
+ it "should be able to find a single post for January" do
94
+ size("January").should eql(10)
95
+ end
86
96
 
87
- it "should be able to find two posts for the 2nd month" do
88
- size(2).should eql(2)
89
- end
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
- it "should be able to find three posts for the 3rd month, using time instance" do
92
- # Hack... if we're running this test during march there's going to be more posts than usual.
93
- # This is due to the #today, #yesterday and #tomorrow methods.
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
- size(Time.local(Time.zone.now.year, 3, 1)).should eql(3)
96
- end
105
+ size(Time.local(Time.zone.now.year, 3, 1)).should eql(3)
106
+ end
97
107
 
98
- it "should be able to find a single post from January last year" do
99
- size("January", :year => Time.zone.now.year - 1).should eql(1)
100
- end
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
- it "should fail when given incorrect months" do
103
- lambda { find(0) }.should raise_error(ByStar::ParseError)
104
- lambda { find(13) }.should raise_error(ByStar::ParseError)
105
- lambda { find("Ryan") }.should raise_error(ByStar::ParseError)
106
- lambda { find([1,2,3]) }.should raise_error(ByStar::ParseError)
107
- end
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
- it "should be able to take decimals" do
110
- size(1.5).should eql(10)
111
- end
119
+ it "should be able to take decimals" do
120
+ size(1.5).should eql(10)
121
+ end
112
122
 
113
- it "should be able to use an alternative field" do
114
- if Time.zone.now.month == 12
115
- Event.by_month(nil, :field => "start_time").size.should eql(4)
116
- else
117
- stub_time(1, 12)
118
- Event.by_month(nil, :field => "start_time").size.should eql(1)
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
- it "should be able to specify the year as a string" do
123
- size(1, :year => (Time.zone.now.year - 1).to_s).should eql(1)
124
- end
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
- end
136
+ end
127
137
 
128
- describe "by fortnight" do
138
+ describe "by fortnight" do
129
139
 
130
- it "should be able to find posts in the current fortnight" do
131
- stub_time
132
- size.should eql(4)
133
- end
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
- it "should be able to find posts in the 1st fortnight" do
136
- size(0).should eql(4)
137
- end
145
+ it "should be able to find posts in the 1st fortnight" do
146
+ size(0).should eql(4)
147
+ end
138
148
 
139
- it "should be able to find posts for a fortnight ago" do
140
- stub_time
141
- size(2.weeks.ago).should eql(0)
142
- end
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
- it "should raise an error when given an invalid argument" do
145
- 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.")
146
- end
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
- it "should be able to use an alternative field" do
149
- stub_time
150
- Event.by_fortnight(nil, :field => "start_time").size.should eql(0)
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
- describe "by week" do
164
+ describe "by week" do
155
165
 
156
- it "should be able to find posts in the current week" do
157
- stub_time
158
- size.should eql(4)
159
- end
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
- it "should be able to find posts in the 1st week" do
162
- size(0).should eql(4)
163
- end
171
+ it "should be able to find posts in the 1st week" do
172
+ size(0).should eql(4)
173
+ end
164
174
 
165
- it "should be able to find posts in the 1st week of last year" do
166
- size(0, :year => Time.zone.now.year-1).should eql(1)
167
- end
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
- it "should not find any posts from a week ago" do
170
- stub_time
171
- size(1.week.ago).should eql(0)
172
- end
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
- it "should be able to find posts by a given date" do
175
- stub_time
176
- size(1.week.ago.to_date).should eql(0)
177
- end
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
- it "should find, not size the posts for the current week" do
180
- stub_time
181
- find.map(&:text).include?("The 'Current' Week")
182
- find.map(&:text).include?("Weekend of May")
183
- end
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
- it "should raise an error when given an invalid argument" do
186
- 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.")
187
- end
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
- it "should be able to use an alternative field" do
190
- stub_time
191
- Event.by_week(nil, :field => "start_time").size.should eql(0)
192
- end
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
- it "should find posts at the start of the year" do
195
- size(0).should eql(4)
196
- end
204
+ it "should find posts at the start of the year" do
205
+ size(0).should eql(4)
206
+ end
197
207
 
198
- it "should find posts at the end of the year" do
199
- size(Time.zone.now.end_of_year).should eql(1)
200
- end
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
- it "should be able to use an alternative field" do
211
- year = Time.zone.now.year
212
- stub_time(1, 8)
213
- Event.by_weekend(nil, :field => "start_time").size.should eql(0)
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
- describe "by current weekend" do
218
- it "should work" do
219
- range_test do
220
- Post.by_current_weekend
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
- describe "by current work week" do
226
- it "should work" do
227
- range_test do
228
- Post.by_current_work_week
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
- describe "by day" do
234
- it "should be able to find a post for today" do
235
- stub_time
236
- size.should eql(3)
237
- end
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
- it "should be able to find a post by a given date" do
240
- stub_time
241
- size(Date.today).should eql(3)
242
- end
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
- it "should be able to use an alternative field" do
245
- Event.by_day(Time.now - 1.day, :field => "start_time").size.should eql(1)
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
- describe "today" do
250
- it "should show the post for today" do
251
- find.map(&:text).should include("Today's post")
252
- end
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
- it "should be able to use an alternative field" do
255
- # Test may occur on an event day.
256
- stub_time
257
- Event.today(nil, :field => "start_time").size.should eql(0)
258
- end
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
- end
270
+ end
261
271
 
262
- describe "tomorrow" do
263
- it "should show the post for tomorrow" do
264
- find.map(&:text).should include("Tomorrow's post")
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
- describe "yesterday" do
269
- it "should show the post for yesterday" do
270
- find.map(&:text).should include("Yesterday's post")
271
- end
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
- it "should be able find yesterday, given a Date" do
274
- find(Time.now).map(&:text).should include("Yesterday's post")
275
- end
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
- it "should be able to use an alternative field" do
278
- # Test may occur on an event day.
279
- stub_time
280
- Event.yesterday(nil, :field => "start_time").size.should eql(0)
281
- end
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
- it "should be able find tomorrow, given a Date" do
291
- find(Time.now).map(&:text).should include("Tomorrow's post")
292
- end
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
- it "should be able to use an alternative field" do
295
- # Test may occur on an event day.
296
- stub_time
297
- Event.tomorrow(nil, :field => "start_time").size.should eql(0)
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
- describe "past" do
311
+ describe "past" do
302
312
 
303
- before do
304
- stub_time
305
- end
313
+ before do
314
+ stub_time
315
+ end
306
316
 
307
- it "should show the correct number of posts in the past" do
308
- size.should eql(1)
309
- end
317
+ it "should show the correct number of posts in the past" do
318
+ size.should eql(1)
319
+ end
310
320
 
311
- it "should find for a given time" do
312
- size(Time.zone.now - 2.days).should eql(1)
313
- end
321
+ it "should find for a given time" do
322
+ size(Time.zone.now - 2.days).should eql(1)
323
+ end
314
324
 
315
- it "should find for a given date" do
316
- size(Date.today - 2).should eql(1)
317
- end
325
+ it "should find for a given date" do
326
+ size(Date.today - 2).should eql(1)
327
+ end
318
328
 
319
- it "should find for a given string" do
320
- size("next tuesday").should eql(5)
321
- end
329
+ it "should find for a given string" do
330
+ size("next tuesday").should eql(5)
331
+ end
322
332
 
323
- it "should be able to find all events before Ryan's birthday using a non-standard field" do
324
- Event.past("01-01-#{Time.zone.now.year+2}".to_time, :field => "start_time").size.should eql(9)
325
- end
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
- it "should be able to order the find" do
328
- stub_time(2,1)
329
- find(Date.today, :order => "created_at ASC").first.text.should eql("Last year")
330
- find(Date.today, :order => "created_at DESC").first.text.should eql("post 0")
331
- end
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
- it "should show the correct number of posts in the future" do
341
- size.should eql(85)
342
- end
350
+ it "should show the correct number of posts in the future" do
351
+ size.should eql(85)
352
+ end
343
353
 
344
- it "should find for a given date" do
345
- size(Date.today - 2).should eql(88)
346
- end
354
+ it "should find for a given date" do
355
+ size(Date.today - 2).should eql(88)
356
+ end
347
357
 
348
- it "should find for a given string" do
349
- size("next tuesday").should eql(84)
350
- end
358
+ it "should find for a given string" do
359
+ size("next tuesday").should eql(84)
360
+ end
351
361
 
352
- it "should be able to find all events before Dad's birthday using a non-standard field" do
353
- # TODO: This will change in May. Figure out how to fix.
354
- Event.past("05-07-#{Time.zone.now.year}".to_time, :field => "start_time").size.should eql(5)
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
- describe "as of" do
359
- it "should be able to find posts as of 2 weeks ago" do
360
- stub_time
361
- Post.as_of_2_weeks_ago.size.should eql(3)
362
- end
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
- it "should be able to find posts as of 2 weeks before a given time" do
365
- stub_time
366
- Post.as_of_2_weeks_ago(Time.zone.now + 1.month).size.should eql(12)
367
- end
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
- it "should error if given a date in the past far enough back" do
370
- 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.")
371
- end
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
- it "should not do anything if given an invalid date" do
374
- lambda { Post.as_of_ryans_birthday }.should raise_error(ByStar::ParseError, "Chronic couldn't work out \"Ryans birthday\"; please be more precise.")
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
- describe "between" do
379
- it "should find posts between last tuesday and next tuesday" do
380
- stub_time
381
- size("last tuesday", "next tuesday").should eql(4)
382
- end
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
- it "should find between two times" do
385
- stub_time
386
- size(Time.zone.now - 5.days, Time.zone.now + 5.days).should eql(4)
387
- end
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
- it "should find between two dates" do
390
- stub_time
391
- size(Date.today, Date.today + 5).should eql(4)
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
- describe "up to" do
396
- it "should be able to find posts up to 2 weeks from now" do
397
- stub_time
398
- Post.up_to_6_weeks_from_now.size.should eql(12)
399
- end
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
- it "should be able to find posts up to 2 weeks from a given time" do
402
- stub_time
403
- Post.up_to_6_weeks_from_now(Time.zone.now - 1.month).size.should eql(12)
404
- end
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
- it "should error if given a date in the past" do
407
- 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.")
408
- end
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
- it "should not do anything if given an invalid date" do
411
- lambda { Post.up_to_ryans_birthday }.should raise_error(ByStar::ParseError, "Chronic couldn't work out \"Ryans birthday\"; please be more precise.")
412
- end
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
- it "should raise a proper NoMethodError" do
423
- lambda { Post.idontexist }.should raise_error(NoMethodError, %r(^undefined method `idontexist'))
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
- describe "named_scopes" do
428
- it "should be compatible" do
429
- Event.private.by_year(nil, :field => "start_time").size.should eql(1)
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
- describe "joins" do
434
- it "should not have ambiguous column names" do
435
- lambda { Post.by_month do
436
- { :joins => :tags }
437
- end }.should_not raise_error
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
- describe "nested find" do
443
-
444
- it "should be able to find posts after right now" do
445
- stub_time
446
- Post.by_current_work_week.size.should eql(3)
447
- Post.by_current_work_week do
448
- { :conditions => ["created_at > ?", Time.now] }
449
- end.size.should eql(0)
450
- end
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
- it "should be able to find a single post from last year with the tag 'ruby'" do
453
- Post.by_year(Time.zone.now.year - 1) do
454
- { :include => :tags, :conditions => ["tags.name = ?", 'ruby'] }
455
- end.size.should eql(1)
456
- end
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
- it "should be able to find a single post from January last year with the tag 'ruby'" do
459
- Post.by_month("January", :year => Time.zone.now.year - 1) do
460
- { :include => :tags, :conditions => ["tags.name = ?", 'ruby'] }
461
- end.size.should eql(1)
462
- end
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
- it "should be able to find a single post from the current fortnight with the tag 'may2'" do
465
- stub_time
466
- Post.by_fortnight do
467
- { :include => :tags, :conditions => ["tags.name = ?", 'may2'] }
468
- end.size.should eql(1)
469
- end
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
- it "should be able to find a single post from the current week with the tag 'may2'" do
472
- stub_time
473
- Post.by_week do
474
- { :include => :tags, :conditions => ["tags.name = ?", 'may2'] }
475
- end.size.should eql(1)
476
- end
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
- it "should be able to find a single post from the current weekend with the tag 'weekend'" do
479
- stub_time
480
- Post.by_weekend do
481
- { :include => :tags, :conditions => ["tags.name = ?", 'weekend'] }
482
- end.size.should eql(1)
483
- end
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
- it "should be able to find a single post from the current day with the tag 'today'" do
486
- Post.by_day do
487
- { :include => :tags, :conditions => ["tags.name = ?", 'today'] }
488
- end.size.should eql(1)
489
- end
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
- it "should be able to find a single post from yesterday with the tag 'yesterday'" do
492
- Post.yesterday do
493
- { :include => :tags, :conditions => ["tags.name = ?", 'yesterday'] }
494
- end.size.should eql(1)
495
- end
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
- it "should be able to find a single post from tomorrow with the tag 'tomorrow'" do
499
- Post.tomorrow do
500
- { :include => :tags, :conditions => ["tags.name = ?", 'tomorrow'] }
501
- end.size.should eql(1)
502
- end
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
- it "should be able to find a single post from the past with the tag 'yesterday'" do
505
- Post.past do
506
- { :include => :tags, :conditions => ["tags.name = ?", 'yesterday'] }
507
- end.size.should eql(1)
508
- end
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
- it "should be able to find a single post from the future with the tag 'tomorrow'" do
511
- Post.future do
512
- { :include => :tags, :conditions => ["tags.name = ?", 'tomorrow'] }
513
- end.size.should eql(1)
514
- end
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
- it "should work when block is empty" do
517
- stub_time
518
- Post.future { }.size.should eql(85)
519
- end
526
+ it "should work when block is empty" do
527
+ stub_time
528
+ Post.future { }.size.should eql(85)
529
+ end
520
530
 
521
- it "should be able to find a single post from the future with the tag 'tomorrow' (redux)" do
522
- Post.future(Time.zone.now, :include => :tags, :conditions => ["tags.name = ?", 'tomorrow']).size.should eql(1)
523
- end
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
- end
535
+ end
526
536
 
527
- describe "Calculations" do
528
- describe "Sum" do
529
- describe "by year" do
530
- it "current year" do
531
- stub_time
532
- Invoice.sum_by_year(:value).should eql(374000)
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
- describe "by month" do
537
- it "current month" do
538
- stub_time
539
- Invoice.sum_by_month(:value).should eql(11000)
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
- describe "Count" do
545
- describe "by year" do
546
- it "current year" do
547
- Invoice.count_by_year.should eql(79)
548
- end
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
- it "using a field" do
551
- Invoice.count_by_year(:number).should eql(Invoice.by_year.size-1)
552
- end
560
+ it "using a field" do
561
+ Invoice.count_by_year(:number).should eql(Invoice.by_year.size-1)
562
+ end
553
563
 
554
- it "different year" do
555
- Invoice.count_by_year(:all, 2008)
556
- end
564
+ it "different year" do
565
+ Invoice.count_by_year(:all, 2008)
566
+ end
557
567
 
558
- it "current year with the given tag" do
559
- Post.count_by_year do
560
- { :include => :tags, :conditions => ["tags.name = ?", 'tomorrow'] }
561
- end.should eql(1)
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
- describe "by month" do
566
- it "current month" do
567
- Invoice.count_by_month
568
- end
575
+ describe "by month" do
576
+ it "current month" do
577
+ Invoice.count_by_month
578
+ end
569
579
 
570
- it "using a field" do
571
- Invoice.count_by_month(:number).should eql(Invoice.by_month.size-1)
572
- end
580
+ it "using a field" do
581
+ Invoice.count_by_month(:number).should eql(Invoice.by_month.size-1)
582
+ end
573
583
 
574
- it "different month" do
575
- stub_time
576
- Invoice.count_by_month(:all, 9)
577
- end
584
+ it "different month" do
585
+ stub_time
586
+ Invoice.count_by_month(:all, 9)
587
+ end
578
588
 
579
- it "current month with the given tag" do
580
- Post.count_by_month(:all, Time.zone.now) do
581
- { :include => :tags, :conditions => ["tags.name = ?", 'tomorrow'] }
582
- end.should eql(1)
583
- end
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
- it "current month with blank block" do
586
- stub_time
587
- Post.count_by_month(:all, Time.zone.now) { }.should eql(10)
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
- end
602
+ end
593
603
 
594
- describe "edge cases" do
595
- # This method previously generated sql like: `day_entries`.`spent_at`.`spent_at`.`spent_at`.`spent_at`
596
- # Which is *obviously* incorrect and #omg worthy.
597
- it "should not spam the field name when using a different field" do
598
- Invoice.first.day_entries.between((Time.zone.now - 3.days).to_date, Time.zone.now.to_date, :field => "spent_at")
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
- describe Time do
603
- it "should work out the beginning of a weekend (Friday 3pm)" do
604
- range_test do
605
- Time.now.beginning_of_weekend.strftime("%A %I:%M%p").should eql("Friday 03:00PM")
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
- it "should work out the end of a weekend (Monday 3am)" do
610
- range_test do
611
- Time.now.end_of_weekend.strftime("%A %I:%M%p").should eql("Monday 03:00AM")
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