Ticket #6814: add_key_option_to_route_resources.diff
| File add_key_option_to_route_resources.diff, 6.2 kB (added by tsaleh, 3 years ago) |
|---|
-
actionpack/test/controller/resources_test.rb
old new 43 43 end 44 44 end 45 45 46 def test_multi le_with_path_prefix46 def test_multiple_with_path_prefix 47 47 with_restful_routing :messages, :comments, :path_prefix => '/thread/:thread_id' do 48 48 assert_simply_restful_for :messages, :path_prefix => 'thread/5/', :options => { :thread_id => '5' } 49 49 assert_simply_restful_for :comments, :path_prefix => 'thread/5/', :options => { :thread_id => '5' } 50 50 end 51 51 end 52 52 53 def test_multiple_with_key 54 with_restful_routing :messages, :comments, :key => :name do 55 assert_simply_restful_for :messages, :key => :name 56 assert_simply_restful_for :comments, :key => :name 57 end 58 end 59 53 60 def test_with_collection_action 54 61 rss_options = {:action => 'rss'} 55 62 rss_path = "/messages;rss" … … 107 114 end 108 115 end 109 116 110 111 117 def test_with_new_action 112 118 with_restful_routing :messages, :new => { :preview => :post } do 113 119 preview_options = {:action => 'preview'} … … 160 166 end 161 167 end 162 168 169 def test_nested_restful_routes_with_key 170 with_routing do |set| 171 set.draw do |map| 172 map.resources :threads, :key => :thread_keyword do |map| 173 map.resources :messages, :key => :name do |map| 174 map.resources :comments, :key => :title 175 end 176 end 177 end 178 179 assert_simply_restful_for :threads, 180 :key => :thread_keyword 181 assert_simply_restful_for :messages, 182 :key => :name, 183 :path_prefix => 'threads/1/', 184 :options => { :thread_keyword => '1' } 185 assert_simply_restful_for :comments, 186 :key => :title, 187 :path_prefix => 'threads/1/messages/2/', 188 :options => { :thread_keyword => '1', :message_name => '2' } 189 end 190 end 191 163 192 def test_restful_routes_dont_generate_duplicates 164 193 with_restful_routing :messages do 165 194 routes = ActionController::Routing::Routes.routes … … 192 221 collection_path = "/#{options[:path_prefix]}#{controller_name}" 193 222 member_path = "#{collection_path}/1" 194 223 new_path = "#{collection_path}/new" 224 id_key = "#{options[:key] || :id}".to_sym 195 225 196 226 with_options(options[:options]) do |controller| 197 227 controller.assert_routing collection_path, :action => 'index' 198 228 controller.assert_routing "#{collection_path}.xml" , :action => 'index', :format => 'xml' 199 229 controller.assert_routing new_path, :action => 'new' 200 controller.assert_routing member_path, :action => 'show', :id=> '1'201 controller.assert_routing "#{member_path};edit", :action => 'edit', :id=> '1'202 controller.assert_routing "#{member_path}.xml", :action => 'show', :id=> '1', :format => 'xml'230 controller.assert_routing member_path, :action => 'show', id_key => '1' 231 controller.assert_routing "#{member_path};edit", :action => 'edit', id_key => '1' 232 controller.assert_routing "#{member_path}.xml", :action => 'show', id_key => '1', :format => 'xml' 203 233 end 204 234 205 235 assert_recognizes( … … 207 237 :path => collection_path, :method => :post) 208 238 209 239 assert_recognizes( 210 options[:options].merge(:action => 'update', :id=> '1'),240 options[:options].merge(:action => 'update', id_key => '1'), 211 241 :path => member_path, :method => :put) 212 242 213 243 assert_recognizes( 214 options[:options].merge(:action => 'destroy', :id=> '1'),244 options[:options].merge(:action => 'destroy', id_key => '1'), 215 245 :path => member_path, :method => :delete) 216 246 217 247 yield options[:options] if block_given? … … 225 255 end 226 256 singular_name ||= controller_name.to_s.singularize 227 257 (options[:options] ||= {})[:controller] = controller_name.to_s 258 id_key = "#{options[:key] || :id}".to_sym 228 259 @controller = "#{controller_name.to_s.camelize}Controller".constantize.new 229 260 @request = ActionController::TestRequest.new 230 261 @response = ActionController::TestResponse.new … … 236 267 assert_named_route "#{full_prefix}", "#{controller_name}_path", options[:options] 237 268 assert_named_route "#{full_prefix}.xml", "formatted_#{controller_name}_path", options[:options].merge(:format => 'xml') 238 269 assert_named_route "#{full_prefix}/new", "new_#{singular_name}_path", options[:options] 239 assert_named_route "#{full_prefix}/1", "#{singular_name}_path", options[:options].merge( :id=> '1')240 assert_named_route "#{full_prefix}/1;edit", "edit_#{singular_name}_path", options[:options].merge( :id=> '1')241 assert_named_route "#{full_prefix}/1.xml", "formatted_#{singular_name}_path", options[:options].merge(:format => 'xml', :id=> '1')270 assert_named_route "#{full_prefix}/1", "#{singular_name}_path", options[:options].merge(id_key => '1') 271 assert_named_route "#{full_prefix}/1;edit", "edit_#{singular_name}_path", options[:options].merge(id_key => '1') 272 assert_named_route "#{full_prefix}/1.xml", "formatted_#{singular_name}_path", options[:options].merge(:format => 'xml', id_key => '1') 242 273 yield options[:options] if block_given? 243 274 end 244 275 -
actionpack/lib/action_controller/resources.rb
old new 9 9 def initialize(entities, options) 10 10 @plural = entities 11 11 @singular = options[:singular] || plural.to_s.singularize 12 @key = (options[:key] || "id").to_s 12 13 13 14 @options = options 14 15 … … 30 31 end 31 32 32 33 def member_path 33 "#{path}/: id"34 "#{path}/:#{@key}" 34 35 end 35 36 36 37 def nesting_path_prefix 37 "#{path}/:#{singular}_id"38 @nesting_path_prefix ||= ((@key =~ /^#{singular}/) ? "#{path}/:#{@key}" : "#{path}/:#{singular}_#{@key}") 38 39 end 39 40 40 41 private