Fork Of

Revisions

gist: 139854 Download_button fork
public
Public Clone URL: git://gist.github.com/139854.git
Embed All Files: show embed
by_star.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
   # Examples:
    # Post.by_day
    # Post.by_day(Time.yesterday)
    # Post.by_day("next tuesday")
    def by_day(time = Time.zone.now, options = {}, &block)
      time = parse(time)
      by_star(time.utc.beginning_of_day, time.utc.end_of_day, options, &block)
    end
    alias_method :today, :by_day
    
    # Examples:
    # Post.yesterday
    # # 2 days ago:
    # Post.yesterday(Time.yesterday)
    # # day before next tuesday
    # Post.yesterday("next tuesday")
    def yesterday(time = Time.zone.now, options = {}, &block)
      time = parse(time)
      by_day(time.advance(:days => -1), options, &block)
    end
    
    # Examples:
    # Post.tomorrow
    # # 2 days from now:
    # Post.tomorrow(Time.tomorrow)
    # # day after next tuesday
    # Post.tomorrow("next tuesday")
    def tomorrow(time = Time.zone.now, options = {}, &block)
      time = parse(time)
      by_day(time.advance(:days => 1), options, &block)
    end
by_star_spec.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  def find(*args)
    method = description_args.first.sub(' ', '_')
    Post.send(method, *args)
  end
 
  describe "today" do
    it "should show the post for tomorrow" do
      find("tomorrow").map(&:text).should include("Tomorrow's post")
    end
  end
 
  describe "yesterday" do
    it "should be able find yesterday, given a Date" do
      find(Date.today).map(&:text).should include("Yesterday's post")
    end
  end
 
  describe "tomorrow" do
    it "should be able find tomorrow, given a Date" do
      find(Date.today).map(&:text).should include("Tomorrow's post")
    end
  end
Text only #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Failures for by_star only occur (seemingly) in a positive timezone where the local time is in a day in the future from UTC.
So in +1000 (Brisbane) these three specs only fail BEFORE 10am. After 10am it's a sea of wonderful green.
 
..................................F..F..F...................................
 
1)
'Post today should show the post for tomorrow' FAILED
expected ["Today's post", "Today"] to include "Tomorrow's post"
./spec/by_star_spec.rb:197:
 
2)
'Post yesterday should be able find yesterday, given a Date' FAILED
expected ["Today's post", "Today"] to include "Yesterday's post"
./spec/by_star_spec.rb:213:
 
3)
'Post tomorrow should be able find tomorrow, given a Date' FAILED
expected [] to include "Tomorrow's post"
./spec/by_star_spec.rb:230:
 
Finished in 0.263454 seconds
 
76 examples, 3 failures