force_format 0.0.2 → 0.0.3

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: a1866205dca78a0b08e5ac3f71874418eff03487
4
- data.tar.gz: d7e6868ffd9742d3e3719e4fd6d20b762eee4dd3
3
+ metadata.gz: a968ff510d11c69b4429c7f611873c9329e0d3dd
4
+ data.tar.gz: e41c0108cdb7b26af2622c7aa1cac7ad144c84c5
5
5
  SHA512:
6
- metadata.gz: 7d977a2e03662310507ca4cae5b9313e76cdb6647b066f37e2f1356cfb4a9e9d606b93517cb99b9055f1855f82659b022f13a3b8727440e366a405f80a547e00
7
- data.tar.gz: 1c5156e06744368cd0a386eca1bc370023ffed67cd7ba8db0218fea225f4c9d9323b0c27214a07b75681e0b060078c6ea49145b3be0378c32a0f0a328e08bf7b
6
+ metadata.gz: 885860ffa29446ae06b1ca60119f645777d62b669649129e127e9f8bf761ade6def5c6453670778805c618133d4608c5bfa08258dc4565f6825c3e729afafe5a
7
+ data.tar.gz: 1e1f4969071e189f20fa6c325ff96aa900c455cab5dc0c3b53c35a27c3b627ffdf0918c15c4f086bce90506499e2d76ea6fbc5b4b2f12b31460f2b010de8367d
data/force_format.gemspec CHANGED
@@ -23,6 +23,7 @@ Gem::Specification.new do |spec|
23
23
  spec.add_development_dependency 'rspec-rails', '~> 2.0'
24
24
  spec.add_development_dependency 'shoulda-matchers', '~> 2.4'
25
25
  spec.add_development_dependency "sqlite3"
26
+ spec.add_development_dependency "pry"
26
27
 
27
28
  spec.add_dependency "rails", "~> 3.2"
28
29
  end
@@ -2,44 +2,38 @@ require_relative "errors"
2
2
 
3
3
  module ControllerAccess
4
4
  extend ActiveSupport::Concern
5
+ FORCE_FORMAT_TYPES = [:html, :js, :json, :pdf, :csv, :zip, :xml]
6
+ FORCE_FORMAT_DEFAULT_TYPES = [:html]
5
7
 
6
8
  module ClassMethods
7
9
  include ForceFormat::Errors
8
- FORCE_FORMAT_TYPES = [:html, :js, :json, :pdf, :csv, :zip, :xml]
9
- FORCE_FORMAT_DEFAULT_TYPES = [:html]
10
10
 
11
11
  def force_format_filter(opts={})
12
- forced_formats = extract_options(opts)
13
-
14
- unsupported = forced_formats - FORCE_FORMAT_TYPES
15
- raise UnsupportedFormatsError.new("There is no support for #{unsupported} format") if unsupported.any?
16
-
17
- self.send(:before_filter, opts.slice(:only, :except, :if, :unless)) do |controller|
18
- return if controller.instance_variable_get("@_skip_force_format_filter") == true
19
- format = controller.request.format
20
- unless forced_formats.include?(format.try(:to_sym))
21
- raise ActionController::RoutingError, "Format '#{format}' not supported for #{request.path.inspect}"
22
- end
23
- end
12
+ send(:before_filter, :force_format_filter_method, opts.slice(:only, :except, :if, :unless, :for))
24
13
  end
25
14
 
26
15
  def skip_force_format_filter(opts={})
27
- self.send(:prepend_before_filter, opts.slice(:only, :except, :if, :unless)) do |controller|
28
- controller.instance_variable_set("@_skip_force_format_filter", true)
29
- end
16
+ send(:skip_before_filter, :force_format_filter_method, opts.slice(:only, :except, :if, :unless))
30
17
  end
31
18
 
32
- private
19
+ end
20
+
21
+ private
33
22
 
34
- def extract_options(opts)
35
- if opts[:for].is_a?(Array)
36
- opts[:for]
37
- elsif opts[:for].is_a?(Symbol)
38
- [opts[:for]]
39
- else
40
- FORCE_FORMAT_DEFAULT_TYPES
41
- end
23
+ def force_format_filter_method
24
+ force_formats = force_format_extract_options_from_filter_chain
25
+ unsupported = force_formats - FORCE_FORMAT_TYPES
26
+ raise UnsupportedFormatsError.new("There is no support for #{unsupported} format") if unsupported.any?
27
+ format = request.format
28
+ unless force_formats.include?(format.try(:to_sym))
29
+ raise ActionController::RoutingError, "Format '#{format}' not supported for #{request.path.inspect}"
42
30
  end
31
+ end
43
32
 
33
+ def force_format_extract_options_from_filter_chain
34
+ filter = self._process_action_callbacks.find { |f| f.filter == :force_format_filter_method }
35
+ force_formats = filter.options[:for]
36
+ force_formats.is_a?(Array) ? force_formats : (force_formats.is_a?(Symbol) ? [force_formats] : FORCE_FORMAT_DEFAULT_TYPES)
44
37
  end
38
+
45
39
  end
@@ -1,3 +1,3 @@
1
1
  module ForceFormat
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/spec/spec_helper.rb CHANGED
@@ -5,6 +5,7 @@ require File.expand_path("../dummy/config/environment.rb", __FILE__)
5
5
  require 'rspec/rails'
6
6
  require 'rspec/autorun'
7
7
  require 'force_format'
8
+ require 'pry'
8
9
 
9
10
  RSpec.configure do |config|
10
11
  config.treat_symbols_as_metadata_keys_with_true_values = true
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.2
4
+ version: 0.0.3
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-25 00:00:00.000000000 Z
12
+ date: 2013-09-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -81,6 +81,20 @@ dependencies:
81
81
  - - '>='
82
82
  - !ruby/object:Gem::Version
83
83
  version: '0'
84
+ - !ruby/object:Gem::Dependency
85
+ name: pry
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
84
98
  - !ruby/object:Gem::Dependency
85
99
  name: rails
86
100
  requirement: !ruby/object:Gem::Requirement