Skip to content

Commit 2d23a63

Browse files
committed
Wrap Time.now in a stub class.
1 parent 729f2b3 commit 2d23a63

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

app/models/content.rb

+5-5
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ def parent_id_cannot_be_this_content
2929
default_scope { where(:type => Content.subclasses.collect { |s| s.name }) unless Rails.env.development? }
3030

3131
#Easily query for active, expired, or future content
32-
scope :expired, where("end_time < :now", {:now => Time.now})
33-
scope :future, where("start_time > :now", {:now => Time.now})
34-
scope :active, where("(start_time IS NULL OR start_time < :now) AND (end_time IS NULL OR end_time > :now)", {:now => Time.now})
32+
scope :expired, where("end_time < :now", {:now => Clock.time})
33+
scope :future, where("start_time > :now", {:now => Clock.time})
34+
scope :active, where("(start_time IS NULL OR start_time < :now) AND (end_time IS NULL OR end_time > :now)", {:now => Clock.time})
3535

3636
#Scoped relations for feed approval states
3737
has_many :approved_feeds, :through => :submissions, :source => :feed, :conditions => {"submissions.moderation_flag" => true}
@@ -46,12 +46,12 @@ def parent_id_cannot_be_this_content
4646
# 1. Start date is before now, or nil.
4747
# 2. End date is after now, or nil.
4848
def is_active?
49-
(start_time.nil? || start_time < Time.now) && (end_time.nil? || end_time > Time.now)
49+
(start_time.nil? || start_time < Clock.time) && (end_time.nil? || end_time > Clock.time)
5050
end
5151

5252
# Determine if content is expired based on its end time.
5353
def is_expired?
54-
(end_time < Time.now)
54+
(end_time < Clock.time)
5555
end
5656

5757
# Setter for the start time. If a hash is passed, convert that into a DateTime object and then a string.
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
<div class="clearfix datetime">
22
<%= form.label :start_time %>
33
<div class="input">
4-
<%= form.text_field("start_time[date]", :id => "start_time_date", :maxlength => 20, :class => "datefield", :value=> (Time.now + ConcertoConfig[:start_date_offset].to_i.days).strftime("%m/%d/%Y"))%><a id="date_start" class="button date">&nbsp;</a>
4+
<%= form.text_field("start_time[date]", :id => "start_time_date", :maxlength => 20, :class => "datefield", :value=> (Clock.time + ConcertoConfig[:start_date_offset].to_i.days).strftime("%m/%d/%Y"))%><a id="date_start" class="button date">&nbsp;</a>
55
<%= form.text_field("start_time[time]", :id => "start_time_time", :maxlength => 20, :class => "timefield", :value=>ConcertoConfig[:content_default_start_time])%><a id="time_start" class="button time">&nbsp;</a>
66
</div>
77
</div>
88

99
<div class="clearfix datetime">
1010
<%= form.label :end_time %>
1111
<div class="input">
12-
<%= form.text_field("end_time[date]", :id => "end_time_date", :maxlength => 20, :class => "datefield", :value=>(Time.now + ConcertoConfig[:default_content_run_time].to_i.days + ConcertoConfig[:start_date_offset].to_i.days).strftime("%m/%d/%Y"))%><a id="date_end" class="button date">&nbsp;</a>
12+
<%= form.text_field("end_time[date]", :id => "end_time_date", :maxlength => 20, :class => "datefield", :value=>(Clock.time + ConcertoConfig[:default_content_run_time].to_i.days + ConcertoConfig[:start_date_offset].to_i.days).strftime("%m/%d/%Y"))%><a id="date_end" class="button date">&nbsp;</a>
1313
<%= form.text_field("end_time[time]", :id => "end_time_time", :maxlength => 20, :class => "timefield", :value=>ConcertoConfig[:content_default_end_time])%><a id="time_end" class="button time">&nbsp;</a>
1414
</div>
1515
</div>

lib/clock.rb

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Stub out a clock
2+
class Clock
3+
# Wrapper around the current time.
4+
# Makes mocking and debugging easier because you can freeze time.
5+
def self.time
6+
Time.now
7+
end
8+
end

0 commit comments

Comments
 (0)