responders_backport 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +7 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +22 -0
- data/Rakefile +41 -0
- data/init.rb +1 -0
- data/lib/responders_backport/respond_to.rb +154 -0
- data/lib/responders_backport/responder.rb +221 -0
- data/lib/responders_backport/version.rb +3 -0
- data/lib/responders_backport.rb +6 -0
- data/test/respond_to_test.rb +155 -0
- data/test/test_helper.rb +37 -0
- metadata +72 -0
data/CHANGELOG
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 George Guimarães and José Valim http://blog.plataformatec.com.br
|
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,22 @@
|
|
1
|
+
== Responders Backport for Rails 2.3.x
|
2
|
+
|
3
|
+
Loving Rails 3 Responders[1] feature? Me too.
|
4
|
+
|
5
|
+
Wants to use Responders in your Rails 2.3.x apps? Use InheritedResources. IR backports [2] Responder to your Rails 2.3.x apps and adds lots of useful and awesome features.
|
6
|
+
|
7
|
+
However, in some cases we don't want to use all IR awesome features. So, I extracted Responders-backporting-stuff to a minimal lib. This is mainly to be use as a dependency by Restfulie (http://github.com/caelum/restfulie)
|
8
|
+
|
9
|
+
|
10
|
+
=== Notes
|
11
|
+
|
12
|
+
[1] Please see these posts about Responders :
|
13
|
+
|
14
|
+
http://blog.plataformatec.com.br/2009/12/one-in-three-inherited-resources-has-scope-and-responders/
|
15
|
+
|
16
|
+
http://blog.plataformatec.com.br/2009/08/inherited-resources-is-scopes-and-responder-fluent/
|
17
|
+
|
18
|
+
|
19
|
+
[2] This is not entirely true, since Rails 3 Responders was actually *inspired* by InheritedResources new features to Rails 2.
|
20
|
+
|
21
|
+
Copyright (c) 2010 George Guimarães and José Valim http://blog.plataformatec.com.br
|
22
|
+
See the attached MIT License.
|
data/Rakefile
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require 'rake'
|
4
|
+
require 'rake/testtask'
|
5
|
+
require 'rake/rdoctask'
|
6
|
+
require File.join(File.dirname(__FILE__), 'lib', 'responders_backport', 'version')
|
7
|
+
|
8
|
+
begin
|
9
|
+
require 'jeweler'
|
10
|
+
Jeweler::Tasks.new do |s|
|
11
|
+
s.name = "responders_backport"
|
12
|
+
s.version = RespondersBackport::VERSION
|
13
|
+
s.rubyforge_project = "responders_backport"
|
14
|
+
s.summary = "Bringing Rails 3 Responders to your obsolete Rails 2.3.x apps =)"
|
15
|
+
s.email = "george@plataformatec.com.br"
|
16
|
+
s.homepage = "http://github.com/plataformatec/responders_backport"
|
17
|
+
s.description = "Bringing Rails 3 Responders to your obsolete Rails 2.3.x apps =)"
|
18
|
+
s.authors = ['George Guimarães', 'José Valim']
|
19
|
+
s.files = FileList["[A-Z]*", "init.rb", "{lib}/**/*"]
|
20
|
+
end
|
21
|
+
|
22
|
+
Jeweler::GemcutterTasks.new
|
23
|
+
rescue LoadError
|
24
|
+
puts "Jeweler, or one of its dependencies, is not available. Install it with: sudo gem install jeweler"
|
25
|
+
end
|
26
|
+
|
27
|
+
desc 'Run tests for RespondersBackport.'
|
28
|
+
Rake::TestTask.new(:test) do |t|
|
29
|
+
t.pattern = 'test/**/*_test.rb'
|
30
|
+
t.verbose = true
|
31
|
+
end
|
32
|
+
|
33
|
+
desc 'Generate documentation for RespondersBackport.'
|
34
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
35
|
+
rdoc.rdoc_dir = 'rdoc'
|
36
|
+
rdoc.title = 'RespondersBackport'
|
37
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
38
|
+
rdoc.rdoc_files.include('README.rdoc')
|
39
|
+
rdoc.rdoc_files.include('MIT-LICENSE')
|
40
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
41
|
+
end
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'responders_backport'
|
@@ -0,0 +1,154 @@
|
|
1
|
+
module ActionController #:nodoc:
|
2
|
+
class Base #:nodoc:
|
3
|
+
attr_accessor :formats
|
4
|
+
|
5
|
+
class_inheritable_accessor :mimes_for_respond_to, :responder, :instance_writer => false
|
6
|
+
|
7
|
+
self.responder = ActionController::Responder
|
8
|
+
self.mimes_for_respond_to = ActiveSupport::OrderedHash.new
|
9
|
+
|
10
|
+
if defined?(ApplicationController)
|
11
|
+
ApplicationController.responder ||= ActionController::Responder
|
12
|
+
ApplicationController.mimes_for_respond_to ||= ActiveSupport::OrderedHash.new
|
13
|
+
end
|
14
|
+
|
15
|
+
# Defines mimes that are rendered by default when invoking respond_with.
|
16
|
+
#
|
17
|
+
# Examples:
|
18
|
+
#
|
19
|
+
# respond_to :html, :xml, :json
|
20
|
+
#
|
21
|
+
# All actions on your controller will respond to :html, :xml and :json.
|
22
|
+
#
|
23
|
+
# But if you want to specify it based on your actions, you can use only and
|
24
|
+
# except:
|
25
|
+
#
|
26
|
+
# respond_to :html
|
27
|
+
# respond_to :xml, :json, :except => [ :edit ]
|
28
|
+
#
|
29
|
+
# The definition above explicits that all actions respond to :html. And all
|
30
|
+
# actions except :edit respond to :xml and :json.
|
31
|
+
#
|
32
|
+
# You can specify also only parameters:
|
33
|
+
#
|
34
|
+
# respond_to :rjs, :only => :create
|
35
|
+
#
|
36
|
+
def self.respond_to(*mimes)
|
37
|
+
options = mimes.extract_options!
|
38
|
+
clear_respond_to unless mimes_for_respond_to
|
39
|
+
|
40
|
+
only_actions = Array(options.delete(:only))
|
41
|
+
except_actions = Array(options.delete(:except))
|
42
|
+
|
43
|
+
mimes.each do |mime|
|
44
|
+
mime = mime.to_sym
|
45
|
+
mimes_for_respond_to[mime] = {}
|
46
|
+
mimes_for_respond_to[mime][:only] = only_actions unless only_actions.empty?
|
47
|
+
mimes_for_respond_to[mime][:except] = except_actions unless except_actions.empty?
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
# Clear all mimes in respond_to.
|
52
|
+
def self.clear_respond_to
|
53
|
+
write_inheritable_attribute(:mimes_for_respond_to, ActiveSupport::OrderedHash.new)
|
54
|
+
end
|
55
|
+
|
56
|
+
def respond_to(*mimes, &block)
|
57
|
+
raise ArgumentError, "respond_to takes either types or a block, never both" if mimes.any? && block_given?
|
58
|
+
if response = retrieve_response_from_mimes(mimes, &block)
|
59
|
+
response.call
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def respond_with(*resources, &block)
|
64
|
+
if response = retrieve_response_from_mimes([], &block)
|
65
|
+
options = resources.extract_options!
|
66
|
+
options.merge!(:default_response => response)
|
67
|
+
(options.delete(:responder) || responder).call(self, resources, options)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
protected
|
72
|
+
|
73
|
+
# Collect mimes declared in the class method respond_to valid for the
|
74
|
+
# current action.
|
75
|
+
#
|
76
|
+
def collect_mimes_from_class_level #:nodoc:
|
77
|
+
action = action_name.to_sym
|
78
|
+
|
79
|
+
mimes_for_respond_to.keys.select do |mime|
|
80
|
+
config = mimes_for_respond_to[mime]
|
81
|
+
|
82
|
+
if config[:except]
|
83
|
+
!config[:except].include?(action)
|
84
|
+
elsif config[:only]
|
85
|
+
config[:only].include?(action)
|
86
|
+
else
|
87
|
+
true
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
# Collects mimes and return the response for the negotiated format. Returns
|
93
|
+
# nil if :not_acceptable was sent to the client.
|
94
|
+
#
|
95
|
+
def retrieve_response_from_mimes(mimes, &block)
|
96
|
+
responder = ActionController::MimeResponds::Responder.new(self)
|
97
|
+
mimes = collect_mimes_from_class_level if mimes.empty?
|
98
|
+
mimes.each { |mime| responder.send(mime) }
|
99
|
+
block.call(responder) if block_given?
|
100
|
+
|
101
|
+
if format = responder.negotiate_mime
|
102
|
+
self.response.template.template_format = format.to_sym
|
103
|
+
self.response.content_type = format.to_s
|
104
|
+
self.formats = [ format.to_sym ]
|
105
|
+
responder.response_for(format) || proc { default_render }
|
106
|
+
else
|
107
|
+
head :not_acceptable
|
108
|
+
nil
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
module MimeResponds
|
114
|
+
class Responder #:nodoc:
|
115
|
+
attr_reader :order
|
116
|
+
|
117
|
+
def any(*args, &block)
|
118
|
+
if args.any?
|
119
|
+
args.each { |type| send(type, &block) }
|
120
|
+
else
|
121
|
+
custom(Mime::ALL, &block)
|
122
|
+
end
|
123
|
+
end
|
124
|
+
alias :all :any
|
125
|
+
|
126
|
+
def custom(mime_type, &block)
|
127
|
+
mime_type = mime_type.is_a?(Mime::Type) ? mime_type : Mime::Type.lookup(mime_type.to_s)
|
128
|
+
@order << mime_type
|
129
|
+
@responses[mime_type] ||= block
|
130
|
+
end
|
131
|
+
|
132
|
+
def response_for(mime)
|
133
|
+
@responses[mime] || @responses[Mime::ALL]
|
134
|
+
end
|
135
|
+
|
136
|
+
def negotiate_mime
|
137
|
+
@mime_type_priority.each do |priority|
|
138
|
+
if priority == Mime::ALL
|
139
|
+
return @order.first
|
140
|
+
elsif @order.include?(priority)
|
141
|
+
return priority
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
if @order.include?(Mime::ALL)
|
146
|
+
return Mime::SET.first if @mime_type_priority.first == Mime::ALL
|
147
|
+
return @mime_type_priority.first
|
148
|
+
end
|
149
|
+
|
150
|
+
nil
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end
|
@@ -0,0 +1,221 @@
|
|
1
|
+
module ActionController #:nodoc:
|
2
|
+
# Responder is responsible to expose a resource for different mime requests,
|
3
|
+
# usually depending on the HTTP verb. The responder is triggered when
|
4
|
+
# respond_with is called. The simplest case to study is a GET request:
|
5
|
+
#
|
6
|
+
# class PeopleController < ApplicationController
|
7
|
+
# respond_to :html, :xml, :json
|
8
|
+
#
|
9
|
+
# def index
|
10
|
+
# @people = Person.find(:all)
|
11
|
+
# respond_with(@people)
|
12
|
+
# end
|
13
|
+
# end
|
14
|
+
#
|
15
|
+
# When a request comes, for example with format :xml, three steps happen:
|
16
|
+
#
|
17
|
+
# 1) responder searches for a template at people/index.xml;
|
18
|
+
#
|
19
|
+
# 2) if the template is not available, it will invoke :to_xml in the given resource;
|
20
|
+
#
|
21
|
+
# 3) if the responder does not respond_to :to_xml, call :to_format on it.
|
22
|
+
#
|
23
|
+
# === Builtin HTTP verb semantics
|
24
|
+
#
|
25
|
+
# Rails default responder holds semantics for each HTTP verb. Depending on the
|
26
|
+
# content type, verb and the resource status, it will behave differently.
|
27
|
+
#
|
28
|
+
# Using Rails default responder, a POST request for creating an object could
|
29
|
+
# be written as:
|
30
|
+
#
|
31
|
+
# def create
|
32
|
+
# @user = User.new(params[:user])
|
33
|
+
# flash[:notice] = 'User was successfully created.' if @user.save
|
34
|
+
# respond_with(@user)
|
35
|
+
# end
|
36
|
+
#
|
37
|
+
# Which is exactly the same as:
|
38
|
+
#
|
39
|
+
# def create
|
40
|
+
# @user = User.new(params[:user])
|
41
|
+
#
|
42
|
+
# respond_to do |format|
|
43
|
+
# if @user.save
|
44
|
+
# flash[:notice] = 'User was successfully created.'
|
45
|
+
# format.html { redirect_to(@user) }
|
46
|
+
# format.xml { render :xml => @user, :status => :created, :location => @user }
|
47
|
+
# else
|
48
|
+
# format.html { render :action => "new" }
|
49
|
+
# format.xml { render :xml => @user.errors, :status => :unprocessable_entity }
|
50
|
+
# end
|
51
|
+
# end
|
52
|
+
# end
|
53
|
+
#
|
54
|
+
# The same happens for PUT and DELETE requests.
|
55
|
+
#
|
56
|
+
# === Nested resources
|
57
|
+
#
|
58
|
+
# You can given nested resource as you do in form_for and polymorphic_url.
|
59
|
+
# Consider the project has many tasks example. The create action for
|
60
|
+
# TasksController would be like:
|
61
|
+
#
|
62
|
+
# def create
|
63
|
+
# @project = Project.find(params[:project_id])
|
64
|
+
# @task = @project.comments.build(params[:task])
|
65
|
+
# flash[:notice] = 'Task was successfully created.' if @task.save
|
66
|
+
# respond_with(@project, @task)
|
67
|
+
# end
|
68
|
+
#
|
69
|
+
# Giving an array of resources, you ensure that the responder will redirect to
|
70
|
+
# project_task_url instead of task_url.
|
71
|
+
#
|
72
|
+
# Namespaced and singleton resources requires a symbol to be given, as in
|
73
|
+
# polymorphic urls. If a project has one manager which has many tasks, it
|
74
|
+
# should be invoked as:
|
75
|
+
#
|
76
|
+
# respond_with(@project, :manager, @task)
|
77
|
+
#
|
78
|
+
# Check polymorphic_url documentation for more examples.
|
79
|
+
#
|
80
|
+
class Responder
|
81
|
+
attr_reader :controller, :request, :response, :format, :resource, :resources, :options
|
82
|
+
|
83
|
+
ACTIONS_FOR_VERBS = {
|
84
|
+
:post => :new,
|
85
|
+
:put => :edit
|
86
|
+
}
|
87
|
+
|
88
|
+
def initialize(controller, resources, options={})
|
89
|
+
@controller = controller
|
90
|
+
@request = controller.request
|
91
|
+
@response = controller.response
|
92
|
+
@format = controller.formats.first
|
93
|
+
@resource = resources.is_a?(Array) ? resources.last : resources
|
94
|
+
@resources = resources
|
95
|
+
@options = options
|
96
|
+
@action = options.delete(:action)
|
97
|
+
@default_response = options.delete(:default_response)
|
98
|
+
end
|
99
|
+
|
100
|
+
delegate :head, :render, :redirect_to, :to => :controller
|
101
|
+
delegate :get?, :post?, :put?, :delete?, :to => :request
|
102
|
+
|
103
|
+
# Undefine :to_json and :to_yaml since it's defined on Object
|
104
|
+
undef_method(:to_json) if method_defined?(:to_json)
|
105
|
+
undef_method(:to_yaml) if method_defined?(:to_yaml)
|
106
|
+
|
107
|
+
# Initializes a new responder an invoke the proper format. If the format is
|
108
|
+
# not defined, call to_format.
|
109
|
+
#
|
110
|
+
def self.call(*args)
|
111
|
+
new(*args).respond
|
112
|
+
end
|
113
|
+
|
114
|
+
# Main entry point for responder responsible to dispatch to the proper format.
|
115
|
+
#
|
116
|
+
def respond
|
117
|
+
method = :"to_#{format}"
|
118
|
+
respond_to?(method) ? send(method) : to_format
|
119
|
+
end
|
120
|
+
|
121
|
+
# HTML format does not render the resource, it always attempt to render a
|
122
|
+
# template.
|
123
|
+
#
|
124
|
+
def to_html
|
125
|
+
default_render
|
126
|
+
rescue ActionView::MissingTemplate => e
|
127
|
+
navigation_behavior(e)
|
128
|
+
end
|
129
|
+
|
130
|
+
# All others formats follow the procedure below. First we try to render a
|
131
|
+
# template, if the template is not available, we verify if the resource
|
132
|
+
# responds to :to_format and display it.
|
133
|
+
#
|
134
|
+
def to_format
|
135
|
+
default_render
|
136
|
+
rescue ActionView::MissingTemplate => e
|
137
|
+
raise unless resourceful?
|
138
|
+
api_behavior(e)
|
139
|
+
end
|
140
|
+
|
141
|
+
protected
|
142
|
+
|
143
|
+
# This is the common behavior for "navigation" requests, like :html, :iphone and so forth.
|
144
|
+
def navigation_behavior(error)
|
145
|
+
if get?
|
146
|
+
raise error
|
147
|
+
elsif has_errors? && default_action
|
148
|
+
render :action => default_action
|
149
|
+
else
|
150
|
+
redirect_to resource_location
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
# This is the common behavior for "API" requests, like :xml and :json.
|
155
|
+
def api_behavior(error)
|
156
|
+
if get?
|
157
|
+
display resource
|
158
|
+
elsif has_errors?
|
159
|
+
display resource.errors, :status => :unprocessable_entity
|
160
|
+
elsif post?
|
161
|
+
display resource, :status => :created, :location => resource_location
|
162
|
+
else
|
163
|
+
head :ok
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
# Checks whether the resource responds to the current format or not.
|
168
|
+
#
|
169
|
+
def resourceful?
|
170
|
+
resource.respond_to?(:"to_#{format}")
|
171
|
+
end
|
172
|
+
|
173
|
+
# Returns the resource location by retrieving it from the options or
|
174
|
+
# returning the resources array.
|
175
|
+
#
|
176
|
+
def resource_location
|
177
|
+
options[:location] || resources
|
178
|
+
end
|
179
|
+
|
180
|
+
# If a given response block was given, use it, otherwise call render on
|
181
|
+
# controller.
|
182
|
+
#
|
183
|
+
def default_render
|
184
|
+
@default_response.call
|
185
|
+
end
|
186
|
+
|
187
|
+
# display is just a shortcut to render a resource with the current format.
|
188
|
+
#
|
189
|
+
# display @user, :status => :ok
|
190
|
+
#
|
191
|
+
# For xml request is equivalent to:
|
192
|
+
#
|
193
|
+
# render :xml => @user, :status => :ok
|
194
|
+
#
|
195
|
+
# Options sent by the user are also used:
|
196
|
+
#
|
197
|
+
# respond_with(@user, :status => :created)
|
198
|
+
# display(@user, :status => :ok)
|
199
|
+
#
|
200
|
+
# Results in:
|
201
|
+
#
|
202
|
+
# render :xml => @user, :status => :created
|
203
|
+
#
|
204
|
+
def display(resource, given_options={})
|
205
|
+
controller.send :render, given_options.merge!(options).merge!(format => resource)
|
206
|
+
end
|
207
|
+
|
208
|
+
# Check if the resource has errors or not.
|
209
|
+
#
|
210
|
+
def has_errors?
|
211
|
+
resource.respond_to?(:errors) && !resource.errors.empty?
|
212
|
+
end
|
213
|
+
|
214
|
+
# By default, render the :edit action for html requests with failure, unless
|
215
|
+
# the verb is post.
|
216
|
+
#
|
217
|
+
def default_action
|
218
|
+
@action ||= ACTIONS_FOR_VERBS[request.method]
|
219
|
+
end
|
220
|
+
end
|
221
|
+
end
|
@@ -0,0 +1,155 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
|
3
|
+
class Project
|
4
|
+
def to_html
|
5
|
+
'Generated HTML'
|
6
|
+
end
|
7
|
+
|
8
|
+
def to_xml
|
9
|
+
'Generated XML'
|
10
|
+
end
|
11
|
+
|
12
|
+
[:to_json, :to_rss, :to_rjs].each do |method|
|
13
|
+
undef_method method if respond_to? method
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class ProjectsController < ActionController::Base
|
18
|
+
respond_to :html
|
19
|
+
respond_to :xml, :except => :edit
|
20
|
+
respond_to :rjs, :only => :edit
|
21
|
+
respond_to :rss, :only => :index
|
22
|
+
respond_to :json, :except => :index
|
23
|
+
respond_to :csv, :except => :index
|
24
|
+
|
25
|
+
def index
|
26
|
+
respond_with(Project.new)
|
27
|
+
end
|
28
|
+
|
29
|
+
def respond_with_resource
|
30
|
+
respond_with(Project.new)
|
31
|
+
end
|
32
|
+
|
33
|
+
def respond_with_resource_and_options
|
34
|
+
respond_with(Project.new, :location => 'http://test.host/')
|
35
|
+
end
|
36
|
+
|
37
|
+
def respond_with_resource_and_blocks
|
38
|
+
respond_with(Project.new) do |format|
|
39
|
+
format.json { render :text => 'Render JSON' }
|
40
|
+
format.rss { render :text => 'Render RSS' }
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
# If the user request Mime::ALL and we have a template called action.html.erb,
|
45
|
+
# the html template should be rendered *unless* html is specified inside the
|
46
|
+
# block. This tests exactly this case.
|
47
|
+
#
|
48
|
+
def respond_to_skip_default_template
|
49
|
+
respond_with(Project.new) do |format|
|
50
|
+
format.html { render :text => 'Render HTML' }
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
class SuperProjectsController < ProjectsController
|
56
|
+
end
|
57
|
+
|
58
|
+
class RespondToFunctionalTest < ActionController::TestCase
|
59
|
+
tests ProjectsController
|
60
|
+
|
61
|
+
def test_respond_with_layout_rendering
|
62
|
+
@request.accept = 'text/html'
|
63
|
+
get :index
|
64
|
+
assert_equal 'Index HTML', @response.body.strip
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_respond_with_calls_to_format_on_resource
|
68
|
+
@request.accept = 'application/xml'
|
69
|
+
get :index
|
70
|
+
assert_equal 'Generated XML', @response.body.strip
|
71
|
+
end
|
72
|
+
|
73
|
+
def test_respond_with_inherits_format
|
74
|
+
@request.accept = 'application/xml'
|
75
|
+
get :index
|
76
|
+
assert_equal 'Generated XML', @response.body.strip
|
77
|
+
end
|
78
|
+
|
79
|
+
def test_respond_with_renders_status_not_acceptable_if_mime_type_is_not_registered
|
80
|
+
@request.accept = 'text/csv'
|
81
|
+
get :index
|
82
|
+
assert_equal '406 Not Acceptable', @response.status
|
83
|
+
end
|
84
|
+
|
85
|
+
def test_respond_with_raises_error_if_could_not_respond
|
86
|
+
@request.accept = 'application/rss+xml'
|
87
|
+
assert_raise ActionView::MissingTemplate do
|
88
|
+
get :index
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
def test_respond_to_all
|
93
|
+
@request.accept = '*/*'
|
94
|
+
get :index
|
95
|
+
assert_equal 'Index HTML', @response.body.strip
|
96
|
+
end
|
97
|
+
|
98
|
+
def test_respond_with_sets_content_type_properly
|
99
|
+
@request.accept = 'text/html'
|
100
|
+
get :index
|
101
|
+
assert_equal 'text/html', @response.content_type
|
102
|
+
assert_equal :html, @response.template.template_format
|
103
|
+
|
104
|
+
@request.accept = 'application/xml'
|
105
|
+
get :index
|
106
|
+
assert_equal 'application/xml', @response.content_type
|
107
|
+
assert_equal :xml, @response.template.template_format
|
108
|
+
end
|
109
|
+
|
110
|
+
def test_respond_with_forwads_extra_options_to_render
|
111
|
+
@request.accept = 'application/xml'
|
112
|
+
get :respond_with_resource_and_options
|
113
|
+
assert_equal 'Generated XML', @response.body.strip
|
114
|
+
assert_equal 'http://test.host/', @response.headers['Location']
|
115
|
+
end
|
116
|
+
|
117
|
+
def test_respond_to_when_a_resource_is_given_as_option
|
118
|
+
@request.accept = 'text/html'
|
119
|
+
get :respond_with_resource
|
120
|
+
assert_equal 'RespondTo HTML', @response.body.strip
|
121
|
+
|
122
|
+
@request.accept = 'application/xml'
|
123
|
+
get :respond_with_resource
|
124
|
+
assert_equal 'Generated XML', @response.body.strip
|
125
|
+
|
126
|
+
@request.accept = 'application/rss+xml'
|
127
|
+
get :respond_with_resource
|
128
|
+
assert_equal '406 Not Acceptable', @response.status
|
129
|
+
|
130
|
+
@request.accept = 'application/json'
|
131
|
+
assert_raise ActionView::MissingTemplate do
|
132
|
+
get :respond_with_resource
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
def test_respond_to_overwrite_class_method_definition
|
137
|
+
@request.accept = 'application/rss+xml'
|
138
|
+
get :respond_with_resource_and_blocks
|
139
|
+
assert_equal 'Render RSS', @response.body.strip
|
140
|
+
end
|
141
|
+
|
142
|
+
def test_respond_to_first_configured_mime_in_respond_to_when_mime_type_is_all
|
143
|
+
@request.accept = '*/*'
|
144
|
+
assert_raise ActionView::MissingTemplate do
|
145
|
+
get :respond_with_resource_and_blocks
|
146
|
+
end
|
147
|
+
assert_equal 'text/html', @response.content_type
|
148
|
+
end
|
149
|
+
|
150
|
+
def test_respond_to_skip_default_template_when_it_is_in_block
|
151
|
+
@request.accept = '*/*'
|
152
|
+
get :respond_to_skip_default_template
|
153
|
+
assert_equal 'Render HTML', @response.body.strip
|
154
|
+
end
|
155
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
|
3
|
+
begin
|
4
|
+
gem "test-unit"
|
5
|
+
rescue LoadError
|
6
|
+
end
|
7
|
+
|
8
|
+
begin
|
9
|
+
gem "ruby-debug"
|
10
|
+
require 'ruby-debug'
|
11
|
+
rescue LoadError
|
12
|
+
end
|
13
|
+
|
14
|
+
require 'test/unit'
|
15
|
+
|
16
|
+
ENV["RAILS_ENV"] = "test"
|
17
|
+
RAILS_ROOT = "anywhere"
|
18
|
+
|
19
|
+
require 'active_support'
|
20
|
+
require 'action_controller'
|
21
|
+
require 'action_controller/test_case'
|
22
|
+
require 'action_controller/test_process'
|
23
|
+
|
24
|
+
I18n.load_path << File.join(File.dirname(__FILE__), 'locales', 'en.yml')
|
25
|
+
I18n.reload!
|
26
|
+
|
27
|
+
class ApplicationController < ActionController::Base; end
|
28
|
+
|
29
|
+
# Add RespondersBackport to load path and load the main file
|
30
|
+
ActiveSupport::Dependencies.load_paths << File.expand_path(File.dirname(__FILE__) + '/../lib')
|
31
|
+
require_dependency 'responders_backport'
|
32
|
+
|
33
|
+
ActionController::Base.view_paths = File.join(File.dirname(__FILE__), 'views')
|
34
|
+
|
35
|
+
ActionController::Routing::Routes.draw do |map|
|
36
|
+
map.connect ':controller/:action/:id'
|
37
|
+
end
|
metadata
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: responders_backport
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 1
|
9
|
+
version: 0.1.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- "George Guimar\xC3\xA3es"
|
13
|
+
- "Jos\xC3\xA9 Valim"
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-03-15 00:00:00 -03:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: Bringing Rails 3 Responders to your obsolete Rails 2.3.x apps =)
|
23
|
+
email: george@plataformatec.com.br
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files:
|
29
|
+
- README.rdoc
|
30
|
+
files:
|
31
|
+
- CHANGELOG
|
32
|
+
- MIT-LICENSE
|
33
|
+
- README.rdoc
|
34
|
+
- Rakefile
|
35
|
+
- init.rb
|
36
|
+
- lib/responders_backport.rb
|
37
|
+
- lib/responders_backport/respond_to.rb
|
38
|
+
- lib/responders_backport/responder.rb
|
39
|
+
- lib/responders_backport/version.rb
|
40
|
+
has_rdoc: true
|
41
|
+
homepage: http://github.com/plataformatec/responders_backport
|
42
|
+
licenses: []
|
43
|
+
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options:
|
46
|
+
- --charset=UTF-8
|
47
|
+
require_paths:
|
48
|
+
- lib
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
segments:
|
54
|
+
- 0
|
55
|
+
version: "0"
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
segments:
|
61
|
+
- 0
|
62
|
+
version: "0"
|
63
|
+
requirements: []
|
64
|
+
|
65
|
+
rubyforge_project: responders_backport
|
66
|
+
rubygems_version: 1.3.6
|
67
|
+
signing_key:
|
68
|
+
specification_version: 3
|
69
|
+
summary: Bringing Rails 3 Responders to your obsolete Rails 2.3.x apps =)
|
70
|
+
test_files:
|
71
|
+
- test/respond_to_test.rb
|
72
|
+
- test/test_helper.rb
|