Changeset 6729
- Timestamp:
- 05/12/07 21:12:31 (1 year ago)
- Files:
-
- trunk/actionpack/CHANGELOG (modified) (2 diffs)
- trunk/actionpack/lib/action_controller/base.rb (modified) (3 diffs)
- trunk/actionpack/lib/action_controller/polymorphic_routes.rb (added)
- trunk/actionpack/lib/action_controller/routing.rb (modified) (2 diffs)
- trunk/actionpack/lib/action_view/helpers/url_helper.rb (modified) (7 diffs)
- trunk/actionpack/test/controller/redirect_test.rb (modified) (3 diffs)
- trunk/actionpack/test/template/active_record_helper_test.rb (modified) (1 diff)
- trunk/actionpack/test/template/asset_tag_helper_test.rb (modified) (1 diff)
- trunk/actionpack/test/template/form_helper_test.rb (modified) (4 diffs)
- trunk/actionpack/test/template/form_tag_helper_test.rb (modified) (3 diffs)
- trunk/actionpack/test/template/java_script_macros_helper_test.rb (modified) (1 diff)
- trunk/actionpack/test/template/prototype_helper_test.rb (modified) (1 diff)
- trunk/actionpack/test/template/scriptaculous_helper_test.rb (modified) (1 diff)
- trunk/actionpack/test/template/url_helper_test.rb (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/actionpack/CHANGELOG
r6722 r6729 1 1 *SVN* 2 3 * Added record identification with polymorphic routes for ActionController::Base#url_for and ActionView::Base#url_for [DHH]. Examples: 4 5 redirect_to(post) # => redirect_to(posts_url(post)) => Location: http://example.com/posts/1 6 link_to(post.title, post) # => link_to(post.title, posts_url(post)) => <a href="/posts/1">Hello world</a> 7 8 Any method that calls url_for on its parameters will automatically benefit from this. 9 10 * Removed deprecated parameters_for_method_reference concept (legacy from before named routes) [DHH] 2 11 3 12 * Add ActionController::Routing::Helpers, a module to contain common URL helpers such as polymorphic_url. [Nicholas Seckar] … … 2695 2704 * Fixed all helpers so that they use XHTML compliant double quotes for values instead of single quotes [htonl/bitsweat] 2696 2705 2697 * Added link_to_image(src, options = {}, html_options = {} , *parameters_for_method_reference). Documentation:2706 * Added link_to_image(src, options = {}, html_options = {}). Documentation: 2698 2707 2699 2708 Creates a link tag to the image residing at the +src+ using an URL created by the set of +options+. See the valid options in trunk/actionpack/lib/action_controller/base.rb
r6574 r6729 561 561 @url.rewrite(rewrite_options(options)) 562 562 else 563 raise ArgumentError, "Unrecognized url_for options: #{options.inspect}"563 polymorphic_url(options, self) 564 564 end 565 565 end … … 1016 1016 # RedirectBackError will be raised. You may specify some fallback 1017 1017 # behavior for this case by rescueing RedirectBackError. 1018 def redirect_to(options = {} , *parameters_for_method_reference) #:doc:1018 def redirect_to(options = {}) #:doc: 1019 1019 case options 1020 1020 when %r{^\w+://.*} … … 1032 1032 1033 1033 else 1034 if parameters_for_method_reference.empty? 1035 redirect_to(url_for(options)) 1036 response.redirected_to = options 1037 else 1038 # TOOD: Deprecate me! 1039 redirect_to(url_for(options, *parameters_for_method_reference)) 1040 response.redirected_to, response.redirected_to_method_params = options, parameters_for_method_reference 1041 end 1034 redirect_to(url_for(options)) 1035 response.redirected_to = options 1042 1036 end 1043 1037 end trunk/actionpack/lib/action_controller/routing.rb
r6722 r6729 1 1 require 'cgi' 2 2 require 'uri' 3 require 'action_controller/polymorphic_routes' 3 4 4 5 class Object … … 256 257 # A helper module to hold URL related helpers. 257 258 module Helpers 259 include PolymorphicRoutes 258 260 end 259 261 trunk/actionpack/lib/action_view/helpers/url_helper.rb
r6674 r6729 30 30 # * <tt>:escape</tt> -- Determines whether the returned URL will be HTML escaped or not (<tt>true</tt> by default) 31 31 # 32 # ==== Relying on named routes 33 # 34 # If you instead of a hash pass a record (like an Active Record or Active Resource) as the options parameter, 35 # you'll trigger the named route for that record. The lookup will happen on the name of the class. So passing 36 # a Workshop object will attempt to use the workshop_path route. If you have a nested route, such as 37 # admin_workshop_path you'll have to call that explicitly (it's impossible for url_for to guess that route). 38 # 32 39 # ==== Examples 33 40 # <%= url_for(:action => 'index') %> … … 48 55 # <%= url_for(:action => 'checkout', :anchor => 'tax&ship', :escape => false) %> 49 56 # # => /testing/jump/#tax&ship 50 def url_for(options = {}, *parameters_for_method_reference) 51 if options.kind_of? Hash 57 # 58 # <%= url_for(Workshop.new) %> 59 # # relies on Workshop answering a new_record? call (and in this case returning true) 60 # # => /workshops 61 # 62 # <%= url_for(@workshop) %> 63 # # calls @workshop.to_s 64 # # => /workshops/5 65 def url_for(options = {}) 66 case options 67 when Hash 52 68 options = { :only_path => true }.update(options.symbolize_keys) 53 escape = options.key?(:escape) ? options.delete(:escape) : true 69 escape = options.key?(:escape) ? options.delete(:escape) : true 70 url = @controller.send(:url_for, options) 71 when String 72 escape = true 73 url = options 74 when NilClass 75 url = @controller.send(:url_for, nil) 54 76 else 55 escape = true56 end57 58 url = @controller.send(:url_for, options, *parameters_for_method_reference) 77 escape = false 78 url = polymorphic_path(options, self) 79 end 80 59 81 escape ? html_escape(url) : url 60 82 end … … 105 127 # var m = document.createElement('input'); m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method'); 106 128 # m.setAttribute('value', 'delete'); f.appendChild(m);f.submit(); };return false;">Delete Image</a> 107 def link_to(name, options = {}, html_options = nil , *parameters_for_method_reference)129 def link_to(name, options = {}, html_options = nil) 108 130 if html_options 109 131 html_options = html_options.stringify_keys … … 114 136 end 115 137 116 url = options.is_a?(String) ? options : self.url_for(options , *parameters_for_method_reference)138 url = options.is_a?(String) ? options : self.url_for(options) 117 139 "<a href=\"#{url}\"#{tag_options}>#{name || url}</a>" 118 140 end … … 223 245 # end 224 246 # %> 225 def link_to_unless_current(name, options = {}, html_options = {}, *parameters_for_method_reference,&block)226 link_to_unless current_page?(options), name, options, html_options, *parameters_for_method_reference,&block247 def link_to_unless_current(name, options = {}, html_options = {}, &block) 248 link_to_unless current_page?(options), name, options, html_options, &block 227 249 end 228 250 … … 247 269 # # If not... 248 270 # # => <a href="/accounts/signup">Reply</a> 249 def link_to_unless(condition, name, options = {}, html_options = {}, *parameters_for_method_reference,&block)271 def link_to_unless(condition, name, options = {}, html_options = {}, &block) 250 272 if condition 251 273 if block_given? 252 block.arity <= 1 ? yield(name) : yield(name, options, html_options , *parameters_for_method_reference)274 block.arity <= 1 ? yield(name) : yield(name, options, html_options) 253 275 else 254 276 name 255 277 end 256 278 else 257 link_to(name, options, html_options , *parameters_for_method_reference)279 link_to(name, options, html_options) 258 280 end 259 281 end … … 279 301 # # If they are logged in... 280 302 # # => <a href="/accounts/show/3">my_username</a> 281 def link_to_if(condition, name, options = {}, html_options = {}, *parameters_for_method_reference,&block)282 link_to_unless !condition, name, options, html_options, *parameters_for_method_reference,&block303 def link_to_if(condition, name, options = {}, html_options = {}, &block) 304 link_to_unless !condition, name, options, html_options, &block 283 305 end 284 306 trunk/actionpack/test/controller/redirect_test.rb
r6435 r6729 1 1 require File.dirname(__FILE__) + '/../abstract_unit' 2 3 class WorkshopsController < ActionController::Base 4 end 5 6 class Workshop 7 attr_accessor :id, :new_record 8 9 def initialize(id, new_record) 10 @id, @new_record = id, new_record 11 end 12 13 def new_record? 14 @new_record 15 end 16 17 def to_s 18 id.to_s 19 end 20 end 2 21 3 22 class RedirectController < ActionController::Base … … 21 40 def redirect_to_back 22 41 redirect_to :back 42 end 43 44 def redirect_to_existing_record 45 redirect_to Workshop.new(5, false) 46 end 47 48 def redirect_to_new_record 49 redirect_to Workshop.new(5, true) 23 50 end 24 51 … … 98 125 } 99 126 end 127 128 def test_redirect_to_record 129 ActionController::Routing::Routes.draw do |map| 130 map.resources :workshops 131 map.connect ':controller/:action/:id' 132 end 133 134 get :redirect_to_existing_record 135 assert_equal "http://test.host/workshops/5", redirect_to_url 136 137 get :redirect_to_new_record 138 assert_equal "http://test.host/workshops", redirect_to_url 139 end 100 140 end 101 141 trunk/actionpack/test/template/active_record_helper_test.rb
r6057 r6729 84 84 85 85 @controller = Object.new 86 def @controller.url_for(options , *parameters_for_method_reference)86 def @controller.url_for(options) 87 87 options = options.symbolize_keys 88 88 trunk/actionpack/test/template/asset_tag_helper_test.rb
r6188 r6729 304 304 attr_accessor :request 305 305 306 def url_for(options , *parameters_for_method_reference)306 def url_for(options) 307 307 "http://www.example.com/collaboration/hieraki" 308 308 end trunk/actionpack/test/template/form_helper_test.rb
r6113 r6729 39 39 @controller = Class.new do 40 40 attr_reader :url_for_options 41 def url_for(options , *parameters_for_method_reference)41 def url_for(options) 42 42 @url_for_options = options 43 43 "http://www.example.com" … … 529 529 form_for(:post, @post, :url => 'http://www.otherdomain.com') do |f| end 530 530 531 assert_equal ' http://www.otherdomain.com', @controller.url_for_options531 assert_equal '<form action="http://www.otherdomain.com" method="post"></form>', _erbout 532 532 end 533 533 … … 541 541 end 542 542 543 def test_form_for_with_record_url_option 544 _erbout = '' 545 546 form_for(:post, @post, :url => @post) do |f| end 547 548 expected = "<form action=\"/posts/123\" method=\"post\"></form>" 549 end 550 543 551 def test_remote_form_for_with_html_options_adds_options_to_form_tag 544 552 self.extend ActionView::Helpers::PrototypeHelper … … 550 558 assert_dom_equal expected, _erbout 551 559 end 560 561 562 protected 563 def polymorphic_path(record, url_writer) 564 "/posts/#{record.id}" 565 end 552 566 end trunk/actionpack/test/template/form_tag_helper_test.rb
r6480 r6729 10 10 def setup 11 11 @controller = Class.new do 12 def url_for(options , *parameters_for_method_reference)12 def url_for(options) 13 13 "http://www.example.com" 14 14 end … … 45 45 form_tag("http://example.com") { _erbout.concat "Hello world!" } 46 46 47 expected = %(<form action="http:// www.example.com" method="post">Hello world!</form>)47 expected = %(<form action="http://example.com" method="post">Hello world!</form>) 48 48 assert_dom_equal expected, _erbout 49 49 end … … 53 53 form_tag("http://example.com", :method => :put) { _erbout.concat "Hello world!" } 54 54 55 expected = %(<form action="http:// www.example.com" method="post"><div style='margin:0;padding:0'><input type="hidden" name="_method" value="put" /></div>Hello world!</form>)55 expected = %(<form action="http://example.com" method="post"><div style='margin:0;padding:0'><input type="hidden" name="_method" value="put" /></div>Hello world!</form>) 56 56 assert_dom_equal expected, _erbout 57 57 end trunk/actionpack/test/template/java_script_macros_helper_test.rb
r6057 r6729 13 13 def setup 14 14 @controller = Class.new do 15 def url_for(options , *parameters_for_method_reference)15 def url_for(options) 16 16 url = "http://www.example.com/" 17 17 url << options[:action].to_s if options and options[:action] trunk/actionpack/test/template/prototype_helper_test.rb
r6633 r6729 18 18 @template = nil 19 19 @controller = Class.new do 20 def url_for(options , *parameters_for_method_reference)20 def url_for(options) 21 21 if options.is_a?(String) 22 22 options trunk/actionpack/test/template/scriptaculous_helper_test.rb
r6057 r6729 14 14 def setup 15 15 @controller = Class.new do 16 def url_for(options , *parameters_for_method_reference)16 def url_for(options) 17 17 url = "http://www.example.com/" 18 18 url << options[:action].to_s if options and options[:action] trunk/actionpack/test/template/url_helper_test.rb
r6405 r6729 11 11 @controller = Class.new do 12 12 attr_accessor :url, :request 13 def url_for(options , *parameters_for_method_reference)13 def url_for(options) 14 14 url 15 15 end … … 169 169 assert_dom_equal "<a href=\"http://www.example.com\">Listing</a>", link_to_unless(false, "Listing", :action => "list", :controller => "weblog") 170 170 assert_equal "Showing", link_to_unless(true, "Showing", :action => "show", :controller => "weblog", :id => 1) 171 assert_equal "<strong>Showing</strong>", link_to_unless(true, "Showing", :action => "show", :controller => "weblog", :id => 1) { |name, options, html_options , *parameters_for_method_reference|171 assert_equal "<strong>Showing</strong>", link_to_unless(true, "Showing", :action => "show", :controller => "weblog", :id => 1) { |name, options, html_options| 172 172 "<strong>#{name}</strong>" 173 173 } … … 353 353 end 354 354 end 355 356 357 class Workshop 358 attr_accessor :id, :new_record 359 360 def initialize(id, new_record) 361 @id, @new_record = id, new_record 362 end 363 364 def new_record? 365 @new_record 366 end 367 368 def to_s 369 id.to_s 370 end 371 end 372 373 class PolymorphicControllerTest < Test::Unit::TestCase 374 class WorkshopsController < ActionController::Base 375 self.view_paths = ["#{File.dirname(__FILE__)}/../fixtures/"] 376 377 def self.controller_path; 'workshops' end 378 379 def index 380 @workshop = Workshop.new(1, true) 381 render :inline => "<%= url_for(@workshop) %>\n<%= link_to('Workshop', @workshop) %>" 382 end 383 384 def show 385 @workshop = Workshop.new(params[:id], false) 386 render :inline => "<%= url_for(@workshop) %>\n<%= link_to('Workshop', @workshop) %>" 387 end 388 389 def rescue_action(e) raise e end 390 end 391 392 include ActionView::Helpers::UrlHelper 393 394 def setup 395 @request = ActionController::TestRequest.new 396 @response = ActionController::TestResponse.new 397 @controller = WorkshopsController.new 398 end 399 400 def test_new_resource 401 with_restful_routing do 402 get :index 403 assert_equal "/workshops\n<a href=\"/workshops\">Workshop</a>", @response.body 404 end 405 end 406 407 def test_existing_resource 408 with_restful_routing do 409 get :show, :id => 1 410 assert_equal "/workshops/1\n<a href=\"/workshops/1\">Workshop</a>", @response.body 411 end 412 end 413 414 protected 415 def with_restful_routing 416 with_routing do |set| 417 set.draw do |map| 418 map.resources :workshops 419 end 420 yield 421 end 422 end 423 end