response_for_rails 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (35) hide show
  1. data/.gitignore +5 -0
  2. data/CHANGELOG +22 -0
  3. data/Gemfile +3 -0
  4. data/Gemfile.lock.development +109 -0
  5. data/MIT-LICENSE +20 -0
  6. data/README.rdoc +126 -0
  7. data/Rakefile +32 -0
  8. data/lib/response_for/action_controller.rb +151 -0
  9. data/lib/response_for/railtie.rb +9 -0
  10. data/lib/response_for/responses_module.rb +47 -0
  11. data/lib/response_for/version.rb +3 -0
  12. data/lib/response_for.rb +7 -0
  13. data/lib/response_for_rails.rb +7 -0
  14. data/response_for.gemspec +30 -0
  15. data/spec/app/database.yml +5 -0
  16. data/spec/app/views/error_in_template_spec/test/error_in_template.html.erb +1 -0
  17. data/spec/app/views/inherited_controller_spec/super/a_response.html.erb +0 -0
  18. data/spec/app/views/inherited_controller_spec/super/an_action.html.erb +0 -0
  19. data/spec/app/views/pick_template_spec/template_only/an_action.atom.erb +1 -0
  20. data/spec/app/views/pick_template_spec/template_only/an_action.html.erb +1 -0
  21. data/spec/app/views/pick_template_spec/template_only/an_action.js.erb +1 -0
  22. data/spec/app/views/pick_template_spec/template_only/an_action.xml.erb +1 -0
  23. data/spec/app/views/stop_at_respond_to_spec/test/index.atom.builder +1 -0
  24. data/spec/app/views/stop_at_respond_to_spec/test/index.html.erb +1 -0
  25. data/spec/controllers/default_rails_behaviour_spec.rb +48 -0
  26. data/spec/controllers/error_in_template_spec.rb +20 -0
  27. data/spec/controllers/inherited_controllers_spec.rb +137 -0
  28. data/spec/controllers/no_response_if_performed_spec.rb +40 -0
  29. data/spec/controllers/pick_template_spec.rb +159 -0
  30. data/spec/controllers/remove_response_for_spec.rb +34 -0
  31. data/spec/controllers/responses_module_spec.rb +38 -0
  32. data/spec/controllers/stacking_responses_spec.rb +125 -0
  33. data/spec/controllers/stop_at_respond_to_spec.rb +51 -0
  34. data/spec/spec_helper.rb +27 -0
  35. metadata +142 -0
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ module ErrorInTemplateSpec
4
+ class TestController < ApplicationController
5
+ response_for :action_rendering_erroneous_template do |format|
6
+ format.html { render :action => 'error_in_template' }
7
+ end
8
+
9
+ def action_rendering_erroneous_template
10
+ end
11
+ end
12
+
13
+ describe TestController do
14
+ render_views
15
+
16
+ it "GET :action_rendering_erroneous_template should raise \"Boom!\"" do
17
+ lambda { get :action_rendering_erroneous_template }.should raise_error("Boom!")
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,137 @@
1
+ require 'spec_helper'
2
+
3
+ module InheritedControllerSpec
4
+ # example setup
5
+ class SuperController < ApplicationController
6
+ response_for :a_response do |format|
7
+ format.html { super_inside_a_response }
8
+ end
9
+
10
+ def a_response
11
+ end
12
+
13
+ def an_action
14
+ inside_an_action
15
+ end
16
+
17
+ def performing_action
18
+ respond_to do |format|
19
+ format.html { redirect_to 'http://redirected.from.an_action_which_performs' }
20
+ end
21
+ end
22
+
23
+ protected
24
+ def super_inside_a_response; end
25
+ def inside_an_action; end
26
+ end
27
+
28
+ class SubController < SuperController
29
+ response_for :a_response do |format|
30
+ format.html { sub_inside_a_response }
31
+ end
32
+
33
+ response_for :an_action do |format|
34
+ format.html do
35
+ redirect_to 'http://redirected.from.response_for'
36
+ end
37
+ end
38
+
39
+ response_for :performing_action do |format|
40
+ never_reached_because_action_performs
41
+ end
42
+
43
+ protected
44
+ def sub_inside_a_response; end
45
+ end
46
+
47
+ # specs
48
+ describe "action_responses", :type => :spec do
49
+ it "SuperController.action_responses should not == SubController.action_responses" do
50
+ SuperController.action_responses.should_not == SubController.action_responses
51
+ end
52
+
53
+ it "SuperController should have one action_response for 'a_response'" do
54
+ SuperController.action_responses.keys.should == ['a_response']
55
+ SuperController.action_responses['a_response'].size.should == 1
56
+ end
57
+
58
+ it "SubController should have two action_responses for 'a_response', and one each for 'an_action', and 'performing_action'" do
59
+ SubController.action_responses.keys.sort.should == ['a_response', 'an_action', 'performing_action']
60
+ SubController.action_responses['a_response'].size.should == 2
61
+ SubController.action_responses['an_action'].size.should == 1
62
+ SubController.action_responses['performing_action'].size.should == 1
63
+ end
64
+ end
65
+
66
+ describe SuperController do
67
+ describe "GET :an_action" do
68
+
69
+ it "should execute action" do
70
+ @controller.should_receive :inside_an_action
71
+ get :an_action
72
+ end
73
+
74
+ it "should render :an_action" do
75
+ get :an_action
76
+ response.should render_template('inherited_controller_spec/super/an_action')
77
+ end
78
+ end
79
+
80
+ describe "GET :a_response" do
81
+ it "should execute inside the super response block" do
82
+ @controller.should_receive :super_inside_a_response
83
+ get :a_response
84
+ end
85
+
86
+ it "should NOT execute inside the sub response block" do
87
+ @controller.should_not_receive :sub_inside_a_response
88
+ get :a_response
89
+ end
90
+ end
91
+
92
+ describe "GET :performing_action" do
93
+ it "should redirect" do
94
+ get :performing_action
95
+ response.should redirect_to('http://redirected.from.an_action_which_performs')
96
+ end
97
+ end
98
+ end
99
+
100
+ describe SubController do
101
+ describe "GET :an_action (decorated with redirecting response_for)" do
102
+ it "should execute action" do
103
+ @controller.should_receive :inside_an_action
104
+ get :an_action
105
+ end
106
+
107
+ it "should redirect" do
108
+ get :an_action
109
+ response.should redirect_to('http://redirected.from.response_for')
110
+ end
111
+ end
112
+
113
+ describe "GET :a_response (decorated with a new response)" do
114
+ it "should NOT execute the super response" do
115
+ @controller.should_not_receive :super_inside_a_response
116
+ get :a_response
117
+ end
118
+
119
+ it "should execute the sub response" do
120
+ @controller.should_receive :sub_inside_a_response
121
+ get :a_response
122
+ end
123
+ end
124
+
125
+ describe "GET :performing_action" do
126
+ it "should NOT execute the sub response" do
127
+ @controller.should_not_receive :never_reached_because_action_performs
128
+ get :performing_action
129
+ end
130
+
131
+ it "should redirect as per the super def" do
132
+ get :performing_action
133
+ response.should redirect_to('http://redirected.from.an_action_which_performs')
134
+ end
135
+ end
136
+ end
137
+ end
@@ -0,0 +1,40 @@
1
+ require 'spec_helper'
2
+
3
+ module NoResponseIfPerformedSpec
4
+ # example setup
5
+ class TestController < ApplicationController
6
+ before_filter :do_redirect
7
+
8
+ response_for :an_action do |format|
9
+ format.html { render :text => "in response for" }
10
+ end
11
+
12
+ def an_action
13
+ end
14
+
15
+ protected
16
+ def do_redirect
17
+ redirect_to 'http://redirected.from.before_filter'
18
+ end
19
+ end
20
+
21
+ describe TestController do
22
+ describe "(when before_filter redirects)" do
23
+ it "GET :an_action should redirect to 'http://redirected.from.before_filter'" do
24
+ get :an_action
25
+ response.should redirect_to('http://redirected.from.before_filter')
26
+ end
27
+ end
28
+
29
+ describe "(when before_filter doesn't redirect)" do
30
+ before do
31
+ @controller.stub!(:do_redirect)
32
+ end
33
+
34
+ it "GET :an_action should execute inside response for" do
35
+ get :an_action
36
+ response.body.should == 'in response for'
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,159 @@
1
+ require 'spec_helper'
2
+
3
+ module PickTemplateSpec
4
+ # example setup
5
+ class TemplateOnlyController < ApplicationController
6
+ end
7
+
8
+ class RespondToTypesController < TemplateOnlyController
9
+ def an_action
10
+ respond_to :atom, :xml, :html, :js
11
+ end
12
+ end
13
+
14
+ class RespondToBlockController < TemplateOnlyController
15
+ def an_action
16
+ respond_to do |format|
17
+ format.atom
18
+ format.xml
19
+ format.html
20
+ format.js
21
+ end
22
+ end
23
+ end
24
+
25
+ class ResponseForTypesController < TemplateOnlyController
26
+ response_for :an_action, :types => [:atom, :xml, :html, :js]
27
+ end
28
+
29
+ class ResponseForBlockController < TemplateOnlyController
30
+ response_for :an_action do |format|
31
+ format.atom
32
+ format.xml
33
+ format.html
34
+ format.js
35
+ end
36
+ end
37
+
38
+ class ResponseForMixOfBlockAndTypesController < TemplateOnlyController
39
+ # response_for gives priority to most recent declarations, and
40
+ # gives priority of blocks over types.
41
+ response_for :an_action, :types => :js
42
+ response_for :an_action, :types => :html
43
+ response_for :an_action, :types => :xml do |format|
44
+ format.atom
45
+ end
46
+ end
47
+
48
+ class SuperclassController < TemplateOnlyController
49
+ response_for :an_action, :types => :js
50
+ end
51
+
52
+ class InheritedController < SuperclassController
53
+ response_for :an_action, :types => [:xml, :html] do |format|
54
+ format.atom
55
+ end
56
+ end
57
+
58
+ # specs
59
+ shared_examples_for "Standard behaviour of respond_to :atom, :xml, :html, :js" do
60
+ it "GET :an_action, should render an_action.html" do
61
+ get :an_action
62
+ response.body.should == 'body of an_action.html'
63
+ end
64
+
65
+ describe "GET :an_action, HTTP_ACCEPT =" do
66
+ it "text/html, should render an_action.html" do
67
+ request.env["HTTP_ACCEPT"] = 'text/html'
68
+ get :an_action
69
+ response.body.should == 'body of an_action.html'
70
+ end
71
+
72
+ it "application/xml, should render an_action.xml" do
73
+ request.env["HTTP_ACCEPT"] = 'application/xml'
74
+ get :an_action
75
+ response.body.should == 'body of an_action.xml'
76
+ end
77
+
78
+ it "text/javascript, should render an_action.js" do
79
+ request.env["HTTP_ACCEPT"] = 'text/javascript'
80
+ get :an_action
81
+ response.body.should == 'body of an_action.js'
82
+ end
83
+
84
+ it "application/atom+xml, should render an_action.atom" do
85
+ request.env["HTTP_ACCEPT"] = 'application/atom+xml'
86
+ get :an_action
87
+ response.body.should == 'body of an_action.atom'
88
+ end
89
+ end
90
+
91
+ describe "GET :an_action, :format =>" do
92
+ it ":html, should render an_action.html" do
93
+ get :an_action, :format => 'html'
94
+ response.body.should == 'body of an_action.html'
95
+ end
96
+
97
+ it ":js, should render an_action.js" do
98
+ get :an_action, :format => 'js'
99
+ response.body.should == 'body of an_action.js'
100
+ end
101
+
102
+ it ":xml, should render an_action.xml" do
103
+ get :an_action, :format => 'xml'
104
+ response.body.should == 'body of an_action.xml'
105
+ end
106
+
107
+ it ":atom, should render an_action.atom" do
108
+ get :an_action, :format => 'atom'
109
+ response.body.should == 'body of an_action.atom'
110
+ end
111
+ end
112
+ end
113
+
114
+ describe "Picking template" do
115
+ render_views
116
+
117
+ describe TemplateOnlyController do
118
+ describe "GET :an_action, HTTP_ACCEPT =" do
119
+ it "text/html, should render an_action.html" do
120
+ request.env["HTTP_ACCEPT"] = 'text/html'
121
+ get :an_action
122
+ response.body.should == 'body of an_action.html'
123
+ end
124
+
125
+ it "application/xml, should render an_action.xml" do
126
+ request.env["HTTP_ACCEPT"] = 'application/xml'
127
+ get :an_action
128
+ response.body.should == 'body of an_action.xml'
129
+ end
130
+ end
131
+ end
132
+
133
+ describe "[:atom, :xml, :html, :js]" do
134
+ describe RespondToTypesController do
135
+ it_should_behave_like "Standard behaviour of respond_to :atom, :xml, :html, :js"
136
+ end
137
+
138
+ describe RespondToBlockController do
139
+ it_should_behave_like "Standard behaviour of respond_to :atom, :xml, :html, :js"
140
+ end
141
+
142
+ describe ResponseForTypesController do
143
+ it_should_behave_like "Standard behaviour of respond_to :atom, :xml, :html, :js"
144
+ end
145
+
146
+ describe ResponseForBlockController do
147
+ it_should_behave_like "Standard behaviour of respond_to :atom, :xml, :html, :js"
148
+ end
149
+
150
+ describe ResponseForMixOfBlockAndTypesController do
151
+ it_should_behave_like "Standard behaviour of respond_to :atom, :xml, :html, :js"
152
+ end
153
+
154
+ describe InheritedController do
155
+ it_should_behave_like "Standard behaviour of respond_to :atom, :xml, :html, :js"
156
+ end
157
+ end
158
+ end
159
+ end
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+
3
+ module RemoveResponseForSpec
4
+ class TheController < ApplicationController
5
+ response_for :foo, :bar, :types => :html
6
+ response_for :foo, :types => :js
7
+ end
8
+
9
+ describe TheController do
10
+ it "should have action_responses for :foo and :bar" do
11
+ TheController.action_responses.keys.sort.should == ['bar', 'foo']
12
+ end
13
+
14
+ describe ".remove_response_for :bar" do
15
+ before do
16
+ TheController.remove_response_for :bar
17
+ end
18
+
19
+ it "should hanve action_responses for :foo" do
20
+ TheController.action_responses.keys.should == ['foo']
21
+ end
22
+ end
23
+
24
+ describe ".remove_response_for" do
25
+ before do
26
+ TheController.remove_response_for
27
+ end
28
+
29
+ it "should have empty action_responses" do
30
+ TheController.action_responses.should be_empty
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+
3
+ module ResponsesModuleSpec
4
+ module MyActionsAndResponses
5
+
6
+ def self.included(controller)
7
+ controller.class_eval do
8
+ def defined_by_included; end
9
+ end
10
+ end
11
+
12
+ extend ResponseFor::ResponsesModule
13
+
14
+ def foo; end
15
+
16
+ response_for :foo do |format|
17
+ format.html {}
18
+ end
19
+ end
20
+
21
+ class MyController < ApplicationController
22
+ include MyActionsAndResponses
23
+ end
24
+
25
+ describe MyController do
26
+ it "should have action_response for :foo" do
27
+ @controller.class.action_responses['foo'].should_not == nil
28
+ end
29
+
30
+ it "should have action :foo" do
31
+ @controller.should respond_to(:foo)
32
+ end
33
+
34
+ it "should have method defined by included" do
35
+ @controller.should respond_to(:defined_by_included)
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,125 @@
1
+ require 'spec_helper'
2
+
3
+ # this spec shows how to stack responses - the important thing to note is that
4
+ # once a format block is executed, that will stop all subsequent blocks of that
5
+ # type being called.
6
+ module StackingResponsesSpec
7
+ class TestController < ApplicationController
8
+ response_for :foo do |format|
9
+ format.html do
10
+ render :text => "default"
11
+ end
12
+ end
13
+
14
+ response_for :foo do |format|
15
+ in_first
16
+ if params[:first]
17
+ format.html do
18
+ in_first_html
19
+ redirect_to 'http://first'
20
+ end
21
+ end
22
+ end
23
+
24
+ response_for :foo do |format|
25
+ in_second
26
+ if params[:second]
27
+ format.html do
28
+ in_second_html
29
+ redirect_to 'http://second'
30
+ end
31
+ end
32
+ end
33
+
34
+ def foo
35
+ end
36
+
37
+ protected
38
+ def in_first; end
39
+ def in_first_html; end
40
+ def in_second; end
41
+ def in_second_html; end
42
+ end
43
+
44
+ describe TestController, "with responses conditionally executed" do
45
+ describe "GET :foo (no conditions)" do
46
+ it "should execute second, then first, response" do
47
+ @controller.should_receive(:in_second).once.ordered
48
+ @controller.should_receive(:in_first).once.ordered
49
+ get :foo
50
+ end
51
+
52
+ it "should NOT execute the html response in first" do
53
+ @controller.should_not_receive(:in_first_html)
54
+ get :foo
55
+ end
56
+
57
+ it "should NOT execute the html response in second" do
58
+ @controller.should_not_receive(:in_second_html)
59
+ get :foo
60
+ end
61
+
62
+ it "should render the default response" do
63
+ get :foo
64
+ response.body.should == "default"
65
+ end
66
+ end
67
+
68
+ describe "GET :foo, :second => true" do
69
+ it "should execute second, then first, then html second, response" do
70
+ @controller.should_receive(:in_second).once.ordered
71
+ @controller.should_receive(:in_first).once.ordered
72
+ @controller.should_receive(:in_second_html).once.ordered
73
+ get :foo, :second => true
74
+ end
75
+
76
+ it "should redirect from second response" do
77
+ get :foo, :second => true
78
+ response.should redirect_to('http://second')
79
+ end
80
+
81
+ it "should NOT execute first html response" do
82
+ @controller.should_not_receive(:in_first_html)
83
+ get :foo, :second => true
84
+ end
85
+ end
86
+
87
+ describe "GET :foo, :first => true" do
88
+ it "should execute second, then first, then first html response" do
89
+ @controller.should_receive(:in_second).once.ordered
90
+ @controller.should_receive(:in_first).once.ordered
91
+ @controller.should_receive(:in_first_html).once.ordered
92
+ get :foo, :first => true
93
+ end
94
+
95
+ it "should redirect from first response" do
96
+ get :foo, :first => true
97
+ response.should redirect_to('http://first')
98
+ end
99
+
100
+ it "should NOT execute second html response" do
101
+ @controller.should_not_receive(:in_second_html)
102
+ get :foo, :first => true
103
+ end
104
+ end
105
+
106
+ describe "GET :foo, :first => true, :second => true (can't execute two html blocks)" do
107
+ it "should execute second, then first, then second html response" do
108
+ @controller.should_receive(:in_second).once.ordered
109
+ @controller.should_receive(:in_first).once.ordered
110
+ @controller.should_receive(:in_second_html).once.ordered
111
+ get :foo, :first => true, :second => true
112
+ end
113
+
114
+ it "should redirect from second response" do
115
+ get :foo, :first => true, :second => true
116
+ response.should redirect_to('http://second')
117
+ end
118
+
119
+ it "should NOT execute first html response" do
120
+ @controller.should_not_receive(:in_first_html)
121
+ get :foo, :first => true, :second => true
122
+ end
123
+ end
124
+ end
125
+ end
@@ -0,0 +1,51 @@
1
+ require 'spec_helper'
2
+
3
+ module StopAtRespondToSpec
4
+ class TestController < ApplicationController
5
+ def index
6
+ respond_to do |format|
7
+ format.html do
8
+ @html = true
9
+ end
10
+ format.atom do
11
+ @atom = true
12
+ end
13
+ end
14
+ end
15
+ end
16
+
17
+ shared_examples_for "Standard respond_to behaviour" do
18
+ it "should render the respond_to html response" do
19
+ request.env["HTTP_ACCEPT"] = 'text/html'
20
+ get :index
21
+ assigns['html'].should == true
22
+ response.body.should =~ /body of index\.html\.erb/
23
+ end
24
+
25
+ it "should render the respond_to atom response" do
26
+ request.env["HTTP_ACCEPT"] = 'application/atom+xml'
27
+ get :index
28
+ assigns['atom'].should == true
29
+ response.body.should =~ /body of index\.atom\.builder/
30
+ end
31
+ end
32
+
33
+ describe TestController do
34
+ render_views
35
+
36
+ it_should_behave_like "Standard respond_to behaviour"
37
+
38
+ describe "with a redundant response_for" do
39
+ before do
40
+ # this should be ignored, because index has a respond_to
41
+ TestController.response_for :index do |format|
42
+ format.html
43
+ format.js
44
+ format.xml
45
+ end
46
+ end
47
+
48
+ it_should_behave_like "Standard respond_to behaviour"
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,27 @@
1
+ ENV["RAILS_ENV"] ||= "test"
2
+ require 'rails/all'
3
+ require 'rspec'
4
+ require 'rspec/rails'
5
+ require File.expand_path('../../lib/response_for', __FILE__)
6
+
7
+ RSpec.configure do |config|
8
+ config.use_transactional_fixtures = true
9
+ config.use_instantiated_fixtures = false
10
+ end
11
+
12
+ module ResponseForTest
13
+ class Application < Rails::Application
14
+ config.active_support.deprecation = :stderr
15
+ paths['config/database'] = File.expand_path('../app/database.yml', __FILE__)
16
+ paths['log'] = File.expand_path('../../../tmp/log', __FILE__)
17
+ end
18
+ end
19
+
20
+ class ApplicationController < ActionController::Base
21
+ self.view_paths = [File.expand_path('../app/views', __FILE__)]
22
+ end
23
+
24
+ ResponseForTest::Application.initialize!
25
+ Rails.application.routes.draw do
26
+ match '*controller/:action'
27
+ end