diff options
author | Andrew White <andyw@pixeltrix.co.uk> | 2010-03-28 11:10:38 +0100 |
---|---|---|
committer | Rizwan Reza <rizwanreza@gmail.com> | 2010-03-28 23:31:13 +0430 |
commit | e1a70faea675499d717cef7662262ceb03b23975 (patch) | |
tree | 70e1e307e8cf6e8d94aa628096033491b9ead72b /actionpack/lib | |
parent | 5c058295d1e43ea88dd4161592844263a12f02ad (diff) | |
download | rails-e1a70faea675499d717cef7662262ceb03b23975.tar.gz rails-e1a70faea675499d717cef7662262ceb03b23975.tar.bz2 rails-e1a70faea675499d717cef7662262ceb03b23975.zip |
Add constraints to resources in new routing DSL
Signed-off-by: Rizwan Reza <rizwanreza@gmail.com>
Diffstat (limited to 'actionpack/lib')
-rw-r--r-- | actionpack/lib/action_dispatch/routing/mapper.rb | 61 |
1 files changed, 47 insertions, 14 deletions
diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index 26f0e14e72..2546bdd43e 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -259,6 +259,7 @@ module ActionDispatch def scope(*args) options = args.extract_options! + options = options.dup case args.first when String @@ -440,6 +441,32 @@ module ActionDispatch def id_segment ":#{singular}_id" end + + def constraints + options[:constraints] || {} + end + + def id_constraint? + options[:id] && options[:id].is_a?(Regexp) || constraints[:id] && constraints[:id].is_a?(Regexp) + end + + def id_constraint + options[:id] || constraints[:id] + end + + def collection_options + (options || {}).dup.tap do |options| + options.delete(:id) + options[:constraints] = options[:constraints].dup if options[:constraints] + options[:constraints].delete(:id) if options[:constraints].is_a?(Hash) + end + end + + def nested_options + options = { :name_prefix => member_name } + options["#{singular}_id".to_sym] = id_constraint if id_constraint? + options + end end class SingletonResource < Resource #:nodoc: @@ -484,12 +511,14 @@ module ActionDispatch yield if block_given? end - get :show if resource.actions.include?(:show) - post :create if resource.actions.include?(:create) - put :update if resource.actions.include?(:update) - delete :destroy if resource.actions.include?(:destroy) - get :new, :as => resource.name if resource.actions.include?(:new) - get :edit, :as => resource.name if resource.actions.include?(:edit) + scope(resource.options) do + get :show if resource.actions.include?(:show) + post :create if resource.actions.include?(:create) + put :update if resource.actions.include?(:update) + delete :destroy if resource.actions.include?(:destroy) + get :new, :as => resource.name if resource.actions.include?(:new) + get :edit, :as => resource.name if resource.actions.include?(:edit) + end end end @@ -510,17 +539,21 @@ module ActionDispatch yield if block_given? with_scope_level(:collection) do - get :index if resource.actions.include?(:index) - post :create if resource.actions.include?(:create) - get :new, :as => resource.singular if resource.actions.include?(:new) + scope(resource.collection_options) do + get :index if resource.actions.include?(:index) + post :create if resource.actions.include?(:create) + get :new, :as => resource.singular if resource.actions.include?(:new) + end end with_scope_level(:member) do scope(':id') do - get :show if resource.actions.include?(:show) - put :update if resource.actions.include?(:update) - delete :destroy if resource.actions.include?(:destroy) - get :edit, :as => resource.singular if resource.actions.include?(:edit) + scope(resource.options) do + get :show if resource.actions.include?(:show) + put :update if resource.actions.include?(:update) + delete :destroy if resource.actions.include?(:destroy) + get :edit, :as => resource.singular if resource.actions.include?(:edit) + end end end end @@ -559,7 +592,7 @@ module ActionDispatch end with_scope_level(:nested) do - scope(parent_resource.id_segment, :name_prefix => parent_resource.member_name) do + scope(parent_resource.id_segment, parent_resource.nested_options) do yield end end |