Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 6232

Show
Ignore:
Timestamp:
02/25/07 20:13:19 (2 years ago)
Author:
david
Message:

Allow routing requirements on map.resource(s) (closes #7633) [quixoten]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/actionpack/CHANGELOG

    r6226 r6232  
    11*SVN* 
     2 
     3* Allow routing requirements on map.resource(s) #7633 [quixoten]. Example: 
     4 
     5  map.resources :network_interfaces, :requirements => { :id => /^\d+\.\d+\.\d+\.\d+$/ } 
    26 
    37* Cookie session store: empty and unchanged sessions don't write a cookie.  [Jeremy Kemper] 
  • trunk/actionpack/lib/action_controller/resources.rb

    r6113 r6232  
    1212         
    1313        @options = options 
    14  
     14         
    1515        arrange_actions 
    1616        add_default_actions 
     
    2020      def controller 
    2121        @controller ||= (options[:controller] || plural).to_s 
     22      end 
     23       
     24      def requirements(with_id = false) 
     25        @requirements   ||= @options[:requirements] || {} 
     26        @id_requirement ||= { :id => @requirements.delete(:id) || /[^#{Routing::SEPARATORS.join}]+/ } 
     27         
     28        with_id ? @requirements.merge(@id_requirement) : @requirements 
    2229      end 
    2330       
     
    390397      def action_options_for(action, resource, method = nil) 
    391398        default_options = { :action => action.to_s } 
    392         require_id = resource.kind_of?(SingletonResource) ? {} : { :requirements => { :id => Regexp.new("[^#{Routing::SEPARATORS.join}]+") } } 
     399        require_id = !resource.kind_of?(SingletonResource) 
    393400        case default_options[:action] 
    394           when "index", "new" : default_options.merge(conditions_for(method || :get)) 
    395           when "create"       : default_options.merge(conditions_for(method || :post)) 
    396           when "show", "edit" : default_options.merge(conditions_for(method || :get)).merge(require_id
    397           when "update"       : default_options.merge(conditions_for(method || :put)).merge(require_id
    398           when "destroy"      : default_options.merge(conditions_for(method || :delete)).merge(require_id
    399           else                  default_options.merge(conditions_for(method)) 
     401          when "index", "new" : default_options.merge(conditions_for(method || :get)).merge(resource.requirements) 
     402          when "create"       : default_options.merge(conditions_for(method || :post)).merge(resource.requirements) 
     403          when "show", "edit" : default_options.merge(conditions_for(method || :get)).merge(resource.requirements(require_id)
     404          when "update"       : default_options.merge(conditions_for(method || :put)).merge(resource.requirements(require_id)
     405          when "destroy"      : default_options.merge(conditions_for(method || :delete)).merge(resource.requirements(require_id)
     406          else                  default_options.merge(conditions_for(method)).merge(resource.requirements) 
    400407        end 
    401408      end 
  • trunk/actionpack/test/controller/resources_test.rb

    r6062 r6232  
    5555      assert_simply_restful_for :messages 
    5656      assert_simply_restful_for :comments 
     57    end 
     58  end 
     59 
     60  def test_irregular_id_with_no_requirements_should_raise_error 
     61    expected_options = {:controller => 'messages', :action => 'show', :id => '1.1.1'} 
     62     
     63    with_restful_routing :messages do 
     64      assert_raises(ActionController::RoutingError) do 
     65        assert_recognizes(expected_options, :path => 'messages/1.1.1', :method => :get) 
     66      end 
     67    end 
     68  end 
     69   
     70  def test_irregular_id_with_requirements_should_pass 
     71    expected_options = {:controller => 'messages', :action => 'show', :id => '1.1.1'} 
     72     
     73    with_restful_routing(:messages, :requirements => {:id => /[0-9]\.[0-9]\.[0-9]/}) do 
     74      assert_recognizes(expected_options, :path => 'messages/1.1.1', :method => :get) 
     75    end 
     76  end 
     77   
     78  def test_with_path_prefix_requirements 
     79    expected_options = {:controller => 'messages', :action => 'show', :thread_id => '1.1.1', :id => '1'} 
     80    with_restful_routing :messages, :path_prefix => '/thread/:thread_id', :requirements => {:thread_id => /[0-9]\.[0-9]\.[0-9]/} do 
     81      assert_recognizes(expected_options, :path => 'thread/1.1.1/messages/1', :method => :get) 
    5782    end 
    5883  end