force_format 0.0.4 → 0.0.5

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: 28bd300a1fa894d648a065a26018ecf8ac37c3d3
4
- data.tar.gz: 8eefca9fc7f88b18668829c9e7dcf02ee89c55a5
3
+ metadata.gz: 2fe49634705c6855feceebc92a404d1387131592
4
+ data.tar.gz: 4612743defc0c159744ff61c0446a9f9a597a192
5
5
  SHA512:
6
- metadata.gz: 62449330b602903831c8abec45e214ecce5314cec8c5104fdee9e7c90159809aab31ebc1c63c092a36313ee5a1ef4b95fdd9b9b77bd113fd84d27251aaef89e9
7
- data.tar.gz: 40aaacd7294b881fbce2125039df48e131b5d28ec3d80acfafdd26bdd88a49241ac9378a95d263aafe317887bb684068814a90b4d1346e3ae40b7a24f9d9548a
6
+ metadata.gz: 9e8dd46441bd10e71348585584b5a06f75b2785755a7d83872b2c804b1c7fffb3132e23fb0025c992c15b3ef2bd2d9aa5a0f19051483da453dfd7adc53609704
7
+ data.tar.gz: f09a265a3532b543204e86e62100a6bb757b417c8260807a8540bee041722f17ba9f292edb75abb29ed74286e6a454cfead4cbe6239591dc43ee59657f56aea0
data/README.md CHANGED
@@ -28,6 +28,8 @@ Or install it yourself as:
28
28
 
29
29
  ## Usage
30
30
 
31
+ #### Basics
32
+
31
33
  Include the ```force_format_filter``` method in your controllers.
32
34
  The important param is the ```:for => [:my, :formats]```.
33
35
  With that given array of fomat types you can define the formats the
@@ -47,6 +49,8 @@ and ```:unless => ...``` parameters like the Rails filters.
47
49
  If you want to skip the filter in inherited controllers, use the ```skip_force_format_filter``` method.
48
50
  It accepts the same parameters the ```force_format_filter``` methods except ```:for => ...```.
49
51
 
52
+ #### And more options...
53
+
50
54
  Maybe you want to define the formats more granular, for example different per action.
51
55
  To accomplish this, pass an hash with action names and required formats. Add a *:default* key with formats
52
56
  for actions that are not specified directly.
@@ -66,6 +70,8 @@ for actions that are not specified directly.
66
70
  end
67
71
 
68
72
 
73
+ #### Exceptions
74
+
69
75
  By default ```force_format``` raises an ```ActionController::RoutingError```
70
76
  if a requested format matches none of the attributes specified via ```:for => ...```.
71
77
  It should be easy to rescue from this exception, for example in your ```application_controller.rb```:
@@ -79,13 +85,20 @@ It should be easy to rescue from this exception, for example in your ```applicat
79
85
  # handle it
80
86
  end
81
87
  end
88
+
82
89
 
90
+ You can pass an custom exception lambda to the ```force_format_filter``` method for a better error handling.
91
+
92
+ class PagesController < ApplicationController
93
+ force_format_filter :for => :html, :exception => lambda { |msg| raise(MyApp::AwesomeException.new(msg)) }
94
+ end
83
95
 
96
+ NOTE: Call the method ```force_format_filter``` only once per controller!
97
+ If you call it multiple times, the last one would be used.
84
98
 
85
99
  ## TODO
86
100
  1. More tests
87
- 3. More robust params checking
88
- 4. Custom exception
101
+ 2. More robust params checking
89
102
 
90
103
 
91
104
  ## Contributing
@@ -4,12 +4,13 @@ module ControllerAccess
4
4
  extend ActiveSupport::Concern
5
5
  FORCE_FORMAT_TYPES = [:html, :js, :json, :pdf, :csv, :zip, :xml]
6
6
  FORCE_FORMAT_DEFAULT_TYPES = [:html]
7
+ FORCE_FORMAT_EXCEPTION = lambda { |o| raise(ActionController::RoutingError, o) }
7
8
 
8
9
  module ClassMethods
9
10
  include ForceFormat::Errors
10
11
 
11
12
  def force_format_filter(opts={})
12
- send(:before_filter, :force_format_filter_method, opts.slice(:only, :except, :if, :unless, :for))
13
+ send(:before_filter, :force_format_filter_method, opts.slice(:only, :except, :if, :unless, :for, :exception))
13
14
  end
14
15
 
15
16
  def skip_force_format_filter(opts={})
@@ -21,19 +22,22 @@ module ControllerAccess
21
22
  private
22
23
 
23
24
  def force_format_filter_method
24
- force_formats = force_format_extract_options_from_filter_chain
25
+ force_formats = force_format_extract_formats
25
26
  return unless force_formats
26
27
  unsupported = force_formats - FORCE_FORMAT_TYPES
27
28
  raise UnsupportedFormatsError.new("There is no support for #{unsupported} format") if unsupported.any?
28
29
  format = request.format
29
30
  unless force_formats.include?(format.try(:to_sym))
30
- raise ActionController::RoutingError, "Format '#{format}' not supported for #{request.path.inspect}"
31
+ force_format_extract_exception.call("Format '#{format}' not supported for #{request.path.inspect}")
31
32
  end
32
33
  end
33
34
 
34
- def force_format_extract_options_from_filter_chain
35
+ def force_format_load_filter_chain
35
36
  filter = self._process_action_callbacks.find { |f| f.filter == :force_format_filter_method }
36
- force_formats = filter.options[:for]
37
+ end
38
+
39
+ def force_format_extract_formats
40
+ force_formats = force_format_load_filter_chain.options[:for]
37
41
 
38
42
  if force_formats.is_a? (Array || Symbol)
39
43
  [*force_formats]
@@ -48,4 +52,8 @@ module ControllerAccess
48
52
  end
49
53
  end
50
54
 
55
+ def force_format_extract_exception
56
+ force_format_load_filter_chain.options[:exception] || FORCE_FORMAT_EXCEPTION
57
+ end
58
+
51
59
  end
@@ -1,3 +1,3 @@
1
1
  module ForceFormat
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
data/spec/base_spec.rb CHANGED
@@ -341,4 +341,51 @@ describe PagesController, :type => :controller do
341
341
  end
342
342
 
343
343
 
344
+ context "force_format_filter is used with unsupported format" do
345
+
346
+ controller do
347
+ send "force_format_filter", :for => {:index => :what}
348
+
349
+ def index
350
+ render "with_js"
351
+ end
352
+ end
353
+
354
+ it "should respond with RoutingError for html" do
355
+ expect { get "index", :format => :html }.to raise_error(UnsupportedFormatsError)
356
+ end
357
+
358
+ end
359
+
360
+ context "force_format_filter is used with unsupported format" do
361
+
362
+ controller do
363
+ send "force_format_filter", :for => {:index => :what}
364
+
365
+ def index
366
+ render "with_js"
367
+ end
368
+ end
369
+
370
+ it "should respond with RoutingError for html" do
371
+ expect { get "index", :format => :html }.to raise_error(UnsupportedFormatsError)
372
+ end
373
+
374
+ end
375
+
376
+ context "force_format_filter is used with a custom exception" do
377
+
378
+ controller do
379
+ send "force_format_filter", :for => [:js], :exception => lambda { |o| raise(ActiveRecord::RecordNotFound, o) }
380
+
381
+ def index
382
+ render "with_js"
383
+ end
384
+ end
385
+
386
+ it "should respond with RoutingError for html" do
387
+ expect { get "index", :format => :html }.to raise_error(ActiveRecord::RecordNotFound)
388
+ end
389
+
390
+ end
344
391
  end
data/spec/spec_helper.rb CHANGED
@@ -6,6 +6,9 @@ require 'rspec/rails'
6
6
  require 'rspec/autorun'
7
7
  require 'force_format'
8
8
  require 'pry'
9
+ require_relative '../lib/force_format/errors'
10
+
11
+ include ForceFormat::Errors
9
12
 
10
13
  RSpec.configure do |config|
11
14
  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.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - |