Permalink
Browse files

[padrino-core] Controllers accept :conditions option

  • Loading branch information...
bernerdschaefer committed Jun 14, 2011
1 parent 434c4be commit 6e30adf7788071bb1945f91aca034a9e5b3dc950
Showing with 69 additions and 3 deletions.
  1. +28 −2 padrino-core/lib/padrino-core/application/routing.rb
  2. +41 −1 padrino-core/test/test_routing.rb
@@ -207,6 +207,30 @@ module ClassMethods
# end
# end
#
# You can specify conditions to run for all routes:
#
# controller :conditions => {:protect => true} do
# def self.protect(protected)
# condition do
# halt 403, "No secrets for you!" unless params[:key] == "s3cr3t"
# end if protected
# end
#
# # This route will only return "secret stuff" if the user goes to
# # `/private?key=s3cr3t`.
# get("/private") { "secret stuff" }
#
# # And this one, too!
# get("/also-private") { "secret stuff" }
#
# # But you can override the conditions for each route as needed.
# # This route will be publicly accessible without providing the
# # secret key.
# get :index, :protect => false do
# "Welcome!"
# end
# end
#
# You can supply default values:
#
# controller :lang => :de do
@@ -233,6 +257,7 @@ def controller(*args, &block)
@_use_format, original_use_format = options.delete(:use_format), @_use_format
@_cache, original_cache = options.delete(:cache), @_cache
@_map, original_map = options.delete(:map), @_map
@_conditions, original_conditions = options.delete(:conditions), @_conditions
@_defaults, original_defaults = options, @_defaults
# Application defaults
@@ -248,7 +273,7 @@ def controller(*args, &block)
# Controller defaults
@_controller, @_parents, @_cache = original_controller, original_parent, original_cache
@_defaults, @_provides, @_map = original_defaults, original_provides, original_map
@_use_format = original_use_format
@_conditions, @_use_format = original_conditions, original_use_format
else
include(*args) if extensions.any?
end
@@ -431,6 +456,7 @@ def route(verb, path, *args, &block)
route_options[:provides] = @_provides if @_provides
path, *route_options[:with] = path if path.is_a?(Array)
path, name, options = *parse_route(path, route_options, verb)
options.reverse_merge!(@_conditions) if @_conditions
# Sinatra defaults
define_method "#{verb} #{path}", &block
@@ -791,4 +817,4 @@ def route!(base=self.class, pass_block=nil)
end
end # InstanceMethods
end # Routing
end # Padrino
end # Padrino
@@ -1276,6 +1276,46 @@ def authorize(username, password)
assert_equal "foo", body
end
should "pass controller conditions to each route" do
counter = 0
mock_app do
self.class.send(:define_method, :increment!) do |*args|
condition { counter += 1 }
end
controller :posts, :conditions => {:increment! => true} do
get("/foo") { "foo" }
get("/bar") { "bar" }
end
end
get "/posts/foo"
get "/posts/bar"
assert_equal 2, counter
end
should "allow controller conditions to be overridden" do
counter = 0
mock_app do
self.class.send(:define_method, :increment!) do |increment|
condition { counter += 1 } if increment
end
controller :posts, :conditions => {:increment! => true} do
get("/foo") { "foo" }
get("/bar", :increment! => false) { "bar" }
end
end
get "/posts/foo"
get "/posts/bar"
assert_equal 1, counter
end
should "parse params with class level provides" do
mock_app do
controllers :posts, :provides => [:html, :js] do
@@ -1424,4 +1464,4 @@ def authorize(username, password)
get @app.url(:index, :page => 10)
assert_equal "/paginate/66", body
end
end
end

0 comments on commit 6e30adf

Please sign in to comment.