respond_with_backport 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/MIT-LICENSE +20 -0
- data/README +17 -0
- data/Rakefile +53 -0
- data/VERSION +1 -0
- data/init.rb +5 -0
- data/install.rb +1 -0
- data/lib/responds_to_backport.rb +588 -0
- data/tasks/responds_to_backport_tasks.rake +4 -0
- data/test/responds_to_backport_test.rb +8 -0
- data/test/test_helper.rb +3 -0
- data/uninstall.rb +1 -0
- metadata +66 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 [name of plugin creator]
|
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
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
= respond_with_backport
|
2
|
+
|
3
|
+
Description goes here.
|
4
|
+
|
5
|
+
== Note on Patches/Pull Requests
|
6
|
+
|
7
|
+
* Fork the project.
|
8
|
+
* Make your feature addition or bug fix.
|
9
|
+
* Add tests for it. This is important so I don't break it in a
|
10
|
+
future version unintentionally.
|
11
|
+
* Commit, do not mess with rakefile, version, or history.
|
12
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
13
|
+
* Send me a pull request. Bonus points for topic branches.
|
14
|
+
|
15
|
+
== Copyright
|
16
|
+
|
17
|
+
See MIT-LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "respond_with_backport"
|
8
|
+
gem.summary = %Q{Backport of Rails 3 respond_with functionality}
|
9
|
+
gem.description = %Q{Backport of Rails 3 respond_with functionality}
|
10
|
+
gem.email = "tom@smallroomsoftware.com"
|
11
|
+
gem.homepage = "http://github.com/tomriley/respond_with_backport"
|
12
|
+
gem.authors = ["Tom Riley"]
|
13
|
+
# gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
|
14
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
|
+
end
|
16
|
+
Jeweler::GemcutterTasks.new
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'rake/testtask'
|
22
|
+
Rake::TestTask.new(:test) do |test|
|
23
|
+
test.libs << 'lib' << 'test'
|
24
|
+
test.pattern = 'test/**/test_*.rb'
|
25
|
+
test.verbose = true
|
26
|
+
end
|
27
|
+
|
28
|
+
begin
|
29
|
+
require 'rcov/rcovtask'
|
30
|
+
Rcov::RcovTask.new do |test|
|
31
|
+
test.libs << 'test'
|
32
|
+
test.pattern = 'test/**/test_*.rb'
|
33
|
+
test.verbose = true
|
34
|
+
end
|
35
|
+
rescue LoadError
|
36
|
+
task :rcov do
|
37
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
task :test => :check_dependencies
|
42
|
+
|
43
|
+
task :default => :test
|
44
|
+
|
45
|
+
require 'rake/rdoctask'
|
46
|
+
Rake::RDocTask.new do |rdoc|
|
47
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
48
|
+
|
49
|
+
rdoc.rdoc_dir = 'rdoc'
|
50
|
+
rdoc.title = "respond_with_backport #{version}"
|
51
|
+
rdoc.rdoc_files.include('README*')
|
52
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
53
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/init.rb
ADDED
data/install.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Install hook code here
|
@@ -0,0 +1,588 @@
|
|
1
|
+
module ActionController #:nodoc:
|
2
|
+
|
3
|
+
|
4
|
+
class Request
|
5
|
+
def negotiate_mime(order)
|
6
|
+
formats.each do |priority|
|
7
|
+
if priority == Mime::ALL
|
8
|
+
return order.first
|
9
|
+
elsif order.include?(priority)
|
10
|
+
return priority
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
order.include?(Mime::ALL) ? formats.first : nil
|
15
|
+
end
|
16
|
+
|
17
|
+
def format(view_path = [])
|
18
|
+
formats.first
|
19
|
+
end
|
20
|
+
|
21
|
+
def formats
|
22
|
+
accept = @env['HTTP_ACCEPT']
|
23
|
+
|
24
|
+
@env["action_dispatch.request.formats"] ||=
|
25
|
+
if parameters[:format]
|
26
|
+
Array.wrap(Mime::Type.lookup(parameters[:format]))
|
27
|
+
elsif xhr? || (accept && !accept.include?(?,))
|
28
|
+
accepts
|
29
|
+
else
|
30
|
+
[Mime::HTML]
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def format=(extension)
|
35
|
+
parameters[:format] = extension.to_s
|
36
|
+
@env["action_dispatch.request.formats"] = [Mime::Type.lookup_by_extension(parameters[:format])]
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
# Responder is responsible to expose a resource for different mime requests,
|
42
|
+
# usually depending on the HTTP verb. The responder is triggered when
|
43
|
+
# respond_with is called. The simplest case to study is a GET request:
|
44
|
+
#
|
45
|
+
# class PeopleController < ApplicationController
|
46
|
+
# respond_to :html, :xml, :json
|
47
|
+
#
|
48
|
+
# def index
|
49
|
+
# @people = Person.find(:all)
|
50
|
+
# respond_with(@people)
|
51
|
+
# end
|
52
|
+
# end
|
53
|
+
#
|
54
|
+
# When a request comes, for example with format :xml, three steps happen:
|
55
|
+
#
|
56
|
+
# 1) responder searches for a template at people/index.xml;
|
57
|
+
#
|
58
|
+
# 2) if the template is not available, it will invoke :to_xml in the given resource;
|
59
|
+
#
|
60
|
+
# 3) if the responder does not respond_to :to_xml, call :to_format on it.
|
61
|
+
#
|
62
|
+
# === Builtin HTTP verb semantics
|
63
|
+
#
|
64
|
+
# Rails default responder holds semantics for each HTTP verb. Depending on the
|
65
|
+
# content type, verb and the resource status, it will behave differently.
|
66
|
+
#
|
67
|
+
# Using Rails default responder, a POST request for creating an object could
|
68
|
+
# be written as:
|
69
|
+
#
|
70
|
+
# def create
|
71
|
+
# @user = User.new(params[:user])
|
72
|
+
# flash[:notice] = 'User was successfully created.' if @user.save
|
73
|
+
# respond_with(@user)
|
74
|
+
# end
|
75
|
+
#
|
76
|
+
# Which is exactly the same as:
|
77
|
+
#
|
78
|
+
# def create
|
79
|
+
# @user = User.new(params[:user])
|
80
|
+
#
|
81
|
+
# respond_to do |format|
|
82
|
+
# if @user.save
|
83
|
+
# flash[:notice] = 'User was successfully created.'
|
84
|
+
# format.html { redirect_to(@user) }
|
85
|
+
# format.xml { render :xml => @user, :status => :created, :location => @user }
|
86
|
+
# else
|
87
|
+
# format.html { render :action => "new" }
|
88
|
+
# format.xml { render :xml => @user.errors, :status => :unprocessable_entity }
|
89
|
+
# end
|
90
|
+
# end
|
91
|
+
# end
|
92
|
+
#
|
93
|
+
# The same happens for PUT and DELETE requests.
|
94
|
+
#
|
95
|
+
# === Nested resources
|
96
|
+
#
|
97
|
+
# You can given nested resource as you do in form_for and polymorphic_url.
|
98
|
+
# Consider the project has many tasks example. The create action for
|
99
|
+
# TasksController would be like:
|
100
|
+
#
|
101
|
+
# def create
|
102
|
+
# @project = Project.find(params[:project_id])
|
103
|
+
# @task = @project.comments.build(params[:task])
|
104
|
+
# flash[:notice] = 'Task was successfully created.' if @task.save
|
105
|
+
# respond_with(@project, @task)
|
106
|
+
# end
|
107
|
+
#
|
108
|
+
# Giving an array of resources, you ensure that the responder will redirect to
|
109
|
+
# project_task_url instead of task_url.
|
110
|
+
#
|
111
|
+
# Namespaced and singleton resources requires a symbol to be given, as in
|
112
|
+
# polymorphic urls. If a project has one manager which has many tasks, it
|
113
|
+
# should be invoked as:
|
114
|
+
#
|
115
|
+
# respond_with(@project, :manager, @task)
|
116
|
+
#
|
117
|
+
# Check polymorphic_url documentation for more examples.
|
118
|
+
#
|
119
|
+
class Responder
|
120
|
+
attr_reader :controller, :request, :format, :resource, :resources, :options
|
121
|
+
|
122
|
+
ACTIONS_FOR_VERBS = {
|
123
|
+
:post => :new,
|
124
|
+
:put => :edit
|
125
|
+
}
|
126
|
+
|
127
|
+
def initialize(controller, resources, options={})
|
128
|
+
@controller = controller
|
129
|
+
@request = controller.request
|
130
|
+
@format = controller.formats.first
|
131
|
+
@resource = resources.is_a?(Array) ? resources.last : resources
|
132
|
+
@resources = resources
|
133
|
+
@options = options
|
134
|
+
@action = options.delete(:action)
|
135
|
+
@default_response = options.delete(:default_response)
|
136
|
+
end
|
137
|
+
|
138
|
+
delegate :head, :render, :redirect_to, :to => :controller
|
139
|
+
delegate :get?, :post?, :put?, :delete?, :to => :request
|
140
|
+
|
141
|
+
# Undefine :to_json and :to_yaml since it's defined on Object
|
142
|
+
undef_method(:to_json) if method_defined?(:to_json)
|
143
|
+
undef_method(:to_yaml) if method_defined?(:to_yaml)
|
144
|
+
|
145
|
+
# Initializes a new responder an invoke the proper format. If the format is
|
146
|
+
# not defined, call to_format.
|
147
|
+
#
|
148
|
+
def self.call(*args)
|
149
|
+
new(*args).respond
|
150
|
+
end
|
151
|
+
|
152
|
+
# Main entry point for responder responsible to dispatch to the proper format.
|
153
|
+
#
|
154
|
+
def respond
|
155
|
+
method = :"to_#{format}"
|
156
|
+
respond_to?(method) ? send(method) : to_format
|
157
|
+
end
|
158
|
+
|
159
|
+
# HTML format does not render the resource, it always attempt to render a
|
160
|
+
# template.
|
161
|
+
#
|
162
|
+
def to_html
|
163
|
+
default_render
|
164
|
+
rescue ActionView::MissingTemplate => e
|
165
|
+
navigation_behavior(e)
|
166
|
+
end
|
167
|
+
|
168
|
+
# All others formats follow the procedure below. First we try to render a
|
169
|
+
# template, if the template is not available, we verify if the resource
|
170
|
+
# responds to :to_format and display it.
|
171
|
+
#
|
172
|
+
def to_format
|
173
|
+
default_render
|
174
|
+
rescue ActionView::MissingTemplate => e
|
175
|
+
raise unless resourceful?
|
176
|
+
api_behavior(e)
|
177
|
+
end
|
178
|
+
|
179
|
+
protected
|
180
|
+
|
181
|
+
# This is the common behavior for "navigation" requests, like :html, :iphone and so forth.
|
182
|
+
def navigation_behavior(error)
|
183
|
+
if get?
|
184
|
+
raise error
|
185
|
+
elsif has_errors? && default_action
|
186
|
+
render :action => default_action
|
187
|
+
else
|
188
|
+
redirect_to resource_location
|
189
|
+
end
|
190
|
+
end
|
191
|
+
|
192
|
+
# This is the common behavior for "API" requests, like :xml and :json.
|
193
|
+
def api_behavior(error)
|
194
|
+
if get?
|
195
|
+
display resource
|
196
|
+
elsif has_errors?
|
197
|
+
display resource.errors, :status => :unprocessable_entity
|
198
|
+
elsif post?
|
199
|
+
display resource, :status => :created, :location => resource_location
|
200
|
+
else
|
201
|
+
head :ok
|
202
|
+
end
|
203
|
+
end
|
204
|
+
|
205
|
+
# Checks whether the resource responds to the current format or not.
|
206
|
+
#
|
207
|
+
def resourceful?
|
208
|
+
resource.respond_to?(:"to_#{format}")
|
209
|
+
end
|
210
|
+
|
211
|
+
# Returns the resource location by retrieving it from the options or
|
212
|
+
# returning the resources array.
|
213
|
+
#
|
214
|
+
def resource_location
|
215
|
+
options[:location] || resources
|
216
|
+
end
|
217
|
+
|
218
|
+
# If a given response block was given, use it, otherwise call render on
|
219
|
+
# controller.
|
220
|
+
#
|
221
|
+
def default_render
|
222
|
+
@default_response.call
|
223
|
+
end
|
224
|
+
|
225
|
+
# display is just a shortcut to render a resource with the current format.
|
226
|
+
#
|
227
|
+
# display @user, :status => :ok
|
228
|
+
#
|
229
|
+
# For xml request is equivalent to:
|
230
|
+
#
|
231
|
+
# render :xml => @user, :status => :ok
|
232
|
+
#
|
233
|
+
# Options sent by the user are also used:
|
234
|
+
#
|
235
|
+
# respond_with(@user, :status => :created)
|
236
|
+
# display(@user, :status => :ok)
|
237
|
+
#
|
238
|
+
# Results in:
|
239
|
+
#
|
240
|
+
# render :xml => @user, :status => :created
|
241
|
+
#
|
242
|
+
def display(resource, given_options={})
|
243
|
+
controller.render given_options.merge!(options).merge!(format => resource)
|
244
|
+
end
|
245
|
+
|
246
|
+
# Check if the resource has errors or not.
|
247
|
+
#
|
248
|
+
def has_errors?
|
249
|
+
resource.respond_to?(:errors) && !resource.errors.empty?
|
250
|
+
end
|
251
|
+
|
252
|
+
# By default, render the :edit action for html requests with failure, unless
|
253
|
+
# the verb is post.
|
254
|
+
#
|
255
|
+
def default_action
|
256
|
+
@action ||= ACTIONS_FOR_VERBS[request.method]
|
257
|
+
end
|
258
|
+
end
|
259
|
+
end
|
260
|
+
|
261
|
+
|
262
|
+
# RespondsToBackport
|
263
|
+
module ActionController #:nodoc:
|
264
|
+
module MimeResponds #:nodoc:
|
265
|
+
#extend ActiveSupport::Concern
|
266
|
+
|
267
|
+
def self.included(base)
|
268
|
+
base.extend(ClassMethods)
|
269
|
+
#instance_eval do
|
270
|
+
# write_inheritable_attribute(:responder, ActionController::Responder)
|
271
|
+
#end
|
272
|
+
base.class_eval do
|
273
|
+
class_inheritable_accessor :responder
|
274
|
+
class_inheritable_accessor :mimes_for_respond_to#, :instance_writer => false
|
275
|
+
attr_accessor :formats
|
276
|
+
self.responder = ActionController::Responder
|
277
|
+
#clear_respond_to
|
278
|
+
end
|
279
|
+
base.clear_respond_to
|
280
|
+
end
|
281
|
+
|
282
|
+
module ClassMethods
|
283
|
+
# Defines mimes that are rendered by default when invoking respond_with.
|
284
|
+
#
|
285
|
+
# Examples:
|
286
|
+
#
|
287
|
+
# respond_to :html, :xml, :json
|
288
|
+
#
|
289
|
+
# All actions on your controller will respond to :html, :xml and :json.
|
290
|
+
#
|
291
|
+
# But if you want to specify it based on your actions, you can use only and
|
292
|
+
# except:
|
293
|
+
#
|
294
|
+
# respond_to :html
|
295
|
+
# respond_to :xml, :json, :except => [ :edit ]
|
296
|
+
#
|
297
|
+
# The definition above explicits that all actions respond to :html. And all
|
298
|
+
# actions except :edit respond to :xml and :json.
|
299
|
+
#
|
300
|
+
# You can specify also only parameters:
|
301
|
+
#
|
302
|
+
# respond_to :rjs, :only => :create
|
303
|
+
#
|
304
|
+
def respond_to(*mimes)
|
305
|
+
options = mimes.extract_options!
|
306
|
+
|
307
|
+
only_actions = Array(options.delete(:only))
|
308
|
+
except_actions = Array(options.delete(:except))
|
309
|
+
|
310
|
+
mimes.each do |mime|
|
311
|
+
mime = mime.to_sym
|
312
|
+
mimes_for_respond_to[mime] = {}
|
313
|
+
mimes_for_respond_to[mime][:only] = only_actions unless only_actions.empty?
|
314
|
+
mimes_for_respond_to[mime][:except] = except_actions unless except_actions.empty?
|
315
|
+
end
|
316
|
+
end
|
317
|
+
|
318
|
+
# Clear all mimes in respond_to.
|
319
|
+
#
|
320
|
+
def clear_respond_to
|
321
|
+
self.mimes_for_respond_to = ActiveSupport::OrderedHash.new
|
322
|
+
end
|
323
|
+
end
|
324
|
+
|
325
|
+
# Without web-service support, an action which collects the data for displaying a list of people
|
326
|
+
# might look something like this:
|
327
|
+
#
|
328
|
+
# def index
|
329
|
+
# @people = Person.find(:all)
|
330
|
+
# end
|
331
|
+
#
|
332
|
+
# Here's the same action, with web-service support baked in:
|
333
|
+
#
|
334
|
+
# def index
|
335
|
+
# @people = Person.find(:all)
|
336
|
+
#
|
337
|
+
# respond_to do |format|
|
338
|
+
# format.html
|
339
|
+
# format.xml { render :xml => @people.to_xml }
|
340
|
+
# end
|
341
|
+
# end
|
342
|
+
#
|
343
|
+
# What that says is, "if the client wants HTML in response to this action, just respond as we
|
344
|
+
# would have before, but if the client wants XML, return them the list of people in XML format."
|
345
|
+
# (Rails determines the desired response format from the HTTP Accept header submitted by the client.)
|
346
|
+
#
|
347
|
+
# Supposing you have an action that adds a new person, optionally creating their company
|
348
|
+
# (by name) if it does not already exist, without web-services, it might look like this:
|
349
|
+
#
|
350
|
+
# def create
|
351
|
+
# @company = Company.find_or_create_by_name(params[:company][:name])
|
352
|
+
# @person = @company.people.create(params[:person])
|
353
|
+
#
|
354
|
+
# redirect_to(person_list_url)
|
355
|
+
# end
|
356
|
+
#
|
357
|
+
# Here's the same action, with web-service support baked in:
|
358
|
+
#
|
359
|
+
# def create
|
360
|
+
# company = params[:person].delete(:company)
|
361
|
+
# @company = Company.find_or_create_by_name(company[:name])
|
362
|
+
# @person = @company.people.create(params[:person])
|
363
|
+
#
|
364
|
+
# respond_to do |format|
|
365
|
+
# format.html { redirect_to(person_list_url) }
|
366
|
+
# format.js
|
367
|
+
# format.xml { render :xml => @person.to_xml(:include => @company) }
|
368
|
+
# end
|
369
|
+
# end
|
370
|
+
#
|
371
|
+
# If the client wants HTML, we just redirect them back to the person list. If they want Javascript
|
372
|
+
# (format.js), then it is an RJS request and we render the RJS template associated with this action.
|
373
|
+
# Lastly, if the client wants XML, we render the created person as XML, but with a twist: we also
|
374
|
+
# include the person's company in the rendered XML, so you get something like this:
|
375
|
+
#
|
376
|
+
# <person>
|
377
|
+
# <id>...</id>
|
378
|
+
# ...
|
379
|
+
# <company>
|
380
|
+
# <id>...</id>
|
381
|
+
# <name>...</name>
|
382
|
+
# ...
|
383
|
+
# </company>
|
384
|
+
# </person>
|
385
|
+
#
|
386
|
+
# Note, however, the extra bit at the top of that action:
|
387
|
+
#
|
388
|
+
# company = params[:person].delete(:company)
|
389
|
+
# @company = Company.find_or_create_by_name(company[:name])
|
390
|
+
#
|
391
|
+
# This is because the incoming XML document (if a web-service request is in process) can only contain a
|
392
|
+
# single root-node. So, we have to rearrange things so that the request looks like this (url-encoded):
|
393
|
+
#
|
394
|
+
# person[name]=...&person[company][name]=...&...
|
395
|
+
#
|
396
|
+
# And, like this (xml-encoded):
|
397
|
+
#
|
398
|
+
# <person>
|
399
|
+
# <name>...</name>
|
400
|
+
# <company>
|
401
|
+
# <name>...</name>
|
402
|
+
# </company>
|
403
|
+
# </person>
|
404
|
+
#
|
405
|
+
# In other words, we make the request so that it operates on a single entity's person. Then, in the action,
|
406
|
+
# we extract the company data from the request, find or create the company, and then create the new person
|
407
|
+
# with the remaining data.
|
408
|
+
#
|
409
|
+
# Note that you can define your own XML parameter parser which would allow you to describe multiple entities
|
410
|
+
# in a single request (i.e., by wrapping them all in a single root node), but if you just go with the flow
|
411
|
+
# and accept Rails' defaults, life will be much easier.
|
412
|
+
#
|
413
|
+
# If you need to use a MIME type which isn't supported by default, you can register your own handlers in
|
414
|
+
# environment.rb as follows.
|
415
|
+
#
|
416
|
+
# Mime::Type.register "image/jpg", :jpg
|
417
|
+
#
|
418
|
+
# Respond to also allows you to specify a common block for different formats by using any:
|
419
|
+
#
|
420
|
+
# def index
|
421
|
+
# @people = Person.find(:all)
|
422
|
+
#
|
423
|
+
# respond_to do |format|
|
424
|
+
# format.html
|
425
|
+
# format.any(:xml, :json) { render request.format.to_sym => @people }
|
426
|
+
# end
|
427
|
+
# end
|
428
|
+
#
|
429
|
+
# In the example above, if the format is xml, it will render:
|
430
|
+
#
|
431
|
+
# render :xml => @people
|
432
|
+
#
|
433
|
+
# Or if the format is json:
|
434
|
+
#
|
435
|
+
# render :json => @people
|
436
|
+
#
|
437
|
+
# Since this is a common pattern, you can use the class method respond_to
|
438
|
+
# with the respond_with method to have the same results:
|
439
|
+
#
|
440
|
+
# class PeopleController < ApplicationController
|
441
|
+
# respond_to :html, :xml, :json
|
442
|
+
#
|
443
|
+
# def index
|
444
|
+
# @people = Person.find(:all)
|
445
|
+
# respond_with(@person)
|
446
|
+
# end
|
447
|
+
# end
|
448
|
+
#
|
449
|
+
# Be sure to check respond_with and respond_to documentation for more examples.
|
450
|
+
#
|
451
|
+
def respond_to(*mimes, &block)
|
452
|
+
raise ArgumentError, "respond_to takes either types or a block, never both" if mimes.any? && block_given?
|
453
|
+
|
454
|
+
if response = retrieve_response_from_mimes(mimes, &block)
|
455
|
+
response.call
|
456
|
+
end
|
457
|
+
end
|
458
|
+
|
459
|
+
# respond_with wraps a resource around a responder for default representation.
|
460
|
+
# First it invokes respond_to, if a response cannot be found (ie. no block
|
461
|
+
# for the request was given and template was not available), it instantiates
|
462
|
+
# an ActionController::Responder with the controller and resource.
|
463
|
+
#
|
464
|
+
# ==== Example
|
465
|
+
#
|
466
|
+
# def index
|
467
|
+
# @users = User.all
|
468
|
+
# respond_with(@users)
|
469
|
+
# end
|
470
|
+
#
|
471
|
+
# It also accepts a block to be given. It's used to overwrite a default
|
472
|
+
# response:
|
473
|
+
#
|
474
|
+
# def destroy
|
475
|
+
# @user = User.find(params[:id])
|
476
|
+
# flash[:notice] = "User was successfully created." if @user.save
|
477
|
+
#
|
478
|
+
# respond_with(@user) do |format|
|
479
|
+
# format.html { render }
|
480
|
+
# end
|
481
|
+
# end
|
482
|
+
#
|
483
|
+
# All options given to respond_with are sent to the underlying responder,
|
484
|
+
# except for the option :responder itself. Since the responder interface
|
485
|
+
# is quite simple (it just needs to respond to call), you can even give
|
486
|
+
# a proc to it.
|
487
|
+
#
|
488
|
+
def respond_with(*resources, &block)
|
489
|
+
if response = retrieve_response_from_mimes([], &block)
|
490
|
+
options = resources.extract_options!
|
491
|
+
options.merge!(:default_response => response)
|
492
|
+
(options.delete(:responder) || responder).call(self, resources, options)
|
493
|
+
end
|
494
|
+
end
|
495
|
+
|
496
|
+
protected
|
497
|
+
|
498
|
+
# Collect mimes declared in the class method respond_to valid for the
|
499
|
+
# current action.
|
500
|
+
#
|
501
|
+
def collect_mimes_from_class_level #:nodoc:
|
502
|
+
action = action_name.to_sym
|
503
|
+
|
504
|
+
mimes_for_respond_to.keys.select do |mime|
|
505
|
+
config = mimes_for_respond_to[mime]
|
506
|
+
|
507
|
+
if config[:except]
|
508
|
+
!config[:except].include?(action)
|
509
|
+
elsif config[:only]
|
510
|
+
config[:only].include?(action)
|
511
|
+
else
|
512
|
+
true
|
513
|
+
end
|
514
|
+
end
|
515
|
+
end
|
516
|
+
|
517
|
+
# Collects mimes and return the response for the negotiated format. Returns
|
518
|
+
# nil if :not_acceptable was sent to the client.
|
519
|
+
#
|
520
|
+
def retrieve_response_from_mimes(mimes, &block)
|
521
|
+
collector = Collector.new { default_render }
|
522
|
+
mimes = collect_mimes_from_class_level if mimes.empty?
|
523
|
+
mimes.each { |mime| collector.send(mime) }
|
524
|
+
block.call(collector) if block_given?
|
525
|
+
|
526
|
+
if format = request.negotiate_mime(collector.order)
|
527
|
+
self.formats = [format.to_sym]
|
528
|
+
collector.response_for(format)
|
529
|
+
else
|
530
|
+
head :not_acceptable
|
531
|
+
nil
|
532
|
+
end
|
533
|
+
end
|
534
|
+
|
535
|
+
class Collector #:nodoc:
|
536
|
+
attr_accessor :order
|
537
|
+
|
538
|
+
def initialize(&block)
|
539
|
+
@order, @responses, @default_response = [], {}, block
|
540
|
+
end
|
541
|
+
|
542
|
+
def any(*args, &block)
|
543
|
+
if args.any?
|
544
|
+
args.each { |type| send(type, &block) }
|
545
|
+
else
|
546
|
+
custom(Mime::ALL, &block)
|
547
|
+
end
|
548
|
+
end
|
549
|
+
alias :all :any
|
550
|
+
|
551
|
+
def custom(mime_type, &block)
|
552
|
+
mime_type = mime_type.is_a?(Mime::Type) ? mime_type : Mime::Type.lookup(mime_type.to_s)
|
553
|
+
@order << mime_type
|
554
|
+
@responses[mime_type] ||= block
|
555
|
+
end
|
556
|
+
|
557
|
+
def response_for(mime)
|
558
|
+
@responses[mime] || @responses[Mime::ALL] || @default_response
|
559
|
+
end
|
560
|
+
|
561
|
+
def self.generate_method_for_mime(mime)
|
562
|
+
sym = mime.is_a?(Symbol) ? mime : mime.to_sym
|
563
|
+
const = sym.to_s.upcase
|
564
|
+
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
565
|
+
def #{sym}(&block) # def html(&block)
|
566
|
+
custom(Mime::#{const}, &block) # custom(Mime::HTML, &block)
|
567
|
+
end # end
|
568
|
+
RUBY
|
569
|
+
end
|
570
|
+
|
571
|
+
Mime::SET.each do |mime|
|
572
|
+
generate_method_for_mime(mime)
|
573
|
+
end
|
574
|
+
|
575
|
+
def method_missing(symbol, &block)
|
576
|
+
mime_constant = Mime.const_get(symbol.to_s.upcase)
|
577
|
+
|
578
|
+
if Mime::SET.include?(mime_constant)
|
579
|
+
self.class.generate_method_for_mime(mime_constant)
|
580
|
+
send(symbol, &block)
|
581
|
+
else
|
582
|
+
super
|
583
|
+
end
|
584
|
+
end
|
585
|
+
|
586
|
+
end
|
587
|
+
end
|
588
|
+
end
|
data/test/test_helper.rb
ADDED
data/uninstall.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Uninstall hook code here
|
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: respond_with_backport
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tom Riley
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-01-02 00:00:00 +00:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Backport of Rails 3 respond_with functionality
|
17
|
+
email: tom@smallroomsoftware.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README
|
24
|
+
files:
|
25
|
+
- MIT-LICENSE
|
26
|
+
- README
|
27
|
+
- Rakefile
|
28
|
+
- VERSION
|
29
|
+
- init.rb
|
30
|
+
- install.rb
|
31
|
+
- lib/responds_to_backport.rb
|
32
|
+
- tasks/responds_to_backport_tasks.rake
|
33
|
+
- test/responds_to_backport_test.rb
|
34
|
+
- test/test_helper.rb
|
35
|
+
- uninstall.rb
|
36
|
+
has_rdoc: true
|
37
|
+
homepage: http://github.com/tomriley/respond_with_backport
|
38
|
+
licenses: []
|
39
|
+
|
40
|
+
post_install_message:
|
41
|
+
rdoc_options:
|
42
|
+
- --charset=UTF-8
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: "0"
|
50
|
+
version:
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: "0"
|
56
|
+
version:
|
57
|
+
requirements: []
|
58
|
+
|
59
|
+
rubyforge_project:
|
60
|
+
rubygems_version: 1.3.5
|
61
|
+
signing_key:
|
62
|
+
specification_version: 3
|
63
|
+
summary: Backport of Rails 3 respond_with functionality
|
64
|
+
test_files:
|
65
|
+
- test/responds_to_backport_test.rb
|
66
|
+
- test/test_helper.rb
|