Skip to content

Commit

Permalink
Merge branch 'features/layout_console_helper'
Browse files Browse the repository at this point in the history
  • Loading branch information
ferndopolis committed Mar 25, 2014
2 parents d361409 + f3db1a6 commit 30491e6
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 50 deletions.
41 changes: 13 additions & 28 deletions README.rdoc
Original file line number Diff line number Diff line change
@@ -1,59 +1,44 @@
= timecop-console

* http://github.com/jtrupiano/timecop-console

== Description

timecop-console exposes controllers/routes for manipulating Time.now (using the Timecop gem) from your app. This is especially useful
during development and QA, as you can very easily simulate the movement of time. Just as timecop gives you this ability within your tests,
you can now easily make this available to your dev and QA team through a debug console.

I have plans to build out rails-caddy, a debug console that will pull in timecop-console (and a few others that I'm thinking about) to
truly give you a powerful QA tool.

== How to Use

Add to your `Gemfile`:

group :development, :staging do
gem 'timecop-console', '~> 0.2', :require => 'timecop_console'
gem 'timecop-console', :require => 'timecop_console'
end

Protip: avoid having `timecop-console` gem as part of `test` group if you use Timecop in your tests to avoid any hard-to-debug exceptions.
Avoid having `timecop-console` gem as part of `test` group if you use Timecop in your tests to avoid any hard-to-debug exceptions.

By requiring this dependency, it will open up ActionController::Base and inject an around_filter that will manage Time.now and friends for you.


You'll want to hook in the mountable engine for handling time changes in the specific environments that you want this to load in (probably only development, staging). Modify your `config/routes.rb`, adding:

if Rails.env.development? || Rails.env.staging?
mount TimecopConsole::Engine => '/timecop_console'
end


Then, to take advantage of this, you'll want to add a snippet of code to the bottom of your application's layout file, e.g.:

<% if Rails.env.development? || Rails.env.staging? %>
<%= timecop_console_layout %>
<% end %>

<div id="debug-console">
<p>
The time is <%= Time.now.to_s(:db) %> |
</p>
<%= form_tag timecop_console.update_path do %>
<p>
<%= datetime_select("timecop", "current_time") %>
</p>

<p>
<%= submit_tag "Time Travel", :class => 'btn' %>
</p>
<% end %>
<%= link_to "Reset", timecop_console.reset_path %>
</div>
This snippet exposes fields to allow you to alter each component of time (year, month, day, hour, minute, second). It's raw, and there is no validation whatsoever. A default (and customizable) snippet like this will be added to this library shortly. In the meantime, you can hand-write it.

<% end %>
== Helpers

Use <tt>time_travel_to</tt> to create a button which will jump directly to a specific date:

<%= time_travel_to App.launch_date %>

Where <tt>App.launch_date</tt> is a Date or DateTime object. By default the button will format to that date, but can be overwritten by passing a string:

This snippet exposes textfields to allow you to alter each component of time (year, month, day, hour, minute, second). It's raw, and there is no validation whatsoever. A default (and customizable) snippet like this will be added to this library shortly. In the meantime, you can hand-write it.
<%= time_travel_to App.launch_date, "LAUNCH DATE" %>

== Copyright

Expand Down
18 changes: 12 additions & 6 deletions app/helpers/timecop_console/main_helper.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
module TimecopConsole
module MainHelper
def time_travel_to(date)
def time_travel_to(date, name = nil)
unless date.respond_to?(:year) && date.respond_to?(:month) && date.respond_to?(:day)
raise ArgumentError, "Argument must be a Date object"
end

name ||= date.strftime("%B %d, %Y")
hour = date.respond_to?(:hour) ? date.hour : 12
min = date.respond_to?(:min) ? date.min : 0

update_path = timecop_console.update_path(timecop: {
'current_time(1i)' => date.year,
'current_time(2i)' => date.month,
'current_time(3i)' => date.day,
'current_time(4i)' => 12,
'current_time(5i)' => 0
'current_time(4i)' => hour,
'current_time(5i)' => min
})

button_to(date.strftime("%B %d, %Y"),
update_path,
method: :post)
button_to(name, update_path)
end

def timecop_console_layout
render partial: 'timecop_console/console_layout', layout: false
end
end
end
14 changes: 14 additions & 0 deletions app/views/timecop_console/_console_layout.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<div id="timecop-console">
<p>
<%= Time.now.strftime("Current time is: %D %r") %>
</p>
<%= form_tag timecop_console.update_path do %>
<p>
<%= datetime_select("timecop", "current_time") %>
</p>
<p>
<%= submit_tag "Time Travel", :class => 'btn' %>
</p>
<% end %>
<%= link_to "Reset", timecop_console.reset_path %>
</div>
1 change: 0 additions & 1 deletion spec/controllers/main_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
it 'redirects back' do
post :update, :timecop => timecop_param, :use_route => :timecop_console


response.should redirect_to("where_i_came_from")
end

Expand Down
7 changes: 1 addition & 6 deletions spec/dummy_app/app/views/sample/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
<h1>TimeCopConsole Demo</h1>
<p>Hello world!</p>
<p>
The time is now
<%= Time.now %>
</p>

<%= time_travel_to(Date.parse("December 31, 1999")) %>
<%= link_to "Reset Time", timecop_console.reset_path %>
<%= timecop_console_layout %>
9 changes: 1 addition & 8 deletions spec/dummy_app/config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,9 @@
require "action_controller/railtie"
#require "action_mailer/railtie"
#require "active_resource/railtie"
# require "sprockets/railtie"
# require "rails/test_unit/railtie"

if defined?(Bundler)
# If you precompile assets before deploying to production, use this line
Bundler.require(*Rails.groups(:assets => %w(development test)))
# If you want your assets lazily compiled in production, use this line
# Bundler.require(:default, :assets, Rails.env)
end

Bundler.require(*Rails.groups)
require 'timecop_console'

module DummyApp
Expand Down
4 changes: 4 additions & 0 deletions spec/dummy_app/config/environments/development.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require "sprockets/railtie"

DummyApp::Application.configure do
# Settings specified here will take precedence over those in config/application.rb

Expand All @@ -21,4 +23,6 @@

config.eager_load = false

config.assets.debug = true

end
25 changes: 24 additions & 1 deletion spec/helpers/main_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,33 @@
context "when a Date object is passed" do
let(:date) { Date.parse('Dec 12, 1950') }

it 'should return a link with timecop console route' do
it 'should return a button with timecop console route' do
helper.time_travel_to(date).should == "<form action=\"/timecop_console/update?timecop%5Bcurrent_time%281i%29%5D=1950&amp;timecop%5Bcurrent_time%282i%29%5D=12&amp;timecop%5Bcurrent_time%283i%29%5D=12&amp;timecop%5Bcurrent_time%284i%29%5D=12&amp;timecop%5Bcurrent_time%285i%29%5D=0\" class=\"button_to\" method=\"post\"><div><input type=\"submit\" value=\"December 12, 1950\" /></div></form>"
end

it 'should set the hour to default of 12' do
helper.time_travel_to(date).should include("current_time%284i%29%5D=12")
end

context "when a custom name is passed" do
let(:name) { "important_event" }
it "should pass the name as the button name" do
helper.time_travel_to(date, name).should include("value=\"important_event\"")
end
end
end

context "when a DateTime object is passed" do
let(:date) { DateTime.parse('2001-02-03T04:05:06+07:00') }

it "should set the hour accordingly" do
helper.time_travel_to(date).should include("current_time%284i%29%5D=4")
end
it "should set the minute accordingly" do
helper.time_travel_to(date).should include("current_time%285i%29%5D=5")
end
end

context "when a string is passed" do
let(:date) { "12-12-1950" }
it "should raise an ArgumentError" do
Expand Down

0 comments on commit 30491e6

Please sign in to comment.