Search code examples
ruby-on-railsherokuheroku-toolbelt

Changing Date and Time of Heroku(ruby on rails) Server


I am having ruby on rails application which is hosted on Heroku. For testing purpose I have to change Date and Time of the server. Or set it manually to particular date..? Is there any way I can do that..? Using console or anything.?


Solution

  • I achieved this with the Timecop gem using an around_action to change the time in my staging environment.

    module TimeTravelFilters
      extend ActiveSupport::Concern
    
      included do
        if Time::Clock.travel_ok?
          around_action :time_travel_for_request
        end
      end
    
      def time_travel_for_request
        time_travel
        yield
        time_travel_return
      end
    
      def time_travel
        if Time::Clock.fake_time
          Timecop.travel Time::Clock.fake_time
        else
          Timecop.return
        end
      end
    
      def time_travel_return
        Timecop.return
      end
    end
    

    Time::Clock is my own class that keeps track of the fake time.

    I have a separate TimeController that lets me change the time on the server.

    class TimeController < ApplicationController
      before_action :require_admin!
    
      def index
      end
    
      def update
        @clock.update_attributes params[:time_clock]
        redirect_to time_index_path
      end
    
      def destroy
        @clock.reset
        redirect_to time_index_path
      end
    end