force_format 0.0.5 → 0.0.6

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2fe49634705c6855feceebc92a404d1387131592
4
- data.tar.gz: 4612743defc0c159744ff61c0446a9f9a597a192
3
+ metadata.gz: 00ac779bcc013cd1f365392fc62a32e2f4f46e44
4
+ data.tar.gz: fa92721556b7cdf1ab10e2e0939ab5bbb16afd4d
5
5
  SHA512:
6
- metadata.gz: 9e8dd46441bd10e71348585584b5a06f75b2785755a7d83872b2c804b1c7fffb3132e23fb0025c992c15b3ef2bd2d9aa5a0f19051483da453dfd7adc53609704
7
- data.tar.gz: f09a265a3532b543204e86e62100a6bb757b417c8260807a8540bee041722f17ba9f292edb75abb29ed74286e6a454cfead4cbe6239591dc43ee59657f56aea0
6
+ metadata.gz: 6c61b3540f0d4e658ec60c82327d0de69f7f03238fe5cd3efcb478abd59d13aeacc9812a6aa04e53eba27323c72c6d7c08738a969cd21744a4249c57fddffee8
7
+ data.tar.gz: 6acfad6780cb668e7ffd489ae62317a1efa9ee6acc350928323dc8ce2719fce90a3b6e91926eb2ef6297bea4fa631b530ef915a02f091754f0fa74e3042e849e
@@ -1,59 +1,77 @@
1
1
  require_relative "errors"
2
2
 
3
- module ControllerAccess
4
- extend ActiveSupport::Concern
5
- FORCE_FORMAT_TYPES = [:html, :js, :json, :pdf, :csv, :zip, :xml]
6
- FORCE_FORMAT_DEFAULT_TYPES = [:html]
7
- FORCE_FORMAT_EXCEPTION = lambda { |o| raise(ActionController::RoutingError, o) }
3
+ module ForceFormat
4
+ module ControllerAccess
5
+ extend ActiveSupport::Concern
6
+ FORCE_FORMAT_TYPES = [:html, :js, :json, :pdf, :csv, :zip, :xml]
7
+ FORCE_FORMAT_DEFAULT_TYPES = [:html]
8
+ FORCE_FORMAT_EXCEPTION = lambda { |o| raise(ActionController::RoutingError, o) }
9
+ FORCE_FORMAT_WILDCARD = "*/*"
8
10
 
9
- module ClassMethods
10
- include ForceFormat::Errors
11
+ module ClassMethods
12
+ include ForceFormat::Errors
11
13
 
12
- def force_format_filter(opts={})
13
- send(:before_filter, :force_format_filter_method, opts.slice(:only, :except, :if, :unless, :for, :exception))
14
- end
14
+ def force_format_filter(opts={})
15
+ parsed = opts.slice(:only, :except, :if, :unless, :for, :exception, :skip_wildcard)
16
+ send(:before_filter, :force_format_filter_method, parsed)
17
+ end
18
+
19
+ def skip_force_format_filter(opts={})
20
+ parsed = opts.slice(:only, :except, :if, :unless)
21
+ send(:skip_before_filter, :force_format_filter_method, parsed)
22
+ end
15
23
 
16
- def skip_force_format_filter(opts={})
17
- send(:skip_before_filter, :force_format_filter_method, opts.slice(:only, :except, :if, :unless))
18
24
  end
19
25
 
20
- end
26
+ private
21
27
 
22
- private
28
+ def force_format_filter_method
29
+ force_formats = force_format_extract_formats
30
+ return unless force_formats
31
+ unsupported = force_formats - FORCE_FORMAT_TYPES
32
+ raise UnsupportedFormatsError.new("There is no support for #{unsupported} format") if unsupported.any?
23
33
 
24
- def force_format_filter_method
25
- force_formats = force_format_extract_formats
26
- return unless force_formats
27
- unsupported = force_formats - FORCE_FORMAT_TYPES
28
- raise UnsupportedFormatsError.new("There is no support for #{unsupported} format") if unsupported.any?
29
- format = request.format
30
- unless force_formats.include?(format.try(:to_sym))
31
- force_format_extract_exception.call("Format '#{format}' not supported for #{request.path.inspect}")
34
+ if request.format.to_s == FORCE_FORMAT_WILDCARD and not force_format_skip_wildcard_rewrite?
35
+ request.format = :html
36
+ end
37
+
38
+ format = request.format
39
+ unless force_formats.include?(format.try(:to_sym))
40
+ force_format_extract_exception.call("Format '#{format}' not supported for #{request.path.inspect}")
41
+ end
32
42
  end
33
- end
34
43
 
35
- def force_format_load_filter_chain
36
- filter = self._process_action_callbacks.find { |f| f.filter == :force_format_filter_method }
37
- end
44
+ def force_format_load_filter_chain
45
+ filter = self._process_action_callbacks.find { |f| f.filter == :force_format_filter_method }
46
+ end
38
47
 
39
- def force_format_extract_formats
40
- force_formats = force_format_load_filter_chain.options[:for]
48
+ def force_format_extract_formats
49
+ force_formats = force_format_load_filter_chain.options[:for]
41
50
 
42
- if force_formats.is_a? (Array || Symbol)
43
- [*force_formats]
44
- elsif force_formats.is_a? Hash
45
- if force_formats[self.action_name.to_sym]
46
- [*force_formats[self.action_name.to_sym]]
51
+ if force_formats.is_a? (Array || Symbol)
52
+ [*force_formats]
53
+ elsif force_formats.is_a? Hash
54
+ if force_formats[self.action_name.to_sym]
55
+ [*force_formats[self.action_name.to_sym]]
56
+ else
57
+ force_formats[:default] ? [*force_formats[:default]] : nil
58
+ end
47
59
  else
48
- force_formats[:default] ? [*force_formats[:default]] : nil
60
+ FORCE_FORMAT_DEFAULT_TYPES
49
61
  end
50
- else
51
- FORCE_FORMAT_DEFAULT_TYPES
52
62
  end
53
- end
54
63
 
55
- def force_format_extract_exception
56
- force_format_load_filter_chain.options[:exception] || FORCE_FORMAT_EXCEPTION
57
- end
64
+ def force_format_extract_wildcard_option
65
+ force_format_load_filter_chain.options[:skip_wildcard]
66
+ end
58
67
 
68
+ def force_format_skip_wildcard_rewrite?
69
+ force_format_extract_wildcard_option
70
+ end
71
+
72
+ def force_format_extract_exception
73
+ force_format_load_filter_chain.options[:exception] || FORCE_FORMAT_EXCEPTION
74
+ end
75
+
76
+ end
59
77
  end
@@ -1,3 +1,3 @@
1
1
  module ForceFormat
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -1,11 +1,13 @@
1
- module ViewPatch
2
- class ActionView::LookupContext
3
- def formats=(values)
4
- if values
5
- values.concat(default_formats) if values.delete "*/*"
6
- values << :html if values.empty?
1
+ module ForceFormat
2
+ module ViewPatch
3
+ class ActionView::LookupContext
4
+ def formats=(values)
5
+ if values
6
+ values.concat(default_formats) if values.delete "*/*"
7
+ values << :html if values.empty?
8
+ end
9
+ super(values)
7
10
  end
8
- super(values)
9
11
  end
10
12
  end
11
- end
13
+ end
data/spec/base_spec.rb CHANGED
@@ -351,7 +351,7 @@ describe PagesController, :type => :controller do
351
351
  end
352
352
  end
353
353
 
354
- it "should respond with RoutingError for html" do
354
+ it "should respond with UnsupportedFormatsError for html" do
355
355
  expect { get "index", :format => :html }.to raise_error(UnsupportedFormatsError)
356
356
  end
357
357
 
@@ -367,7 +367,7 @@ describe PagesController, :type => :controller do
367
367
  end
368
368
  end
369
369
 
370
- it "should respond with RoutingError for html" do
370
+ it "should respond with UnsupportedFormatsError for html" do
371
371
  expect { get "index", :format => :html }.to raise_error(UnsupportedFormatsError)
372
372
  end
373
373
 
@@ -383,9 +383,47 @@ describe PagesController, :type => :controller do
383
383
  end
384
384
  end
385
385
 
386
- it "should respond with RoutingError for html" do
386
+ it "should respond with RecordNotFound for html" do
387
387
  expect { get "index", :format => :html }.to raise_error(ActiveRecord::RecordNotFound)
388
388
  end
389
389
 
390
390
  end
391
+
392
+ context "force_format_filter is used with a wildcard format and rewrite set" do
393
+
394
+ controller do
395
+ send "force_format_filter", :for => :html, :skip_wildcard => false
396
+
397
+ def index
398
+ render "with_html"
399
+ end
400
+ end
401
+
402
+ it "should respond with 200 for html" do
403
+ request.env["HTTP_ACCEPT"] = '*/*'
404
+ get "index"
405
+ end
406
+
407
+ end
408
+
409
+
410
+ context "force_format_filter is used with a wildcard format and rewrite set to false" do
411
+
412
+ controller do
413
+ send "force_format_filter", :for => :html, :skip_wildcard => true
414
+
415
+ def index
416
+ render "with_html"
417
+ end
418
+ end
419
+
420
+ it "should respond with RoutingError for html" do
421
+ expect do
422
+ request.env["HTTP_ACCEPT"] = '*/*'
423
+ get "index"
424
+ end.to raise_error(ActionController::RoutingError)
425
+ end
426
+
427
+ end
428
+
391
429
  end
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.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - |
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-09-26 00:00:00.000000000 Z
12
+ date: 2013-09-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler