rails-caddy 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (32) hide show
  1. data/LICENSE +20 -0
  2. data/README.rdoc +69 -0
  3. data/VERSION.yml +4 -0
  4. data/lib/rails-caddy.rb +43 -0
  5. data/lib/rails-caddy/controllers/rails_caddy_controller.rb +17 -0
  6. data/lib/rails-caddy/controllers/sanitize_email_controller.rb +42 -0
  7. data/lib/rails-caddy/controllers/session_editing_controller.rb +21 -0
  8. data/lib/rails-caddy/controllers/timecop_controller.rb +56 -0
  9. data/lib/rails-caddy/errors.rb +8 -0
  10. data/lib/rails-caddy/helpers/rails_caddy_helper.rb +33 -0
  11. data/lib/rails-caddy/session_controller_finder.rb +17 -0
  12. data/lib/rails-caddy/views/_rails_caddy.html.erb +57 -0
  13. data/lib/rails-caddy/views/_rails_caddy_css.html.erb +60 -0
  14. data/lib/rails-caddy/views/_rails_caddy_js.html.erb +88 -0
  15. data/test/README.txt +40 -0
  16. data/test/files/acet.rb +40 -0
  17. data/test/files/fct.rb +35 -0
  18. data/test/files/rcct.rb +13 -0
  19. data/test/files/sanitize_email_action_controller_extensions_test_methods.rb +71 -0
  20. data/test/files/sanitize_email_controller_test_methods.rb +31 -0
  21. data/test/files/session_editing_controller_test_methods.rb +41 -0
  22. data/test/files/timecop_action_controller_extensions_test_methods.rb +55 -0
  23. data/test/files/timecop_controller_test_methods.rb +30 -0
  24. data/test/geminstaller.yml +5 -0
  25. data/test/rails_caddy_controller_test.rb +43 -0
  26. data/test/rails_caddy_helper_test.rb +24 -0
  27. data/test/rails_caddy_test.rb +39 -0
  28. data/test/rails_modifier.rb +131 -0
  29. data/test/routes.rb +3 -0
  30. data/test/session_controller_finder_test.rb +57 -0
  31. data/test/test_helper.rb +12 -0
  32. metadata +110 -0
@@ -0,0 +1,88 @@
1
+ <script type="text/javascript">
2
+ var RailsCaddyCookieStore = {
3
+ shouldOpenOnLoad: function() {
4
+ var all_cookies = $A(document.cookie.split('; '));
5
+ var our_cookie = all_cookies.find(function(cookie) {
6
+ return cookie.split('=')[0] == "rails_caddy"
7
+ });
8
+
9
+ return our_cookie != undefined;
10
+ },
11
+
12
+ removeCookie: function() {
13
+ // Set the expiration date in the past
14
+ document.cookie = "rails_caddy=off;expires=" + new Date(new Date().getTime() - 31536000000).toGMTString();
15
+ },
16
+
17
+ setCookie: function() {
18
+ // Set the expiration date in the future
19
+ document.cookie = "rails_caddy=on;expires=" + new Date(new Date().getTime() + 31536000000).toGMTString();
20
+ }
21
+ };
22
+
23
+ var RailsCaddy = {
24
+
25
+ editors: {},
26
+
27
+ isExtended: false,
28
+ sessionVariableTemplate: new Template("<p>#{key}<br /> <strong><%= escape_javascript(rc_in_place_editor_field("\#{key}", "\#{value}")) %></strong><a href='javascript:void(0)' onclick='RailsCaddy.removeSessionVariable(\"#{key}\", this);' class='x'>x</a></p>"),
29
+
30
+ init: function() {
31
+ Event.observe('railsCaddyTab', 'click', RailsCaddy.slideSideBar, true);
32
+ if (RailsCaddyCookieStore.shouldOpenOnLoad()) {
33
+ RailsCaddy.slideSideBar();
34
+ }
35
+ },
36
+
37
+ slideSideBar: function() {
38
+
39
+ new Effect.toggle('railsCaddyContents', 'blind', {scaleX: 'true', scaleY: 'true;', scaleContent: false});
40
+
41
+ if (RailsCaddy.isExtended == 0) {
42
+ new Effect.Fade('railsCaddyContents', { duration:1.0, from:0.0, to:1.0 });
43
+ RailsCaddy.isExtended = true;
44
+ RailsCaddyCookieStore.setCookie();
45
+ }
46
+ else {
47
+ new Effect.Fade('railsCaddyContents', { duration:1.0, from:1.0, to:0.0 });
48
+ RailsCaddy.isExtended = false;
49
+ RailsCaddyCookieStore.removeCookie();
50
+ }
51
+
52
+ },
53
+
54
+ scroll: function() {
55
+ $('railsCaddy').style.top = (window.scrollY + 100) + "px";
56
+ },
57
+
58
+ sessionVariableEditor: function(key, value) {
59
+ if (!value) { value = "blank"; }
60
+ return RailsCaddy.sessionVariableTemplate.evaluate({key : key, value : value});
61
+ },
62
+
63
+ addSessionVariable: function() {
64
+ var val = prompt("Enter the name of the new session variable:");
65
+ if (val) {
66
+ $('sessionObjects').insert(RailsCaddy.sessionVariableEditor(val, "blank"));
67
+ setTimeout("RailsCaddy.editors['" + val + "'].enterEditMode();", 100);
68
+ }
69
+ },
70
+
71
+ removeSessionVariable: function(key, anchor) {
72
+ //alert('removing key: ' + key);
73
+ new Ajax.Request('<%= translated_remove_session_path %>' + key, {method: 'post',
74
+ onSuccess: function(response) {
75
+ RailsCaddy.editors[key] = null;
76
+ $(anchor.parentNode).remove();
77
+ }
78
+ });
79
+ }
80
+ };
81
+
82
+ if (Event.observe === undefined) {
83
+ alert("It appears that the Prototype library has not been loaded. rails_caddy currently requires Prototype.");
84
+ } else {
85
+ Event.observe(window, 'load', RailsCaddy.init, true);
86
+ Event.observe(window, 'scroll', RailsCaddy.scroll, true);
87
+ }
88
+ </script>
@@ -0,0 +1,40 @@
1
+ ## Steps to build a new baseline app in a separate rails version for testing
2
+
3
+ rails _VERSION_ testVERSION
4
+
5
+ ruby script/generate controller frogs index
6
+
7
+ rake db:migrate
8
+
9
+ edit frogs_controller, add:
10
+ %w(abc def ghi).each do |action|
11
+ define_method(action) do
12
+ session[:something] = action
13
+ render :action => 'index'
14
+ end
15
+ end
16
+
17
+ edit frogs/index.html.erb
18
+ <html>
19
+ <head>
20
+ <title>Test</title>
21
+ <%= javascript_include_tag :all %>
22
+ </head>
23
+ <body>
24
+ <% if %w(development staging).include?(RAILS_ENV) -%>
25
+ <%= rails_caddy %>
26
+ <% end -%>
27
+ </body>
28
+ </html>
29
+
30
+ edit application.rb
31
+ helper RailsCaddyHelper if %w(development staging).include? RAILS_ENV
32
+ layout nil
33
+
34
+ edit environment.rb
35
+ config.gem "rails-caddy"
36
+
37
+ require 'rails-caddy'
38
+ require_dependency 'application'
39
+ RailsCaddy.init!
40
+
@@ -0,0 +1,40 @@
1
+ # Becomes test/functional/action_controller_extensions_test.rb in rails apps. Tests that our extensions to ActionController::Base work
2
+ # in combination.
3
+
4
+ require File.join(File.dirname(__FILE__), '..', 'test_helper')
5
+ require 'shoulda'
6
+ require 'rr'
7
+ require File.join(File.dirname(__FILE__), '..', 'timecop_action_controller_extensions_test_methods')
8
+ require File.join(File.dirname(__FILE__), '..', 'sanitize_email_action_controller_extensions_test_methods')
9
+
10
+ class Test::Unit::TestCase
11
+ include RR::Adapters::TestUnit unless include?(RR::Adapters::TestUnit)
12
+ end
13
+
14
+ require 'rails-caddy'
15
+ RailsCaddy.init!
16
+
17
+ class DummyMailer < ActionMailer::Base
18
+ def test_email
19
+ recipients "nobody@smartlogicsolutions.com"
20
+ @subject = "A Title"
21
+ @body = "some dummy content"
22
+ end
23
+ end
24
+
25
+ ActionMailer::Base.delivery_method = :test
26
+ ActionMailer::Base.local_environments = ["test"]
27
+
28
+ class TestController < ActionController::Base
29
+ # Simply a helper for us
30
+ def self.around_filters
31
+ self.filter_chain.select {|filter| filter.class == ActionController::Filters::AroundFilter}.map(&:method)
32
+ end
33
+ end
34
+
35
+ class ActionControllerExtensionsTest < Test::Unit::TestCase
36
+
37
+ include TimecopActionControllerExtensionsTestMethods
38
+ include SanitizeEmailActionControllerExtensionsTestMethods
39
+
40
+ end
@@ -0,0 +1,35 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'test_helper')
2
+
3
+ class FrogsControllerTest < ActionController::TestCase
4
+ def test_should_render_rails_caddy_on_index_with_empty_session
5
+ get :index
6
+ assert_response :success
7
+ assert_template 'index'
8
+ end
9
+
10
+ def test_should_render_entries_for_each_key_in_the_session
11
+ sess = {"key1" => "abc", "key2" => "def"}
12
+ get :index, {}, sess
13
+ assert_response :success
14
+ assert_template 'index'
15
+ assert_outputs_session_vars(sess.keys)
16
+ end
17
+
18
+ def test_should_render_newly_created_session_variable
19
+ # Recall that hitting /abc sets the 'something' session variable to 'abc'
20
+ sess = {"key1" => "abc", "key2" => "def"}
21
+ get :abc, {}, sess
22
+ assert_response :success
23
+ assert_template 'index'
24
+ assert_outputs_session_vars(sess.keys + ["something"])
25
+ end
26
+
27
+ private
28
+ def assert_outputs_session_vars(keys)
29
+ keys.each do |key|
30
+ # ugly regexp...
31
+ regex = Regexp.new("sessionVariableEditor\\(\\\"#{key}\\\"")
32
+ assert_match regex, @response.body
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,13 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'test_helper')
2
+ require 'shoulda'
3
+ require File.join(File.dirname(__FILE__), '..', 'session_editing_controller_test_methods')
4
+ require File.join(File.dirname(__FILE__), '..', 'timecop_controller_test_methods')
5
+ require File.join(File.dirname(__FILE__), '..', 'sanitize_email_controller_test_methods')
6
+
7
+ class RailsCaddyControllerTest < ActionController::TestCase
8
+
9
+ include SessionEditingControllerTestMethods
10
+ include TimecopControllerTestMethods
11
+ include SanitizeEmailControllerTestMethods
12
+
13
+ end
@@ -0,0 +1,71 @@
1
+ module SanitizeEmailActionControllerExtensionsTestMethods
2
+
3
+ def self.included(base)
4
+ base.class_eval do
5
+ context "TestController has been extended by SanitizeEmailController::ActionControllerExtensions" do
6
+ setup do
7
+ @controller = TestController.new
8
+ end
9
+
10
+ # should "add :handle_sanitize_email as an around_filter" do
11
+ # assert @controller.class.around_filters.include?(:handle_sanitize_email)
12
+ # end
13
+
14
+ context "ActionMailer::Base.sanitized_recipients has been set in config/environment.rb" do
15
+
16
+ setup do
17
+ @default_sanitized_email = "jtrupiano@gmail.com"
18
+ ActionMailer::Base.sanitized_recipients = @default_sanitized_email
19
+ end
20
+
21
+ context "session[:sanitize_email_address] has been set and :handle_sanitize_email is invoked" do
22
+ setup do
23
+ @sanitize_email = "john@smartlogicsolutions.com"
24
+ @session_stub = {:sanitize_email_address => @sanitize_email}
25
+
26
+ stub(@controller).session { @session_stub }
27
+ end
28
+
29
+ should "set ActionMailer::Base.sanitized_recipients" do
30
+ @controller.send(:handle_sanitize_email) do
31
+ assert_not_nil ActionMailer::Base.sanitized_recipients
32
+ assert_equal @sanitize_email, ActionMailer::Base.sanitized_recipients
33
+ end
34
+ end
35
+
36
+ should "reroute generated emails as per session[:sanitize_email_address]" do
37
+ @controller.send(:handle_sanitize_email) do
38
+ mail = DummyMailer.create_test_email
39
+ assert_equal [@sanitize_email], mail.to
40
+ end
41
+ end
42
+ end
43
+
44
+ context "session[:sanitize_email_address] has not been set and :handle_sanitize_email is invoked" do
45
+ setup do
46
+ stub(@controller).session { {} }
47
+ end
48
+
49
+ should "not set ActionMailer::Base.sanitized_recipients" do
50
+ @controller.send(:handle_sanitize_email) do
51
+ assert_equal @default_sanitized_email, ActionMailer::Base.sanitized_recipients
52
+ # assert_nil ActionMailer::Base.sanitized_cc
53
+ # assert_nil ActionMailer::Base.sanitized_bcc
54
+ end
55
+ end
56
+
57
+ should "not reroute generated emails" do
58
+ @controller.send(:handle_sanitize_email) do
59
+ mail = DummyMailer.create_test_email
60
+ assert_equal [@default_sanitized_email], mail.to
61
+ end
62
+ end
63
+ end
64
+
65
+ end
66
+
67
+ end
68
+
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,31 @@
1
+ module SanitizeEmailControllerTestMethods
2
+
3
+ def self.included(base)
4
+ base.class_eval do
5
+ context "When the SanitizeEmailController module has been included" do
6
+
7
+ should "recognize route set_sanitize_email_address_path" do
8
+ assert_recognizes({:controller => 'rails_caddy', :action => 'set_sanitize_email_address'}, '/rails_caddy/set_sanitize_email_address')
9
+ end
10
+
11
+ should "recognize route unset_sanitize_email_address_path" do
12
+ assert_recognizes({:controller => 'rails_caddy', :action => 'unset_sanitize_email_address'}, '/rails_caddy/unset_sanitize_email_address')
13
+ end
14
+
15
+ should "set :sanitize_email_address session variable when #set_sanitize_email_address is invoked" do
16
+ post :set_sanitize_email_address, :value => "jtrupiano@gmail.com"
17
+ assert_response :success
18
+ assert_equal "jtrupiano@gmail.com", @response.body
19
+ assert_equal "jtrupiano@gmail.com", session[:sanitize_email_address]
20
+ end
21
+
22
+ should "unset :sanitize_email_address session variable when #unset_sanitize_email_address is invoked" do
23
+ post :unset_sanitize_email_address
24
+ assert_response :success
25
+ assert_equal "nil", @response.body
26
+ assert_nil session[:sanitize_email_address]
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,41 @@
1
+ module SessionEditingControllerTestMethods
2
+
3
+ def self.included(base)
4
+ base.class_eval do
5
+ context "When the SessionEditingController module has been included" do
6
+
7
+ should "recognize route update_session_path" do
8
+ assert_recognizes({:controller => 'rails_caddy', :action => 'update_session', :id => 'foo'}, '/rails_caddy/update_session/foo')
9
+ end
10
+
11
+ should "recognize route remove_session_path" do
12
+ assert_recognizes({:controller => 'rails_caddy', :action => 'remove_session', :id => 'foo'}, '/rails_caddy/remove_session/foo')
13
+ end
14
+
15
+ should "be able to update an existing session variable" do
16
+ post(:update_session, {:id => "username", :value => "joe"}, {:username => "john"})
17
+ assert_response 200
18
+ assert_equal "joe", session[:username]
19
+ end
20
+
21
+ should "be able to add a new session variable" do
22
+ post(:update_session, {:id => "name", :value => "jason"})
23
+ assert_response 200
24
+ assert_equal "jason", session[:name]
25
+ end
26
+
27
+ should "render 422 when session variable id is missing" do
28
+ post(:update_session, {})
29
+ assert_response 422
30
+ end
31
+
32
+ should "be able to remove an existing session variable" do
33
+ post(:remove_session, {:id => "username"}, {:username => "john"})
34
+ assert_response 200
35
+ assert_equal nil, session[:username]
36
+ end
37
+
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,55 @@
1
+ module TimecopActionControllerExtensionsTestMethods
2
+
3
+ def self.included(base)
4
+ base.class_eval do
5
+ context "TestController has been extended by TimecopController::ActionControllerExtensions" do
6
+ setup do
7
+ @controller = TestController.new
8
+ end
9
+
10
+ # should "add :handle_timecop_offset as an around_filter" do
11
+ # assert @controller.class.around_filters.include?(:handle_timecop_offset)
12
+ # end
13
+
14
+ context "session has been set" do
15
+ setup do
16
+ @travel_to = Time.local(2009, 1, 1)
17
+ @session_stub = {:timecop_adjusted_time => @travel_to}
18
+
19
+ stub(@controller).session { @session_stub }
20
+ end
21
+
22
+ should "adjust time" do
23
+ @controller.send(:handle_timecop_offset) do
24
+ assert_not_nil Time.now
25
+ assert (@travel_to - Time.now).abs < 100 # 100ms should be plenty of time for this test to run
26
+ # Brittle --> totally dependent upon implementation details of Timecop
27
+ assert (Time.now_without_mock_time - Time.now) > 5.days # We're well past Jan. 1, 2009, so this should be safe
28
+ end
29
+ end
30
+
31
+ should "move time forward by 3 seconds after yielding to consuming controller action" do
32
+ @controller.send(:handle_timecop_offset) do; end
33
+ # I can't do a straight assert_equal for some reason...
34
+ assert (@travel_to + 3 - @controller.session[:timecop_adjusted_time]).abs < 1
35
+ end
36
+ end
37
+
38
+ context "session has not been set" do
39
+ setup do
40
+ stub(@controller).session { {} }
41
+ end
42
+
43
+ should "not adjust time" do
44
+ @controller.send(:handle_timecop_offset) do
45
+ assert_not_nil Time.now
46
+ # Brittle --> totally dependent upon implementation details of Timecop
47
+ assert (Time.now_without_mock_time - Time.now) < 10 # shouldn't take more than 10ms to execute
48
+ end
49
+ end
50
+ end
51
+ end
52
+
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,30 @@
1
+ module TimecopControllerTestMethods
2
+
3
+ def self.included(base)
4
+ base.class_eval do
5
+ context "When the TimecopController module has been included" do
6
+
7
+ should "recognize route timecop_update_path" do
8
+ assert_recognizes({:controller => 'rails_caddy', :action => 'timecop_update'}, '/rails_caddy/timecop_update')
9
+ end
10
+
11
+ should "recognize route timecop_reset_path" do
12
+ assert_recognizes({:controller => 'rails_caddy', :action => 'timecop_reset'}, '/rails_caddy/timecop_reset')
13
+ end
14
+
15
+ should "update time when a valid year/month/day are passed into #timecop_update" do
16
+ post :timecop_update, {:year => 2008, :month => 12, :day => 1}
17
+ assert_response :success
18
+ assert_equal Time.local(2008, 12, 1), session[:timecop_adjusted_time]
19
+ end
20
+
21
+ should "unset :timecop_adjusted_time when #timecop_reset action is invoked" do
22
+ post :timecop_reset, {}, {:timecop_adjusted_time => Time.local(2008, 4, 4)}
23
+ assert_response :success
24
+ assert_equal nil, session[:timecop_adjusted_time]
25
+ end
26
+
27
+ end
28
+ end
29
+ end
30
+ end