force_format 0.0.3 → 0.0.4

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: a968ff510d11c69b4429c7f611873c9329e0d3dd
4
- data.tar.gz: e41c0108cdb7b26af2622c7aa1cac7ad144c84c5
3
+ metadata.gz: 28bd300a1fa894d648a065a26018ecf8ac37c3d3
4
+ data.tar.gz: 8eefca9fc7f88b18668829c9e7dcf02ee89c55a5
5
5
  SHA512:
6
- metadata.gz: 885860ffa29446ae06b1ca60119f645777d62b669649129e127e9f8bf761ade6def5c6453670778805c618133d4608c5bfa08258dc4565f6825c3e729afafe5a
7
- data.tar.gz: 1e1f4969071e189f20fa6c325ff96aa900c455cab5dc0c3b53c35a27c3b627ffdf0918c15c4f086bce90506499e2d76ea6fbc5b4b2f12b31460f2b010de8367d
6
+ metadata.gz: 62449330b602903831c8abec45e214ecce5314cec8c5104fdee9e7c90159809aab31ebc1c63c092a36313ee5a1ef4b95fdd9b9b77bd113fd84d27251aaef89e9
7
+ data.tar.gz: 40aaacd7294b881fbce2125039df48e131b5d28ec3d80acfafdd26bdd88a49241ac9378a95d263aafe317887bb684068814a90b4d1346e3ae40b7a24f9d9548a
data/README.md CHANGED
@@ -43,11 +43,32 @@ and ```:unless => ...``` parameters like the Rails filters.
43
43
  def index
44
44
  end
45
45
  end
46
+
47
+ If you want to skip the filter in inherited controllers, use the ```skip_force_format_filter``` method.
48
+ It accepts the same parameters the ```force_format_filter``` methods except ```:for => ...```.
46
49
 
50
+ Maybe you want to define the formats more granular, for example different per action.
51
+ To accomplish this, pass an hash with action names and required formats. Add a *:default* key with formats
52
+ for actions that are not specified directly.
53
+
54
+
55
+ class PagesController < ApplicationController
56
+ force_format_filter :for => {:index => :js, :default => [:json, :html]}
57
+
58
+ def index
59
+ # should respond with js
60
+ end
61
+
62
+ def new
63
+ # should respond with json or html
64
+ end
65
+
66
+ end
67
+
47
68
 
48
69
  By default ```force_format``` raises an ```ActionController::RoutingError```
49
- if a requested format is not specified via ```:for => []```. It should be easy to
50
- rescue from this exception, for example in your ```application_controller.rb```:
70
+ if a requested format matches none of the attributes specified via ```:for => ...```.
71
+ It should be easy to rescue from this exception, for example in your ```application_controller.rb```:
51
72
 
52
73
 
53
74
  class ApplicationController < ActionController::Base
@@ -63,7 +84,6 @@ rescue from this exception, for example in your ```application_controller.rb```:
63
84
 
64
85
  ## TODO
65
86
  1. More tests
66
- 2. Ability to skip before filters
67
87
  3. More robust params checking
68
88
  4. Custom exception
69
89
 
@@ -22,6 +22,7 @@ module ControllerAccess
22
22
 
23
23
  def force_format_filter_method
24
24
  force_formats = force_format_extract_options_from_filter_chain
25
+ return unless force_formats
25
26
  unsupported = force_formats - FORCE_FORMAT_TYPES
26
27
  raise UnsupportedFormatsError.new("There is no support for #{unsupported} format") if unsupported.any?
27
28
  format = request.format
@@ -33,7 +34,18 @@ module ControllerAccess
33
34
  def force_format_extract_options_from_filter_chain
34
35
  filter = self._process_action_callbacks.find { |f| f.filter == :force_format_filter_method }
35
36
  force_formats = filter.options[:for]
36
- force_formats.is_a?(Array) ? force_formats : (force_formats.is_a?(Symbol) ? [force_formats] : FORCE_FORMAT_DEFAULT_TYPES)
37
+
38
+ if force_formats.is_a? (Array || Symbol)
39
+ [*force_formats]
40
+ elsif force_formats.is_a? Hash
41
+ if force_formats[self.action_name.to_sym]
42
+ [*force_formats[self.action_name.to_sym]]
43
+ else
44
+ force_formats[:default] ? [*force_formats[:default]] : nil
45
+ end
46
+ else
47
+ FORCE_FORMAT_DEFAULT_TYPES
48
+ end
37
49
  end
38
50
 
39
51
  end
@@ -1,3 +1,3 @@
1
1
  module ForceFormat
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
data/spec/base_spec.rb CHANGED
@@ -271,4 +271,74 @@ describe PagesController, :type => :controller do
271
271
 
272
272
  end
273
273
 
274
+
275
+ context "force_format_filter is used with format specified more than once" do
276
+
277
+ controller do
278
+ send "force_format_filter", :for => {:index => [:js], :new => :json}
279
+
280
+ def index
281
+ render "with_js"
282
+ end
283
+
284
+ def new
285
+ render "with_json"
286
+ end
287
+ end
288
+
289
+ it "should respond with 200 for js" do
290
+ get "index", :format => :js
291
+ response.code.should eq("200")
292
+ end
293
+
294
+ it "should respond with 200 for json" do
295
+ get "new", :format => :json
296
+ response.code.should eq("200")
297
+ end
298
+
299
+ it "should respond with RoutingError for html" do
300
+ expect { get "index", :format => :html }.to raise_error(ActionController::RoutingError)
301
+ end
302
+
303
+ it "should respond with RoutingError for html" do
304
+ expect { get "new", :format => :html }.to raise_error(ActionController::RoutingError)
305
+ end
306
+
307
+ end
308
+
309
+ context "force_format_filter is used with format specified more than once but used default" do
310
+
311
+ controller do
312
+ send "force_format_filter", :for => {:index => :js, :default => [:json]}
313
+
314
+ def index
315
+ render "with_js"
316
+ end
317
+
318
+ def new
319
+ render "with_json"
320
+ end
321
+ end
322
+
323
+ it "should respond with 200 for js" do
324
+ get "index", :format => :js
325
+ response.code.should eq("200")
326
+ end
327
+
328
+ it "should respond with 200 for json" do
329
+ get "new", :format => :json
330
+ response.code.should eq("200")
331
+ end
332
+
333
+ it "should respond with RoutingError for html" do
334
+ expect { get "index", :format => :html }.to raise_error(ActionController::RoutingError)
335
+ end
336
+
337
+ it "should respond with RoutingError for html" do
338
+ expect { get "new", :format => :html }.to raise_error(ActionController::RoutingError)
339
+ end
340
+
341
+ end
342
+
343
+
274
344
  end
File without changes
File without changes
File without changes
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: force_format
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - |
@@ -146,6 +146,9 @@ files:
146
146
  - spec/dummy/app/views/pages/with_html.html.erb
147
147
  - spec/dummy/app/views/pages/with_html_js.html.erb
148
148
  - spec/dummy/app/views/pages/with_html_js.js.erb
149
+ - spec/dummy/app/views/pages/with_js.js.erb
150
+ - spec/dummy/app/views/pages/with_json.json.erb
151
+ - spec/dummy/app/views/pages/with_xml.xml.erb
149
152
  - spec/dummy/config.ru
150
153
  - spec/dummy/config/application.rb
151
154
  - spec/dummy/config/boot.rb
@@ -210,6 +213,9 @@ test_files:
210
213
  - spec/dummy/app/views/pages/with_html.html.erb
211
214
  - spec/dummy/app/views/pages/with_html_js.html.erb
212
215
  - spec/dummy/app/views/pages/with_html_js.js.erb
216
+ - spec/dummy/app/views/pages/with_js.js.erb
217
+ - spec/dummy/app/views/pages/with_json.json.erb
218
+ - spec/dummy/app/views/pages/with_xml.xml.erb
213
219
  - spec/dummy/config.ru
214
220
  - spec/dummy/config/application.rb
215
221
  - spec/dummy/config/boot.rb