Module: TimeHelpers

Includes:
ActiveSupport::Testing::TimeHelpers
Defined in:
lib/spreewald/time_steps.rb

Instance Method Summary collapse

Instance Method Details

#current_timeObject



36
37
38
39
40
41
42
# File 'lib/spreewald/time_steps.rb', line 36

def current_time
  if use_timezones?
    Time.current
  else
    Time.now
  end
end

#default_timezoneObject



44
45
46
47
48
49
50
# File 'lib/spreewald/time_steps.rb', line 44

def default_timezone
  if ActiveRecord.respond_to?(:default_timezone)
    ActiveRecord.default_timezone
  else
    ActiveRecord::Base.default_timezone
  end
end

#parse_time(str) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/spreewald/time_steps.rb', line 28

def parse_time(str)
  if use_timezones?
    Time.zone.parse(str)
  else
    Time.parse(str)
  end
end

#travel(duration) ⇒ Object

Emulate ActiveSupport time helper methods with Timecop - don’t rename these methods



54
55
56
# File 'lib/spreewald/time_steps.rb', line 54

def travel(duration)
  Timecop.travel(current_time + duration)
end

#travel_backObject



62
63
64
# File 'lib/spreewald/time_steps.rb', line 62

def travel_back
  Timecop.return
end

#travel_to(date_or_time) ⇒ Object



58
59
60
# File 'lib/spreewald/time_steps.rb', line 58

def travel_to(date_or_time)
  Timecop.travel(date_or_time)
end

#use_timezones?Boolean

When you have to make your rails app time zone aware you have to go 100% otherwise you are better off ignoring time zones at all. makandracards.com/makandra/8723-guide-to-localizing-a-rails-application

Returns:

  • (Boolean)


23
24
25
26
# File 'lib/spreewald/time_steps.rb', line 23

def use_timezones?
  active_record_loaded = defined?(ActiveRecord::Base)
  (!active_record_loaded || default_timezone != :local) && Time.zone
end