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

Changeset 6729

Show
Ignore:
Timestamp:
05/12/07 21:12:31 (2 years ago)
Author:
david
Message:

Removed deprecated parameters_for_method_reference concept (legacy from before named routes) [DHH] Added record identification with polymorphic routes for ActionController::Base#url_for and ActionView::Base#url_for [DHH]

Files:

Legend:

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

    r6722 r6729  
    11*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] 
    211 
    312* Add ActionController::Routing::Helpers, a module to contain common URL helpers such as polymorphic_url. [Nicholas Seckar] 
     
    26952704* Fixed all helpers so that they use XHTML compliant double quotes for values instead of single quotes [htonl/bitsweat] 
    26962705 
    2697 * Added link_to_image(src, options = {}, html_options = {}, *parameters_for_method_reference). Documentation: 
     2706* Added link_to_image(src, options = {}, html_options = {}). Documentation: 
    26982707 
    26992708    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  
    561561            @url.rewrite(rewrite_options(options)) 
    562562          else 
    563             raise ArgumentError, "Unrecognized url_for options: #{options.inspect}" 
     563            polymorphic_url(options, self) 
    564564        end 
    565565      end 
     
    10161016      # RedirectBackError will be raised. You may specify some fallback 
    10171017      # behavior for this case by rescueing RedirectBackError. 
    1018       def redirect_to(options = {}, *parameters_for_method_reference) #:doc: 
     1018      def redirect_to(options = {}) #:doc: 
    10191019        case options 
    10201020          when %r{^\w+://.*} 
     
    10321032 
    10331033          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 
    10421036        end 
    10431037      end 
  • trunk/actionpack/lib/action_controller/routing.rb

    r6722 r6729  
    11require 'cgi' 
    22require 'uri' 
     3require 'action_controller/polymorphic_routes' 
    34 
    45class Object 
     
    256257    # A helper module to hold URL related helpers. 
    257258    module Helpers 
     259      include PolymorphicRoutes 
    258260    end 
    259261     
  • trunk/actionpack/lib/action_view/helpers/url_helper.rb

    r6674 r6729  
    3030      # * <tt>:escape</tt> -- Determines whether the returned URL will be HTML escaped or not (<tt>true</tt> by default) 
    3131      # 
     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      # 
    3239      # ==== Examples 
    3340      #   <%= url_for(:action => 'index') %> 
     
    4855      #   <%= url_for(:action => 'checkout', :anchor => 'tax&ship', :escape => false) %> 
    4956      #   # => /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 
    5268          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) 
    5476        else 
    55           escape = tru
    56         end 
    57  
    58         url = @controller.send(:url_for, options, *parameters_for_method_reference) 
     77          escape = fals
     78          url    = polymorphic_path(options, self) 
     79        end 
     80 
    5981        escape ? html_escape(url) : url 
    6082      end 
     
    105127      #        var m = document.createElement('input'); m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method');  
    106128      #        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
    108130        if html_options 
    109131          html_options = html_options.stringify_keys 
     
    114136        end 
    115137 
    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
    117139        "<a href=\"#{url}\"#{tag_options}>#{name || url}</a>" 
    118140      end 
     
    223245      #        end  
    224246      #     %> 
    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, &block 
     247      def link_to_unless_current(name, options = {}, html_options = {}, &block) 
     248        link_to_unless current_page?(options), name, options, html_options, &block 
    227249      end 
    228250 
     
    247269      #   # If not... 
    248270      #   # => <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) 
    250272        if condition 
    251273          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
    253275          else 
    254276            name 
    255277          end 
    256278        else 
    257           link_to(name, options, html_options, *parameters_for_method_reference
     279          link_to(name, options, html_options
    258280        end 
    259281      end 
     
    279301      #   # If they are logged in... 
    280302      #   # => <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, &block 
     303      def link_to_if(condition, name, options = {}, html_options = {}, &block) 
     304        link_to_unless !condition, name, options, html_options, &block 
    283305      end 
    284306 
  • trunk/actionpack/test/controller/redirect_test.rb

    r6435 r6729  
    11require File.dirname(__FILE__) + '/../abstract_unit' 
     2 
     3class WorkshopsController < ActionController::Base 
     4end 
     5 
     6class 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 
     20end 
    221 
    322class RedirectController < ActionController::Base 
     
    2140  def redirect_to_back 
    2241    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) 
    2350  end 
    2451 
     
    98125    } 
    99126  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 
    100140end 
    101141 
  • trunk/actionpack/test/template/active_record_helper_test.rb

    r6057 r6729  
    8484 
    8585    @controller = Object.new 
    86     def @controller.url_for(options, *parameters_for_method_reference
     86    def @controller.url_for(options
    8787      options = options.symbolize_keys 
    8888 
  • trunk/actionpack/test/template/asset_tag_helper_test.rb

    r6188 r6729  
    304304      attr_accessor :request 
    305305 
    306       def url_for(options, *parameters_for_method_reference
     306      def url_for(options
    307307        "http://www.example.com/collaboration/hieraki" 
    308308      end 
  • trunk/actionpack/test/template/form_helper_test.rb

    r6113 r6729  
    3939    @controller = Class.new do 
    4040      attr_reader :url_for_options 
    41       def url_for(options, *parameters_for_method_reference
     41      def url_for(options
    4242        @url_for_options = options 
    4343        "http://www.example.com" 
     
    529529    form_for(:post, @post, :url => 'http://www.otherdomain.com') do |f| end 
    530530 
    531     assert_equal 'http://www.otherdomain.com', @controller.url_for_options 
     531    assert_equal '<form action="http://www.otherdomain.com" method="post"></form>', _erbout 
    532532  end 
    533533 
     
    541541  end 
    542542   
     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   
    543551  def test_remote_form_for_with_html_options_adds_options_to_form_tag 
    544552    self.extend ActionView::Helpers::PrototypeHelper 
     
    550558    assert_dom_equal expected, _erbout 
    551559  end 
     560 
     561 
     562  protected 
     563    def polymorphic_path(record, url_writer) 
     564      "/posts/#{record.id}" 
     565    end 
    552566end 
  • trunk/actionpack/test/template/form_tag_helper_test.rb

    r6480 r6729  
    1010  def setup 
    1111    @controller = Class.new do 
    12       def url_for(options, *parameters_for_method_reference
     12      def url_for(options
    1313        "http://www.example.com" 
    1414      end 
     
    4545    form_tag("http://example.com") { _erbout.concat "Hello world!" } 
    4646 
    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>) 
    4848    assert_dom_equal expected, _erbout 
    4949  end 
     
    5353    form_tag("http://example.com", :method => :put) { _erbout.concat "Hello world!" } 
    5454 
    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>) 
    5656    assert_dom_equal expected, _erbout 
    5757  end 
  • trunk/actionpack/test/template/java_script_macros_helper_test.rb

    r6057 r6729  
    1313  def setup 
    1414    @controller = Class.new do 
    15       def url_for(options, *parameters_for_method_reference
     15      def url_for(options
    1616        url =  "http://www.example.com/" 
    1717        url << options[:action].to_s if options and options[:action] 
  • trunk/actionpack/test/template/prototype_helper_test.rb

    r6633 r6729  
    1818    @template = nil 
    1919    @controller = Class.new do 
    20       def url_for(options, *parameters_for_method_reference
     20      def url_for(options
    2121        if options.is_a?(String) 
    2222          options 
  • trunk/actionpack/test/template/scriptaculous_helper_test.rb

    r6057 r6729  
    1414  def setup 
    1515    @controller = Class.new do 
    16       def url_for(options, *parameters_for_method_reference
     16      def url_for(options
    1717        url =  "http://www.example.com/" 
    1818        url << options[:action].to_s if options and options[:action] 
  • trunk/actionpack/test/template/url_helper_test.rb

    r6405 r6729  
    1111    @controller = Class.new do 
    1212      attr_accessor :url, :request 
    13       def url_for(options, *parameters_for_method_reference
     13      def url_for(options
    1414        url 
    1515      end 
     
    169169    assert_dom_equal "<a href=\"http://www.example.com\">Listing</a>", link_to_unless(false, "Listing", :action => "list", :controller => "weblog") 
    170170    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
    172172      "<strong>#{name}</strong>" 
    173173    } 
     
    353353    end 
    354354end 
     355 
     356 
     357class 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 
     371end 
     372 
     373class 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 
     423end