force_format 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/README.md +31 -21
- data/force_format.gemspec +5 -5
- data/lib/force_format/controller_access.rb +7 -0
- data/lib/force_format/version.rb +1 -1
- data/spec/base_spec.rb +133 -0
- data/spec/dummy/config/routes.rb +1 -0
- metadata +18 -17
- data/.rvmrc +0 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a1866205dca78a0b08e5ac3f71874418eff03487
|
4
|
+
data.tar.gz: d7e6868ffd9742d3e3719e4fd6d20b762eee4dd3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7d977a2e03662310507ca4cae5b9313e76cdb6647b066f37e2f1356cfb4a9e9d606b93517cb99b9055f1855f82659b022f13a3b8727440e366a405f80a547e00
|
7
|
+
data.tar.gz: 1c5156e06744368cd0a386eca1bc370023ffed67cd7ba8db0218fea225f4c9d9323b0c27214a07b75681e0b060078c6ea49145b3be0378c32a0f0a328e08bf7b
|
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
force-format
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby-2.0.0-p247
|
data/README.md
CHANGED
@@ -1,9 +1,16 @@
|
|
1
1
|
# force_format
|
2
2
|
|
3
|
-
Define the formats your Rails application should respond to.
|
4
|
-
Normally a Rails3 application tries to respond to all kinds of formats (e.g. html, xml, json, ...).
|
5
|
-
Unfortunately this will raise
|
6
|
-
This is where ```force_format``` joins the game
|
3
|
+
Define the formats your Rails application should respond to within your controllers.
|
4
|
+
Normally a Rails3 application tries to respond to all kinds of formats (e.g. html, xml, json, ...).
|
5
|
+
Unfortunately this will raise errors if the template for the given format can not be found.
|
6
|
+
This is where ```force_format``` joins the game.
|
7
|
+
|
8
|
+
|
9
|
+
## Requirements
|
10
|
+
|
11
|
+
```rails (3.2.x)```
|
12
|
+
|
13
|
+
(This gem is tested with Rails 3.2.x only, but other versions may work too.)
|
7
14
|
|
8
15
|
## Installation
|
9
16
|
|
@@ -21,41 +28,44 @@ Or install it yourself as:
|
|
21
28
|
|
22
29
|
## Usage
|
23
30
|
|
24
|
-
Include the ```force_format_filter``` method in your controllers.
|
25
|
-
The important param is the ```:for => [:my, :formats]
|
26
|
-
|
27
|
-
|
31
|
+
Include the ```force_format_filter``` method in your controllers.
|
32
|
+
The important param is the ```:for => [:my, :formats]```.
|
33
|
+
With that given array of fomat types you can define the formats the
|
34
|
+
action should respond to (and hopefully find a template).
|
35
|
+
In addition it accepts ```:only => ...```, ```:except => ...```, ```:if => ...```
|
36
|
+
and ```:unless => ...``` parameters like the Rails filters.
|
37
|
+
|
38
|
+
|
28
39
|
|
29
|
-
```
|
30
40
|
class PagesController < ApplicationController
|
31
41
|
force_format_filter :for => [:html, :js], :only => :index
|
32
|
-
|
42
|
+
|
33
43
|
def index
|
34
44
|
end
|
35
45
|
end
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
if a requested format is not specified via ```:for => []```. It should be easy to
|
46
|
+
|
47
|
+
|
48
|
+
By default ```force_format``` raises an ```ActionController::RoutingError```
|
49
|
+
if a requested format is not specified via ```:for => []```. It should be easy to
|
41
50
|
rescue from this exception, for example in your ```application_controller.rb```:
|
42
51
|
|
43
|
-
|
52
|
+
|
44
53
|
class ApplicationController < ActionController::Base
|
45
54
|
|
46
55
|
rescue_from ActionController::RoutingError, :with => :render_error
|
47
|
-
|
56
|
+
|
48
57
|
def render_error
|
49
58
|
# handle it
|
50
59
|
end
|
51
60
|
end
|
52
|
-
|
53
|
-
```
|
61
|
+
|
54
62
|
|
55
63
|
|
56
64
|
## TODO
|
57
|
-
1.
|
58
|
-
2.
|
65
|
+
1. More tests
|
66
|
+
2. Ability to skip before filters
|
67
|
+
3. More robust params checking
|
68
|
+
4. Custom exception
|
59
69
|
|
60
70
|
|
61
71
|
## Contributing
|
data/force_format.gemspec
CHANGED
@@ -8,7 +8,7 @@ Gem::Specification.new do |spec|
|
|
8
8
|
spec.version = ForceFormat::VERSION
|
9
9
|
spec.authors = ["Marcus Geißler\n"]
|
10
10
|
spec.email = ["marcus3006@gmail.com"]
|
11
|
-
spec.description = %q{Define the formats your Rails application should respond to}
|
11
|
+
spec.description = %q{Define the formats your Rails application should respond to in your controllers}
|
12
12
|
spec.summary = %q{Forcing the format Rails controllers should respond to}
|
13
13
|
spec.homepage = "https://github.com/marcusg/force_format"
|
14
14
|
spec.license = "MIT"
|
@@ -19,10 +19,10 @@ Gem::Specification.new do |spec|
|
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
21
|
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
-
spec.add_development_dependency "rake"
|
23
|
-
spec.add_development_dependency 'rspec-rails'
|
24
|
-
spec.add_development_dependency 'shoulda-matchers'
|
22
|
+
spec.add_development_dependency "rake", '~> 10.1'
|
23
|
+
spec.add_development_dependency 'rspec-rails', '~> 2.0'
|
24
|
+
spec.add_development_dependency 'shoulda-matchers', '~> 2.4'
|
25
25
|
spec.add_development_dependency "sqlite3"
|
26
26
|
|
27
|
-
spec.add_dependency "rails", "~> 3.2
|
27
|
+
spec.add_dependency "rails", "~> 3.2"
|
28
28
|
end
|
@@ -15,6 +15,7 @@ module ControllerAccess
|
|
15
15
|
raise UnsupportedFormatsError.new("There is no support for #{unsupported} format") if unsupported.any?
|
16
16
|
|
17
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
|
18
19
|
format = controller.request.format
|
19
20
|
unless forced_formats.include?(format.try(:to_sym))
|
20
21
|
raise ActionController::RoutingError, "Format '#{format}' not supported for #{request.path.inspect}"
|
@@ -22,6 +23,12 @@ module ControllerAccess
|
|
22
23
|
end
|
23
24
|
end
|
24
25
|
|
26
|
+
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
|
30
|
+
end
|
31
|
+
|
25
32
|
private
|
26
33
|
|
27
34
|
def extract_options(opts)
|
data/lib/force_format/version.rb
CHANGED
data/spec/base_spec.rb
CHANGED
@@ -138,4 +138,137 @@ describe PagesController, :type => :controller do
|
|
138
138
|
end
|
139
139
|
|
140
140
|
|
141
|
+
context "force_format_filter is used with skip_format_filter specified for all actions" do
|
142
|
+
|
143
|
+
controller do
|
144
|
+
send "force_format_filter"
|
145
|
+
send "skip_force_format_filter"
|
146
|
+
|
147
|
+
def index
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
it "should respond with 200 for html" do
|
152
|
+
get "index"
|
153
|
+
response.code.should eq("200")
|
154
|
+
end
|
155
|
+
|
156
|
+
it "should respond with 200 for html" do
|
157
|
+
get "index", :format => :html
|
158
|
+
response.code.should eq("200")
|
159
|
+
end
|
160
|
+
|
161
|
+
it "should respond with MissingTemplate for js" do
|
162
|
+
expect { get "index", :format => :js }.to raise_error(ActionView::MissingTemplate)
|
163
|
+
end
|
164
|
+
|
165
|
+
it "should respond with MissingTemplate for xml" do
|
166
|
+
expect { get "index", :format => :xml }.to raise_error(ActionView::MissingTemplate)
|
167
|
+
end
|
168
|
+
|
169
|
+
it "should respond with MissingTemplate for json" do
|
170
|
+
expect { get "index", :format => :json }.to raise_error(ActionView::MissingTemplate)
|
171
|
+
end
|
172
|
+
|
173
|
+
end
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
context "force_format_filter is used with skip_format_filter specified for all actions" do
|
178
|
+
|
179
|
+
controller do
|
180
|
+
send "skip_force_format_filter"
|
181
|
+
|
182
|
+
def index
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
186
|
+
it "should respond with 200 for html" do
|
187
|
+
get "index"
|
188
|
+
response.code.should eq("200")
|
189
|
+
end
|
190
|
+
|
191
|
+
it "should respond with 200 for html" do
|
192
|
+
get "index", :format => :html
|
193
|
+
response.code.should eq("200")
|
194
|
+
end
|
195
|
+
|
196
|
+
it "should respond with MissingTemplate for js" do
|
197
|
+
expect { get "index", :format => :js }.to raise_error(ActionView::MissingTemplate)
|
198
|
+
end
|
199
|
+
|
200
|
+
it "should respond with MissingTemplate for xml" do
|
201
|
+
expect { get "index", :format => :xml }.to raise_error(ActionView::MissingTemplate)
|
202
|
+
end
|
203
|
+
|
204
|
+
it "should respond with MissingTemplate for json" do
|
205
|
+
expect { get "index", :format => :json }.to raise_error(ActionView::MissingTemplate)
|
206
|
+
end
|
207
|
+
|
208
|
+
end
|
209
|
+
|
210
|
+
|
211
|
+
|
212
|
+
context "force_format_filter is used with no format specified for all actions" do
|
213
|
+
|
214
|
+
controller do
|
215
|
+
send "force_format_filter", :only => :index
|
216
|
+
send "skip_force_format_filter", :only => :skipped
|
217
|
+
|
218
|
+
def new
|
219
|
+
render "with_html"
|
220
|
+
end
|
221
|
+
|
222
|
+
def index
|
223
|
+
render "with_html"
|
224
|
+
end
|
225
|
+
end
|
226
|
+
|
227
|
+
it "should respond with 200 for html" do
|
228
|
+
get "index"
|
229
|
+
response.code.should eq("200")
|
230
|
+
end
|
231
|
+
|
232
|
+
it "should respond with 200 for html" do
|
233
|
+
get "index", :format => :html
|
234
|
+
response.code.should eq("200")
|
235
|
+
end
|
236
|
+
|
237
|
+
it "should respond with RoutingError for js" do
|
238
|
+
expect { get "index", :format => :js }.to raise_error(ActionController::RoutingError)
|
239
|
+
end
|
240
|
+
|
241
|
+
it "should respond with RoutingError for xml" do
|
242
|
+
expect { get "index", :format => :xml }.to raise_error(ActionController::RoutingError)
|
243
|
+
end
|
244
|
+
|
245
|
+
it "should respond with RoutingError for json" do
|
246
|
+
expect { get "index", :format => :json }.to raise_error(ActionController::RoutingError)
|
247
|
+
end
|
248
|
+
|
249
|
+
|
250
|
+
it "should respond with 200 for html" do
|
251
|
+
get "new"
|
252
|
+
response.code.should eq("200")
|
253
|
+
end
|
254
|
+
|
255
|
+
it "should respond with 200 for html" do
|
256
|
+
get "new", :format => :html
|
257
|
+
response.code.should eq("200")
|
258
|
+
end
|
259
|
+
|
260
|
+
it "should respond with MissingTemplate for js" do
|
261
|
+
expect { get "new", :format => :js }.to raise_error(ActionView::MissingTemplate)
|
262
|
+
end
|
263
|
+
|
264
|
+
it "should respond with MissingTemplate for xml" do
|
265
|
+
expect { get "new", :format => :xml }.to raise_error(ActionView::MissingTemplate)
|
266
|
+
end
|
267
|
+
|
268
|
+
it "should respond with MissingTemplate for json" do
|
269
|
+
expect { get "new", :format => :json }.to raise_error(ActionView::MissingTemplate)
|
270
|
+
end
|
271
|
+
|
272
|
+
end
|
273
|
+
|
141
274
|
end
|
data/spec/dummy/config/routes.rb
CHANGED
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.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- |
|
@@ -29,44 +29,44 @@ dependencies:
|
|
29
29
|
name: rake
|
30
30
|
requirement: !ruby/object:Gem::Requirement
|
31
31
|
requirements:
|
32
|
-
- -
|
32
|
+
- - ~>
|
33
33
|
- !ruby/object:Gem::Version
|
34
|
-
version: '
|
34
|
+
version: '10.1'
|
35
35
|
type: :development
|
36
36
|
prerelease: false
|
37
37
|
version_requirements: !ruby/object:Gem::Requirement
|
38
38
|
requirements:
|
39
|
-
- -
|
39
|
+
- - ~>
|
40
40
|
- !ruby/object:Gem::Version
|
41
|
-
version: '
|
41
|
+
version: '10.1'
|
42
42
|
- !ruby/object:Gem::Dependency
|
43
43
|
name: rspec-rails
|
44
44
|
requirement: !ruby/object:Gem::Requirement
|
45
45
|
requirements:
|
46
|
-
- -
|
46
|
+
- - ~>
|
47
47
|
- !ruby/object:Gem::Version
|
48
|
-
version: '0'
|
48
|
+
version: '2.0'
|
49
49
|
type: :development
|
50
50
|
prerelease: false
|
51
51
|
version_requirements: !ruby/object:Gem::Requirement
|
52
52
|
requirements:
|
53
|
-
- -
|
53
|
+
- - ~>
|
54
54
|
- !ruby/object:Gem::Version
|
55
|
-
version: '0'
|
55
|
+
version: '2.0'
|
56
56
|
- !ruby/object:Gem::Dependency
|
57
57
|
name: shoulda-matchers
|
58
58
|
requirement: !ruby/object:Gem::Requirement
|
59
59
|
requirements:
|
60
|
-
- -
|
60
|
+
- - ~>
|
61
61
|
- !ruby/object:Gem::Version
|
62
|
-
version: '
|
62
|
+
version: '2.4'
|
63
63
|
type: :development
|
64
64
|
prerelease: false
|
65
65
|
version_requirements: !ruby/object:Gem::Requirement
|
66
66
|
requirements:
|
67
|
-
- -
|
67
|
+
- - ~>
|
68
68
|
- !ruby/object:Gem::Version
|
69
|
-
version: '
|
69
|
+
version: '2.4'
|
70
70
|
- !ruby/object:Gem::Dependency
|
71
71
|
name: sqlite3
|
72
72
|
requirement: !ruby/object:Gem::Requirement
|
@@ -87,15 +87,15 @@ dependencies:
|
|
87
87
|
requirements:
|
88
88
|
- - ~>
|
89
89
|
- !ruby/object:Gem::Version
|
90
|
-
version: 3.2
|
90
|
+
version: '3.2'
|
91
91
|
type: :runtime
|
92
92
|
prerelease: false
|
93
93
|
version_requirements: !ruby/object:Gem::Requirement
|
94
94
|
requirements:
|
95
95
|
- - ~>
|
96
96
|
- !ruby/object:Gem::Version
|
97
|
-
version: 3.2
|
98
|
-
description: Define the formats your Rails application should respond to
|
97
|
+
version: '3.2'
|
98
|
+
description: Define the formats your Rails application should respond to in your controllers
|
99
99
|
email:
|
100
100
|
- marcus3006@gmail.com
|
101
101
|
executables: []
|
@@ -103,7 +103,8 @@ extensions: []
|
|
103
103
|
extra_rdoc_files: []
|
104
104
|
files:
|
105
105
|
- .gitignore
|
106
|
-
- .
|
106
|
+
- .ruby-gemset
|
107
|
+
- .ruby-version
|
107
108
|
- Gemfile
|
108
109
|
- LICENSE
|
109
110
|
- README.md
|
data/.rvmrc
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
rvm use ruby-2.0.0-p247@force-format --create
|