Skip to content

Commit

Permalink
Adds a force option to run the custom format checks regardless
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Neighman committed Dec 15, 2008
1 parent 7e4369c commit f1d3bd4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
3 changes: 2 additions & 1 deletion lib/merb_custom_formats/router_helper.rb
Expand Up @@ -2,8 +2,9 @@

# Wrap your routes
def custom_formats(*formats, &block)
custom_options_format = extract_options_from_args!(formats) || {}
p = Proc.new do |request, params|
if params[:format]
if params[:format] && !custom_options_format[:force]
params
else
formats_to_run = if formats.empty?
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers.rb
@@ -1,5 +1,5 @@
class MainController < Merb::Controller
provides :iphone, :android, :yaml
provides :iphone, :android, :yaml, :xml
def index
"#{content_type.inspect}"
end
Expand Down
27 changes: 24 additions & 3 deletions spec/merb_custom_formats_spec.rb
Expand Up @@ -110,6 +110,14 @@
match("/foo_for_iphone").register
end

custom_formats(:force => true) do
match("/force_all(.:format)").register
end

custom_formats(:iphone, :force => true) do
match("/force_iphone(.:format)").register
end

match("/foo_with_no_custom_formats").register
end
end
Expand Down Expand Up @@ -144,7 +152,6 @@

it "should check all formats registered with selectors" do
response = request("/foo")
puts Merb::CustomFormats.router_procs.keys.inspect
response.should be_successful
Viking.captures.should include(:iphone)
Viking.captures.should include(:android)
Expand All @@ -153,7 +160,6 @@

it "should set the format to :html if there is nothing matching" do
response = request("/foo")
puts response.body.to_s
response.body.should == ":html"
end

Expand Down Expand Up @@ -184,7 +190,7 @@
end

it "should not run any selectors when the format is already set" do
response = request("/foo.yaml", "User-Agent" => @iphone_ua)
response = request("/foo.yaml", "User-Agent" => @iphone_ua)
response.body.should == ":yaml"
Viking.captures.should_not include(:iphone)
Viking.captures.should_not include(:android)
Expand All @@ -196,6 +202,21 @@
Viking.captures.should_not include(:iphone)
Viking.captures.should_not include(:android)
end

it "should force check the custom formats when a format is set via .format" do
response = request("/force_all.xml")
response.body.should == ":xml"
Viking.captures.should include(:iphone)
Viking.captures.should include(:android)
end

it "should force check the custom formats for a particular format" do
response = request("/force_iphone.xml")
response.body.should == ":xml"
Viking.captures.should include(:iphone)
Viking.captures.should_not include(:android)
end

end

end

0 comments on commit f1d3bd4

Please sign in to comment.