timecop-console 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 John Trupiano
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,53 @@
1
+ = timecop-console
2
+
3
+ * http://github.com/jtrupiano/timecop-console
4
+
5
+ == Description
6
+
7
+ timecop-console exposes controllers/routes for manipulating Time.now (using the Timecop gem) from your app. This is especially useful
8
+ during development and QA, as you can very easily simulate the movement of time. Just as timecop gives you this ability within your tests,
9
+ you can now easily make this available to your dev and QA team through a debug console.
10
+
11
+ 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
12
+ truly give you a powerful QA tool.
13
+
14
+ == Install
15
+ sudo gem install timecop-console (latest stable release)
16
+ sudo gem install jtrupiano-timecop-console (head of repository)
17
+
18
+ == Requirements
19
+ * Timecop ~> 0.2.1
20
+
21
+ == How to Use
22
+
23
+ You'll want to hook in the functionality in the specific environments that you want this to load in (probably only development, staging, and qa). In the specific environment file (e.g. config/environments/staging.rb), include the following snippet:
24
+
25
+ config.after_initialize do
26
+ require 'timecop_console'
27
+ end
28
+
29
+ By requiring this file, you will open up ActionController::Base and inject an around_filter that will manage Time.now and friends for you. Additionally, it will add lib/timecop-console/controllers/timecop_controller.rb to the load path along with its routes. The two routes are:
30
+
31
+ map.update_time '/timecop/update', :controller => 'timecop', :action => 'update'
32
+ map.reset_time '/timecop/reset', :controller => 'timecop', :action => 'reset'
33
+
34
+ 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.:
35
+
36
+ <% if ["development", "staging", "qa"].include?(RAILS_ENV) -%>
37
+ <div id="debug_console">
38
+ The time is <%= Time.now.to_s(:db) %> |
39
+ <% form_remote_tag :url => update_time_path do -%>
40
+ <%= submit_tag "Time Travel" %>
41
+ <% %w(year month day hour min sec).each do |field| -%>
42
+ <%= text_field_tag field, Time.now.send(field), :size => 4 %>
43
+ <% end -%>
44
+ <% end -%>
45
+ <%= link_to_remote "Reset", :url => reset_time_path %>
46
+ </div>
47
+ <% end -%>
48
+
49
+ 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.
50
+
51
+ == Copyright
52
+
53
+ Copyright (c) 2009 John Trupiano. See LICENSE for details.
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :patch: 0
3
+ :major: 0
4
+ :minor: 1
@@ -0,0 +1,21 @@
1
+
2
+ # Because we want to use the session, we need to inherit from ApplicationController
3
+ # This is currently very brittle....need to think this one through further.
4
+ require_dependency 'application' if Object.const_defined?(:RAILS_ENV)
5
+ class TimecopController < ApplicationController
6
+
7
+ # verify :method => 'post'
8
+ skip_filter :handle_timecop_offset
9
+
10
+ def update
11
+ year, month, day, hour, min, sec = params[:year], params[:month], params[:day], params[:hour], params[:min], params[:sec]
12
+ session[:timecop_adjusted_time] = Time.local(year, month, day, hour, min, sec)
13
+ render :status => 200, :nothing => true
14
+ end
15
+
16
+ def reset
17
+ session[:timecop_adjusted_time] = nil
18
+ render :status => 200, :nothing => true
19
+ end
20
+
21
+ end
@@ -0,0 +1,16 @@
1
+ module ActionController
2
+ module Routing #:nodoc:
3
+ class RouteSet #:nodoc:
4
+ alias_method :draw_without_timecop_routes, :draw
5
+ def draw
6
+ draw_without_timecop_routes do |map|
7
+
8
+ map.update_time '/timecop/update', :controller => 'timecop', :action => 'update'
9
+ map.reset_time '/timecop/reset', :controller => 'timecop', :action => 'reset'
10
+
11
+ yield map if block_given?
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,33 @@
1
+ # Defines extensions applied to ActionController::Base to support our time travel
2
+ module TimecopControllerMethods
3
+
4
+ def self.included(base)
5
+ base.class_eval do
6
+ around_filter :handle_timecop_offset
7
+ end
8
+ end
9
+
10
+ # to be used as an around_filter
11
+ def handle_timecop_offset
12
+ # Establish now
13
+ if !session[:timecop_adjusted_time].nil?
14
+ puts "***** Time traveling to #{session[:timecop_adjusted_time].to_s}"
15
+ Timecop.travel(session[:timecop_adjusted_time])
16
+ else
17
+ Timecop.return
18
+ end
19
+
20
+ # Run the intended action
21
+ yield
22
+
23
+ # we want to continue to slide time forward, even if it's only 3 seconds at a time.
24
+ # this ensures that subsequent calls during the same "time travel" actually pass time
25
+ if !session[:timecop_adjusted_time].nil?
26
+ puts "====== Resetting session to: #{Time.now + 3}"
27
+ session[:timecop_adjusted_time] = Time.now + 3 # slide it forward a couple of seconds
28
+ end
29
+ end
30
+
31
+ private :handle_timecop_offset
32
+
33
+ end
@@ -0,0 +1,7 @@
1
+
2
+ require 'timecop'
3
+ require 'timecop-console/timecop_controller_methods'
4
+ ActionController::Base.send(:include, TimecopControllerMethods)
5
+
6
+ require File.join(File.dirname(__FILE__), "timecop-console", "controllers", "timecop_controller")
7
+ require File.join(File.dirname(__FILE__), "timecop-console", "routes")
@@ -0,0 +1,6 @@
1
+
2
+ gems:
3
+ - name: Shoulda
4
+ version: '~> 1.2.0'
5
+ - name: timecop
6
+ version: '~> 0.2.1'
@@ -0,0 +1,9 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+
8
+ class Test::Unit::TestCase
9
+ end
@@ -0,0 +1,29 @@
1
+ require 'test_helper'
2
+ require 'actionpack'
3
+ require 'action_controller'
4
+ # require 'action_controller/assertions/routing_assertions'
5
+
6
+ require "timecop-console/timecop_controller_methods"
7
+
8
+ # We need this defined for the session key
9
+ class ApplicationController < ActionController::Base
10
+
11
+ end
12
+
13
+ require 'timecop_console'
14
+ class SampleController < ActionController::Base
15
+
16
+ end
17
+
18
+ class TimecopConsoleTest < Test::Unit::TestCase
19
+
20
+ should "add around_filter to SampleController" do
21
+ assert SampleController.around_filter.any?{|filter| filter.around? && filter.method == :handle_timecop_offset}, "before_filter should have been applied"
22
+ end
23
+
24
+ # should "recognize the new routes" do
25
+ # assert_recognizes({:controller => 'timecop', :action => 'update'})
26
+ # assert_recognizes({:controller => 'timecop', :action => 'reset'})
27
+ # end
28
+
29
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: timecop-console
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - John Trupiano
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-03-07 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: timecop
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ~>
22
+ - !ruby/object:Gem::Version
23
+ version: 0.2.1
24
+ version:
25
+ description:
26
+ email: jtrupiano@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - README.rdoc
33
+ - LICENSE
34
+ files:
35
+ - README.rdoc
36
+ - VERSION.yml
37
+ - lib/timecop-console
38
+ - lib/timecop-console/controllers
39
+ - lib/timecop-console/controllers/timecop_controller.rb
40
+ - lib/timecop-console/routes.rb
41
+ - lib/timecop-console/timecop_controller_methods.rb
42
+ - lib/timecop_console.rb
43
+ - test/geminstaller.yml
44
+ - test/test_helper.rb
45
+ - test/timecop_console_test.rb
46
+ - LICENSE
47
+ has_rdoc: true
48
+ homepage: http://github.com/jtrupiano/timecop-console
49
+ post_install_message:
50
+ rdoc_options:
51
+ - --inline-source
52
+ - --charset=UTF-8
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
+ version:
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: "0"
66
+ version:
67
+ requirements: []
68
+
69
+ rubyforge_project: johntrupiano
70
+ rubygems_version: 1.3.1
71
+ signing_key:
72
+ specification_version: 2
73
+ summary: Expose Timecop's capabilities to the UI in your rails app, allowing QA to take advantage of it.
74
+ test_files: []
75
+