Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the new ActiveSupport::Testing::TimeHelpers inside controller

Edit to make it more clear

Like the title tells how to use the awesome ActiveSupport::Testing::TimeHelpers#travel_to method inside my controller. Like in tests, I want to achieve something like this:

SomethingConroller < ApplicationController
  def index
    travel_to some_date do
      # some stuff that it depends on the current_date 
    end
  end
end

Tried include the module:

include ActiveSupport::Testing::TimeHelpers

but I got:

uninitialized constant ActiveSupport::Testing

Hoping that the date traveled to will be applied to the view, view_helpers, controller_action

like image 474
mtkcs Avatar asked Jun 04 '15 13:06

mtkcs


1 Answers

If you must, then add require 'active_support/testing/time_helpers' at the top of the file.

Although I've no idea what you're doing with that code. Try this instead:

SomethingConroller < ApplicationController
  def index
    @instance = SomeModel.find_by_date(12.days.ago)
  end
end
like image 167
Matt Gibson Avatar answered Nov 08 '22 06:11

Matt Gibson