josevalim-inherited_resources 0.8.5 → 0.9.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/test/test_helper.rb CHANGED
@@ -1,6 +1,9 @@
1
1
  require 'test/unit'
2
2
  require 'rubygems'
3
- require 'ruby-debug'
3
+ begin
4
+ require 'ruby-debug'
5
+ rescue LoadError
6
+ end
4
7
  require 'mocha'
5
8
 
6
9
  ENV["RAILS_ENV"] = "test"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: josevalim-inherited_resources
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.5
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - "Jos\xC3\xA9 Valim"
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-07-24 00:00:00 -07:00
12
+ date: 2009-08-25 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -35,8 +35,9 @@ files:
35
35
  - lib/inherited_resources/class_methods.rb
36
36
  - lib/inherited_resources/dumb_responder.rb
37
37
  - lib/inherited_resources/has_scope_helpers.rb
38
+ - lib/inherited_resources/legacy/respond_to.rb
39
+ - lib/inherited_resources/legacy/responder.rb
38
40
  - lib/inherited_resources/polymorphic_helpers.rb
39
- - lib/inherited_resources/respond_to.rb
40
41
  - lib/inherited_resources/singleton_helpers.rb
41
42
  - lib/inherited_resources/url_helpers.rb
42
43
  has_rdoc: true
@@ -1,339 +0,0 @@
1
- module ActionController
2
- # Provides an extension for Rails respond_to by expading MimeResponds::Responder
3
- # and adding respond_to class method and respond_with instance method.
4
- #
5
- class Base
6
-
7
- protected
8
- # Defines respond_to method to store formats that are rendered by default.
9
- #
10
- # Examples:
11
- #
12
- # respond_to :html, :xml, :json
13
- #
14
- # All actions on your controller will respond to :html, :xml and :json.
15
- # But if you want to specify it based on your actions, you can use only and
16
- # except:
17
- #
18
- # respond_to :html
19
- # respond_to :xml, :json, :except => [ :edit ]
20
- #
21
- # The definition above explicits that all actions respond to :html. And all
22
- # actions except :edit respond to :xml and :json.
23
- #
24
- # You can specify also only parameters:
25
- #
26
- # respond_to :rjs, :only => :create
27
- #
28
- # Which would be the same as:
29
- #
30
- # respond_to :rjs => :create
31
- #
32
- def self.respond_to(*formats)
33
- options = formats.extract_options!
34
- formats_hash = {}
35
-
36
- only_actions = Array(options.delete(:only))
37
- except_actions = Array(options.delete(:except))
38
-
39
- only_actions.map!{ |a| a.to_sym }
40
- except_actions.map!{ |a| a.to_sym }
41
-
42
- formats.each do |format|
43
- formats_hash[format.to_sym] = {}
44
- formats_hash[format.to_sym][:only] = only_actions unless only_actions.empty?
45
- formats_hash[format.to_sym][:except] = except_actions unless except_actions.empty?
46
- end
47
-
48
- options.each do |format, actions|
49
- formats_hash[format.to_sym] = {}
50
- next if actions == :all || actions == 'all'
51
-
52
- actions = Array(actions)
53
- actions.map!{ |a| a.to_sym }
54
-
55
- formats_hash[format.to_sym][:only] = actions unless actions.empty?
56
- end
57
-
58
- write_inheritable_hash(:formats_for_respond_to, formats_hash)
59
- end
60
- class_inheritable_reader :formats_for_respond_to
61
-
62
- # Method to clear all respond_to declared until the current controller.
63
- # This is like freeing the controller from the inheritance chain. :)
64
- #
65
- def self.clear_respond_to!
66
- formats = formats_for_respond_to
67
- formats.each { |k,v| formats[k] = { :only => [] } }
68
- write_inheritable_hash(:formats_for_respond_to, formats)
69
- end
70
-
71
- # By default, responds only to :html
72
- respond_to :html
73
-
74
- # If ApplicationController is already defined around here, we recriate
75
- # the formats_for_respond_to hash. Since we respond only to :html by
76
- # default, this is as easy as settings the :formats_for_respond_to key
77
- # to {:html=>{}}.
78
- #
79
- if defined?(ApplicationController)
80
- if inheritable = ApplicationController.instance_variable_get("@inheritable_attributes")
81
- inheritable.merge!(:formats_for_respond_to => {:html => {}}) if inheritable
82
- end
83
- end
84
-
85
- # respond_with accepts an object and tries to render a view based in the
86
- # controller and actions that called respond_with. If the view cannot be
87
- # found, it will try to call :to_format in the object.
88
- #
89
- # class ProjectsController < ApplicationController
90
- # respond_to :html, :xml
91
- #
92
- # def show
93
- # @project = Project.find(:id)
94
- # respond_with(@project)
95
- # end
96
- # end
97
- #
98
- # When the client request a xml, we will check first for projects/show.xml
99
- # if it can't be found, we will call :to_xml in the object @project. If the
100
- # object eventually doesn't respond to :to_xml it will render 404.
101
- #
102
- # If you want to overwrite the formats specified in the class, you can
103
- # send your new formats using the options :to.
104
- #
105
- # def show
106
- # @project = Project.find(:id)
107
- # respond_with(@project, :to => :json)
108
- # end
109
- #
110
- # That means that this action will ONLY reply to json requests.
111
- #
112
- # All other options sent will be forwarded to the render method. So you can
113
- # do:
114
- #
115
- # def create
116
- # # ...
117
- # if @project.save
118
- # respond_with(@project, :status => :ok, :location => @project)
119
- # else
120
- # respond_with(@project.errors, :status => :unprocessable_entity)
121
- # end
122
- # end
123
- #
124
- # respond_with does not accept blocks, if you want advanced configurations
125
- # check respond_to method sending :with => @object as option.
126
- #
127
- # Returns true if anything is rendered. Returns false otherwise.
128
- #
129
- def respond_with(object, options = {})
130
- attempt_to_respond = false
131
-
132
- responder = options.delete(:responder) || Responder.new(self)
133
- skip_not_acceptable = options.delete(:skip_not_acceptable)
134
- skip_default_template = options.delete(:skip_default_template)
135
-
136
- mime_types = Array(options.delete(:to))
137
- mime_types.map!{ |mime| mime.to_sym }
138
-
139
- for priority in responder.mime_type_priority
140
- if !skip_default_template && priority == Mime::ALL && respond_to_default_template?(responder)
141
- render options.merge(:action => action_name)
142
- return true
143
-
144
- elsif responder.action_respond_to_format?(priority.to_sym, mime_types)
145
- attempt_to_respond = true
146
- response.template.template_format = priority.to_sym
147
- response.content_type = priority.to_s
148
-
149
- if template_exists?
150
- render options.merge(:action => action_name)
151
- return true
152
- elsif object.respond_to?(:"to_#{priority.to_sym}")
153
- render options.merge(:text => object.send(:"to_#{priority.to_sym}"))
154
- return true
155
- end
156
- end
157
- end
158
-
159
- # If we got here we could not render the object. But if attempted to
160
- # render (this means, the format sent by the client was valid) we should
161
- # render a 404.
162
- #
163
- # If we even didn't attempt to respond, we respond :not_acceptable
164
- # unless is told otherwise.
165
- #
166
- if attempt_to_respond
167
- render :text => '404 Not Found', :status => 404
168
- return true
169
- elsif !skip_not_acceptable
170
- head :not_acceptable
171
- return false
172
- end
173
-
174
- return false
175
- end
176
-
177
- # Extends respond_to behaviour.
178
- #
179
- # You can now pass objects using the options :with.
180
- #
181
- # respond_to(:html, :xml, :rjs, :with => @project)
182
- #
183
- # If you pass an object and send any block, it's exactly the same as:
184
- #
185
- # respond_with(@project, :to => [:html, :xml, :rjs])
186
- #
187
- # But the main difference of respond_to and respond_with is that the first
188
- # allows further customizations:
189
- #
190
- # respond_to(:html, :with => @project) do |format|
191
- # format.xml { render :xml => @project.errors }
192
- # end
193
- #
194
- # It's the same as:
195
- #
196
- # 1. When responding to html, execute respond_with(@object).
197
- # 2. When accessing a xml, execute the block given.
198
- #
199
- # Formats defined in blocks have precedence to formats sent as arguments.
200
- # In other words, if you pass a format as argument and as block, the block
201
- # will always be executed.
202
- #
203
- # And as in respond_with, all extra options sent will be forwarded to
204
- # the render method:
205
- #
206
- # respond_to(:with => @projects.errors, :status => :unprocessable_entity) do |format|
207
- # format.html { render :template => 'new' }
208
- # end
209
- #
210
- # It also accepts an option called prioritize. It allows you to put a
211
- # format as first, and then when Mime::ALL is sent, it will be the one
212
- # used as response.
213
- #
214
- def respond_to(*types, &block)
215
- options = types.extract_options!
216
-
217
- object = options.delete(:with)
218
- responder = options.delete(:responder) || Responder.new(self)
219
- prioritize = options.delete(:prioritize)
220
-
221
- if object.nil?
222
- block ||= lambda { |responder| types.each { |type| responder.send(type) } }
223
- block.call(responder)
224
- responder.respond
225
- return true
226
- else
227
- # Even if Mime::ALL is sent by the client, we do not respond_to it now.
228
- # This is done using calling :respond_except_any instead of :respond.
229
- #
230
- if block_given?
231
- block.call(responder)
232
- return true if responder.respond_except_any
233
- end
234
-
235
- # If the block includes the default template format, we don't render
236
- # the default template (which uses the default_template_format).
237
- options.merge!(:to => types, :responder => responder, :skip_not_acceptable => true,
238
- :skip_default_template => responder.order.include?(default_template_format))
239
-
240
- if respond_with(object, options)
241
- return true
242
- elsif block_given?
243
- responder.prioritize(prioritize) if prioritize
244
- return true if responder.respond_any
245
- end
246
- end
247
-
248
- head :not_acceptable
249
- return false
250
- end
251
-
252
- private
253
-
254
- unless ActionController::Base.private_instance_methods.include?('template_exists?') ||
255
- ActionController::Base.private_instance_methods.include?(:template_exists?)
256
-
257
- # Define template_exists? for Rails 2.3
258
- def template_exists?
259
- default_template ? true : false
260
- rescue ActionView::MissingTemplate
261
- false
262
- end
263
- end
264
-
265
- # We respond to the default template if it's a valid format AND the template
266
- # exists.
267
- #
268
- def respond_to_default_template?(responder) #:nodoc:
269
- responder.action_respond_to_format?(default_template_format) && template_exists?
270
- end
271
-
272
- end
273
-
274
- module MimeResponds #:nodoc:
275
- class Responder #:nodoc:
276
-
277
- attr_reader :mime_type_priority, :order
278
-
279
- # Similar as respond but if we can't find a valid mime type, we do not
280
- # send :not_acceptable message as head and it does not respond to
281
- # Mime::ALL in any case.
282
- #
283
- def respond_except_any
284
- for priority in @mime_type_priority
285
- next if priority == Mime::ALL
286
-
287
- if @responses[priority]
288
- @responses[priority].call
289
- return true
290
- end
291
- end
292
-
293
- false
294
- end
295
-
296
- # Respond to the first format given if Mime::ALL is included in the
297
- # mime type priorites. This is the behaviour expected when the client
298
- # sends "*/*" as mime type.
299
- #
300
- def respond_any
301
- any = @responses[@order.include?(Mime::ALL) ? Mime::ALL : @order.first]
302
-
303
- if any && @mime_type_priority.include?(Mime::ALL)
304
- any.call
305
- return true
306
- end
307
- end
308
-
309
- # Receives an format and checks if the current action responds to
310
- # the given format. If additional mimes are sent, only them are checked.
311
- #
312
- def action_respond_to_format?(format, additional_mimes = [])
313
- if !additional_mimes.blank?
314
- additional_mimes.include?(format.to_sym)
315
- elsif formats = @controller.formats_for_respond_to[format.to_sym]
316
- if formats[:only]
317
- formats[:only].include?(@controller.action_name.to_sym)
318
- elsif formats[:except]
319
- !formats[:except].include?(@controller.action_name.to_sym)
320
- else
321
- true
322
- end
323
- else
324
- false
325
- end
326
- end
327
-
328
- # Makes a given format the first in the @order array.
329
- #
330
- def prioritize(format)
331
- if index = @order.index(format)
332
- @order.unshift(@order.delete_at(index))
333
- end
334
- @order
335
- end
336
-
337
- end
338
- end
339
- end