responders 2.1.2 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8dce735a1e17c51056e99b880daafe35f666771f
4
- data.tar.gz: 61548c0e60e01fa193a5b01ce93f21b1e9c5181f
3
+ metadata.gz: 7ba43cee72d6a197eff78818683a01dfe43814d3
4
+ data.tar.gz: 37ceb10a23ee46f1ec142176eee690c33a98b73d
5
5
  SHA512:
6
- metadata.gz: 1c5e9f72023667804fbb1589dc4512db20e10a4cca06c643fdc85475c713fd16b6cef65936c3bbc1bbe7beba1460277f32b2f82973ab5f2080acce9ae9772260
7
- data.tar.gz: 8bc67ddcb91fb1834483cd99e53f3cfd397b3639b9c0b022996ad2e77d06dcb501cab89a8f586c0f0fc224530ed952fecce88bf9680a9c62d7808e0492420a9a
6
+ metadata.gz: e198e8715fefd1e87c21d7a52ed255ad51445bdfe2eb83e0e8a5eb8845eae8d3b1e665217ce1ac6f836156abd1d56cff5a2082f48cfb6f44e3cabe875117125f
7
+ data.tar.gz: c59cbdaf34207e264e7551bdd700f2d9afe200fa29c2a31672d79debce0979853c76ee8ce0500347f5ee8fb19ebf8b86fc270a45add262d06b4d9c43c7c7ae17
@@ -1,3 +1,10 @@
1
+ ## 2.2.0
2
+
3
+ * Added the `verify_request_format!` method, that can be used as a `before_action`
4
+ callback to prevent your actions from being invoked when the controller does
5
+ not respond to the request mime type, preventing the execution of complex
6
+ queries or creating/deleting records from your app.
7
+
1
8
  ## 2.1.2
2
9
 
3
10
  * Fix rendering when using `ActionController::API`. (by @eLod)
data/README.md CHANGED
@@ -17,7 +17,7 @@ Update your bundle and run the install generator:
17
17
  $ bundle install
18
18
  $ rails g responders:install
19
19
 
20
- If you are including this gem to support backwards compatibilty for responders in previous releases of Rails, you only need to include the gem and bundle.
20
+ If you are including this gem to support backwards compatibilty for responders in previous releases of Rails, you only need to include the gem and bundle.
21
21
 
22
22
  $ bundle install
23
23
 
@@ -188,7 +188,7 @@ to use `respond_with` instead of default `respond_to` blocks. From 2.1, you need
188
188
 
189
189
  config.app_generators.scaffold_controller :responders_controller
190
190
 
191
- #Failure handling
191
+ ## Failure handling
192
192
 
193
193
  Responders don't use `valid?` to check for errors in models to figure out if
194
194
  the request was successfull or not, and relies on your controllers to call
@@ -216,6 +216,29 @@ def create
216
216
  end
217
217
  ```
218
218
 
219
+ ## Verifying request formats
220
+
221
+ `respond_with` will raise an `ActionController::UnknownFormat` if the request
222
+ mime type was not configured through the class level `respond_to`, but the
223
+ action will still be executed and any side effects (like creating a new record)
224
+ will still occur. To raise the `UnknownFormat` exception before your action
225
+ is invoked you can set the `verify_request_format!` method as a `before_action`
226
+ on your controller.
227
+
228
+ ```ruby
229
+ class WidgetsController < ApplicationController
230
+ respond_to :json
231
+ before_action :verify_request_format!
232
+
233
+ # POST /widgets.html won't reach the `create` action.
234
+ def create
235
+ widget = Widget.create(widget_params)
236
+ respond_with widget
237
+ end
238
+ end
239
+
240
+ ```
241
+
219
242
  ## Examples
220
243
 
221
244
  Want more examples ? Check out this blog posts:
@@ -40,14 +40,14 @@ module ActionController #:nodoc:
40
40
  only_actions = Array(options.delete(:only)).map(&:to_s)
41
41
  except_actions = Array(options.delete(:except)).map(&:to_s)
42
42
 
43
- new = mimes_for_respond_to.dup
43
+ hash = mimes_for_respond_to.dup
44
44
  mimes.each do |mime|
45
45
  mime = mime.to_sym
46
- new[mime] = {}
47
- new[mime][:only] = only_actions unless only_actions.empty?
48
- new[mime][:except] = except_actions unless except_actions.empty?
46
+ hash[mime] = {}
47
+ hash[mime][:only] = only_actions unless only_actions.empty?
48
+ hash[mime][:except] = except_actions unless except_actions.empty?
49
49
  end
50
- self.mimes_for_respond_to = new.freeze
50
+ self.mimes_for_respond_to = hash.freeze
51
51
  end
52
52
 
53
53
  # Clear all mime types in <tt>respond_to</tt>.
@@ -193,7 +193,7 @@ module ActionController #:nodoc:
193
193
  "formats your controller responds to in the class level."
194
194
  end
195
195
 
196
- mimes = collect_mimes_from_class_level()
196
+ mimes = collect_mimes_from_class_level
197
197
  collector = ActionController::MimeResponds::Collector.new(mimes, request.variant)
198
198
  block.call(collector) if block_given?
199
199
 
@@ -208,7 +208,24 @@ module ActionController #:nodoc:
208
208
  end
209
209
  end
210
210
 
211
- protected
211
+ protected
212
+
213
+ # Before action callback that can be used to prevent requests that do not
214
+ # match the mime types defined through <tt>respond_to</tt> from being executed.
215
+ #
216
+ # class PeopleController < ApplicationController
217
+ # respond_to :html, :xml, :json
218
+ #
219
+ # before_action :verify_request_format!
220
+ # end
221
+ def verify_request_format!
222
+ mimes = collect_mimes_from_class_level
223
+ collector = ActionController::MimeResponds::Collector.new(mimes, request.variant)
224
+
225
+ unless collector.negotiate_format(request)
226
+ raise ActionController::UnknownFormat
227
+ end
228
+ end
212
229
 
213
230
  # Collect mimes declared in the class method respond_to valid for the
214
231
  # current action.
@@ -1,3 +1,3 @@
1
1
  module Responders
2
- VERSION = "2.1.2".freeze
2
+ VERSION = "2.2.0".freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: responders
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.2
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - José Valim
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-03-21 00:00:00.000000000 Z
11
+ date: 2016-04-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties