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 +4 -4
- data/lib/force_format/controller_access.rb +58 -40
- data/lib/force_format/version.rb +1 -1
- data/lib/force_format/view_patch.rb +10 -8
- data/spec/base_spec.rb +41 -3
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 00ac779bcc013cd1f365392fc62a32e2f4f46e44
|
4
|
+
data.tar.gz: fa92721556b7cdf1ab10e2e0939ab5bbb16afd4d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6c61b3540f0d4e658ec60c82327d0de69f7f03238fe5cd3efcb478abd59d13aeacc9812a6aa04e53eba27323c72c6d7c08738a969cd21744a4249c57fddffee8
|
7
|
+
data.tar.gz: 6acfad6780cb668e7ffd489ae62317a1efa9ee6acc350928323dc8ce2719fce90a3b6e91926eb2ef6297bea4fa631b530ef915a02f091754f0fa74e3042e849e
|
@@ -1,59 +1,77 @@
|
|
1
1
|
require_relative "errors"
|
2
2
|
|
3
|
-
module
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
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
|
-
|
10
|
-
|
11
|
+
module ClassMethods
|
12
|
+
include ForceFormat::Errors
|
11
13
|
|
12
|
-
|
13
|
-
|
14
|
-
|
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
|
-
|
26
|
+
private
|
21
27
|
|
22
|
-
|
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
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
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
|
-
|
36
|
-
|
37
|
-
|
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
|
-
|
40
|
-
|
48
|
+
def force_format_extract_formats
|
49
|
+
force_formats = force_format_load_filter_chain.options[:for]
|
41
50
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
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
|
-
|
60
|
+
FORCE_FORMAT_DEFAULT_TYPES
|
49
61
|
end
|
50
|
-
else
|
51
|
-
FORCE_FORMAT_DEFAULT_TYPES
|
52
62
|
end
|
53
|
-
end
|
54
63
|
|
55
|
-
|
56
|
-
|
57
|
-
|
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
|
data/lib/force_format/version.rb
CHANGED
@@ -1,11 +1,13 @@
|
|
1
|
-
module
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
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
|
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
|
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
|
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.
|
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-
|
12
|
+
date: 2013-09-27 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|