public
Description: A Rails plugin that lets you embed actions in your views (with caching and response_to support)
Homepage: http://github.com/sd/embedded-actions/wikis/home
Clone URL: git://github.com/sd/embedded-actions.git
 r63@chuao:  sd | 2007-10-08 11:35:59 -0400
 Tests now pass cleanly


git-svn-id: 
https://dev.notso.net/svn/rails/plugins/embedded_actions/trunk@125 
71bf3dff-0d17-0410-8985-d5c41cf5d41c
sd (author)
Mon Oct 08 08:38:21 -0700 2007
commit  6b2494809ddaf6a68c46dcc367fa3e47e9eeee9b
tree    6ee57b4a350aef31d5e3b16fd47280c4b1ca0edc
parent  9cc2422f239bb5de88f0bf6cb63854ace65f537f
...
23
24
25
26
27
 
28
29
30
31
32
 
33
34
35
...
38
39
40
41
 
42
43
44
...
23
24
25
 
 
26
27
28
29
30
31
32
33
34
35
...
38
39
40
 
41
42
43
44
0
@@ -23,13 +23,13 @@ module ActionController
0
     
0
     module InstanceMethods
0
       def cache_embedded?(options)
0
- cache_this_instance = options.delete :caching # the rest of the request processing code doesn't have to know about this option
0
-
0
+ cache_this_instance = options[:params] && options[:params].delete(:caching) # the rest of the request processing code doesn't have to know about this option
0
         return false unless self.perform_caching
0
     
0
         if embedded_class(options).cached_embedded[options[:action].to_sym]
0
           return true unless cache_this_instance == false
0
         end
0
+
0
         return cache_this_instance
0
       end
0
 
0
@@ -38,7 +38,7 @@ module ActionController
0
       end
0
   
0
       def embed_action_as_string_with_caching(options)
0
- force_refresh = options.delete :refresh_cache
0
+ force_refresh = options[:params] && options[:params].delete(:refresh_cache)
0
         return embed_action_as_string_without_caching(options) unless self.cache_embedded?(options)
0
 
0
         unless not force_refresh and cached = send(:read_fragment, options)
...
26
27
28
 
29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
31
...
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
0
@@ -26,6 +26,23 @@ class TestController < ActionController::Base
0
   end
0
   
0
   def dump_params
0
+ render :inline => 'Params: <%= params.keys.sort.collect {|name| "#{name}: #{params[name]}"}.join ", " %>'
0
   end
0
+
0
+ def action_with_respond_to
0
+ respond_to do |format|
0
+ format.html { render :inline => "html content" }
0
+ format.embedded { render :inline => "embedded content" }
0
+ format.all { render :inline => "catch all" }
0
+ end
0
+ end
0
+
0
+ def action_that_calls_action_with_respond_to
0
+ render :inline => "<%= embed_action :action => 'action_with_respond_to' %>"
0
+ end
0
+
0
+ def inline_erb_action
0
+ render :inline => params[:erb]
0
+ end
0
 end
0
 
...
37
38
39
40
 
41
42
43
44
 
45
46
47
...
37
38
39
 
40
41
42
43
 
44
45
46
47
0
@@ -37,11 +37,11 @@ class Test::Unit::TestCase
0
 
0
 
0
   def assert_embed_erb(result, erb, msg = nil)
0
- TestController.send(:define_method, :test_action, Proc.new do
0
+ (class << @controller; self; end).send(:define_method, :test_action_for_assert_embed_erb, Proc.new do
0
       render :inline => erb
0
     end)
0
     
0
- get :test_action
0
+ get :test_action_for_assert_embed_erb
0
     assert_equal result, @response.body, msg
0
   end
0
 end
...
9
10
11
12
13
14
15
16
17
18
 
19
20
21
...
38
39
40
41
42
43
 
44
45
46
 
 
47
48
49
...
51
52
53
54
 
55
56
57
...
72
73
74
75
 
76
77
78
79
 
 
80
81
82
83
 
 
84
85
86
87
 
 
88
89
90
91
 
 
92
93
94
...
9
10
11
 
 
 
 
 
 
 
12
13
14
15
...
32
33
34
 
35
36
37
38
39
40
41
42
43
44
45
...
47
48
49
 
50
51
52
53
...
68
69
70
 
71
72
 
 
 
73
74
75
 
 
 
76
77
78
 
 
 
79
80
81
 
 
 
82
83
84
85
86
0
@@ -9,13 +9,7 @@ require File.expand_path(File.dirname(__FILE__) + "/rails/test/test_helper")
0
 # Re-raise errors caught by the controller.
0
 class TestController; def rescue_action(e) raise e end; end
0
 
0
-class TestController
0
- def default_url_options(options)
0
- {:id => 15, :category => "red"}
0
- end
0
-end
0
-
0
-class TestControllerWithDefaultUrlOptions < TestController
0
+class TestControllerWithLessDefaultUrlOptions < TestController
0
   def default_url_options(options)
0
     {:id => 15}
0
   end
0
@@ -38,12 +32,14 @@ end
0
 
0
 class DefaultEmbeddedOptionsTest < Test::Unit::TestCase
0
   def setup
0
- @controller = TestController.new
0
     @request = ActionController::TestRequest.new
0
     @response = ActionController::TestResponse.new
0
+ FileUtils.rm_rf "#{RAILS_ROOT}/tmp/cache/test.host"
0
   end
0
 
0
   def test_normalize_embedded_options
0
+ @controller = TestControllerWithMoreDefaultUrlOptions.new
0
+
0
     assert_equal({:id => 1, :params => {:category => "red"}}, @controller.normalize_embedded_options({:id => 1, :category => "red"}))
0
     assert_equal({:id => 1, :params => {:category => "red"}}, @controller.normalize_embedded_options({:id => 1, :params => {:category => "red"}}))
0
     assert_equal({:params => {:category => "blue"}}, @controller.normalize_embedded_options({:category => "blue"}))
0
@@ -51,7 +47,7 @@ class DefaultEmbeddedOptionsTest < Test::Unit::TestCase
0
   end
0
   
0
   def test_default_url_options
0
- @controller = TestControllerWithDefaultUrlOptions.new
0
+ @controller = TestControllerWithLessDefaultUrlOptions.new
0
     
0
     assert_equal({:id => 15}, @controller.rewrite_embedded_options({}))
0
     assert_equal({:id => 16}, @controller.rewrite_embedded_options({:id => 16}))
0
@@ -72,23 +68,19 @@ class DefaultEmbeddedOptionsTest < Test::Unit::TestCase
0
   end
0
   
0
   def test_embed_action_with_default_options
0
- @controller = TestController.new
0
+ @controller = TestControllerWithMoreDefaultUrlOptions.new
0
 
0
- assert_embed_erb "Params: action: dump_params, category: red, controller: test, id: 15\n",
0
- "<%= embed_action :action => 'dump_params' %>",
0
- "embed_action should use default options"
0
+ get :inline_erb_action, :erb => "<%= embed_action :action => 'dump_params' %>"
0
+ assert_equal "Params: action: dump_params, category: red, controller: test_controller_with_more_default_url_options, id: 15", @response.body, "embed_action should use default options"
0
 
0
- assert_embed_erb "Params: action: dump_params, category: red, controller: test, id: 16\n",
0
- "<%= embed_action :action => 'dump_params', :id => 16 %>",
0
- "embed_action should use default options"
0
+ get :inline_erb_action, :erb => "<%= embed_action :action => 'dump_params', :id => 16 %>"
0
+ assert_equal "Params: action: dump_params, category: red, controller: test_controller_with_more_default_url_options, id: 16", @response.body, "embed_action should use default options"
0
 
0
- assert_embed_erb "Params: action: dump_params, category: blue, controller: test, id: 16\n",
0
- "<%= embed_action :action => 'dump_params', :id => 16, :category => 'blue' %>",
0
- "embed_action should use default options"
0
+ get :inline_erb_action, :erb => "<%= embed_action :action => 'dump_params', :id => 16, :category => 'blue' %>"
0
+ assert_equal "Params: action: dump_params, category: blue, controller: test_controller_with_more_default_url_options, id: 16", @response.body, "embed_action should use default options"
0
 
0
- assert_embed_erb "Params: action: dump_params, category: blue, controller: test, id: 16\n",
0
- "<%= embed_action :action => 'dump_params', :id => 16, :params => {:category => 'blue'} %>",
0
- "embed_action should use default options"
0
+ get :inline_erb_action, :erb => "<%= embed_action :action => 'dump_params', :id => 16, :params => {:category => 'blue'} %>"
0
+ assert_equal "Params: action: dump_params, category: blue, controller: test_controller_with_more_default_url_options, id: 16", @response.body, "embed_action should use default options"
0
   end
0
   
0
 end
...
18
19
20
21
22
23
 
 
24
25
26
27
 
 
28
29
30
31
 
 
32
33
34
35
 
 
36
37
38
39
 
 
40
41
42
43
 
 
44
45
46
47
 
 
48
49
50
...
18
19
20
 
 
 
21
22
23
 
 
 
24
25
26
 
 
 
27
28
29
 
 
 
30
31
32
 
 
 
33
34
35
 
 
 
36
37
38
 
 
 
39
40
41
42
43
0
@@ -18,33 +18,26 @@ class EmbeddedActionTest < Test::Unit::TestCase
0
   end
0
 
0
   def test_embed_action
0
- assert_embed_erb "Params: action: dump_params, controller: test, id: \n",
0
- "<%= embed_action :action => 'dump_params' %>",
0
- "embed_action should accept implicit controller"
0
+ get :inline_erb_action, :erb => "<%= embed_action :action => 'dump_params' %>"
0
+ assert_equal "Params: action: dump_params, controller: test, id: ", @response.body, "embed_action should accept implicit controller"
0
 
0
- assert_embed_erb "Params: action: dump_params, controller: test, id: \n",
0
- "<%= embed_action :controller => 'test', :action => 'dump_params' %>",
0
- "embed_action should accept explicit controller"
0
+ get :inline_erb_action, :erb => "<%= embed_action :controller => 'test', :action => 'dump_params' %>"
0
+ assert_equal "Params: action: dump_params, controller: test, id: ", @response.body, "embed_action should accept explicit controller"
0
                      
0
- assert_embed_erb "Params: action: dump_params, controller: test, id: the id\n",
0
- "<%= embed_action :action => 'dump_params', :id => 'the id' %>",
0
- "embed_action should pass the id"
0
+ get :inline_erb_action, :erb => "<%= embed_action :action => 'dump_params', :id => 'the id' %>"
0
+ assert_equal "Params: action: dump_params, controller: test, id: the id", @response.body, "embed_action should pass the id"
0
                      
0
- assert_embed_erb "Params: action: dump_params, color: blue, controller: test, id: the id\n",
0
- "<%= embed_action :action => 'dump_params', :id => 'the id', :params => {:color => 'blue'} %>",
0
- "embed_action should pass params as expected"
0
+ get :inline_erb_action, :erb => "<%= embed_action :action => 'dump_params', :id => 'the id', :params => {:color => 'blue'} %>"
0
+ assert_equal "Params: action: dump_params, color: blue, controller: test, id: the id", @response.body, "embed_action should pass params as expected"
0
                      
0
- assert_embed_erb "Params: action: dump_params, color: blue, controller: test, id: the id\n",
0
- "<%= embed_action :action => 'dump_params', :id => 'the id', :color => 'blue' %>",
0
- "embed_action should merge into params anything that's not standard"
0
+ get :inline_erb_action, :erb => "<%= embed_action :action => 'dump_params', :id => 'the id', :color => 'blue' %>"
0
+ assert_equal "Params: action: dump_params, color: blue, controller: test, id: the id", @response.body, "embed_action should merge into params anything that's not standard"
0
                      
0
- assert_embed_erb "Params: action: dump_params, color: red, controller: test, id: the id\n",
0
- "<%= embed_action :action => 'dump_params', :id => 'the id', :color => 'blue', :params => {:color => 'red'} %>",
0
- "embed_action should override with the contents of params"
0
+ get :inline_erb_action, :erb => "<%= embed_action :action => 'dump_params', :id => 'the id', :color => 'blue', :params => {:color => 'red'} %>"
0
+ assert_equal "Params: action: dump_params, color: red, controller: test, id: the id", @response.body, "embed_action should override with the contents of params"
0
 
0
- assert_embed_erb "Params: action: dump_params, color: red, controller: test, id: the id\n",
0
- "<%= embed_action :action => 'dump_params', :id => 'the id', :color => 'blue', :params => {'color' => 'red'} %>",
0
- "embed_action should allow indifferent access"
0
+ get :inline_erb_action, :erb => "<%= embed_action :action => 'dump_params', :id => 'the id', :color => 'blue', :params => {'color' => 'red'} %>"
0
+ assert_equal "Params: action: dump_params, color: red, controller: test, id: the id", @response.body, "embed_action should allow indifferent access"
0
   end
0
 
0
 end
...
14
15
16
 
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
 
 
 
 
38
39
...
14
15
16
17
18
19
20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
22
23
24
25
26
27
28
29
0
@@ -14,26 +14,16 @@ class RespondsToTest < Test::Unit::TestCase
0
     @controller = TestController.new
0
     @request = ActionController::TestRequest.new
0
     @response = ActionController::TestResponse.new
0
+ FileUtils.rm_rf "#{RAILS_ROOT}/tmp/cache/test.host"
0
   end
0
 
0
   def test_responds_to_embedded
0
- TestController.class_eval do
0
- def action_with_respond_to
0
- respond_to do |format|
0
- format.html { render :inline => "html content" }
0
- format.embedded { render :inline => "embedded content" }
0
- format.all { render :inline => "catch all" }
0
- end
0
- end
0
- end
0
-
0
- assert_embed_erb "embedded content",
0
- "<%= embed_action :action => 'action_with_respond_to' %>",
0
- "should respond with embedded content"
0
- assert_equal "text/html", @response.content_type
0
-
0
     get :action_with_respond_to
0
     assert_equal "html content", @response.body, "should respond with html content"
0
     assert_equal "text/html", @response.content_type
0
+
0
+ get :action_that_calls_action_with_respond_to
0
+ assert_equal "embedded content", @response.body, "should respond with html content"
0
+ assert_equal "text/html", @response.content_type
0
   end
0
 end

Comments

    No one has commented yet.