~michaelforrest/use-case-mapper/trunk

« back to all changes in this revision

Viewing changes to vendor/rails/actionpack/lib/action_controller/polymorphic_routes.rb

  • Committer: Richard Lee (Canonical)
  • Date: 2010-10-15 15:17:58 UTC
  • mfrom: (190.1.3 use-case-mapper)
  • Revision ID: richard.lee@canonical.com-20101015151758-wcvmfxrexsongf9d
Merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
module ActionController
2
 
  # Polymorphic URL helpers are methods for smart resolution to a named route call when
3
 
  # given an Active Record model instance. They are to be used in combination with
4
 
  # ActionController::Resources.
5
 
  #
6
 
  # These methods are useful when you want to generate correct URL or path to a RESTful
7
 
  # resource without having to know the exact type of the record in question.
8
 
  #
9
 
  # Nested resources and/or namespaces are also supported, as illustrated in the example:
10
 
  #
11
 
  #   polymorphic_url([:admin, @article, @comment])
12
 
  #
13
 
  # results in:
14
 
  #   
15
 
  #   admin_article_comment_url(@article, @comment)
16
 
  #
17
 
  # == Usage within the framework
18
 
  #
19
 
  # Polymorphic URL helpers are used in a number of places throughout the Rails framework:
20
 
  #
21
 
  # * <tt>url_for</tt>, so you can use it with a record as the argument, e.g.
22
 
  #   <tt>url_for(@article)</tt>;
23
 
  # * ActionView::Helpers::FormHelper uses <tt>polymorphic_path</tt>, so you can write
24
 
  #   <tt>form_for(@article)</tt> without having to specify <tt>:url</tt> parameter for the form
25
 
  #   action;
26
 
  # * <tt>redirect_to</tt> (which, in fact, uses <tt>url_for</tt>) so you can write
27
 
  #   <tt>redirect_to(post)</tt> in your controllers;
28
 
  # * ActionView::Helpers::AtomFeedHelper, so you don't have to explicitly specify URLs
29
 
  #   for feed entries.
30
 
  #
31
 
  # == Prefixed polymorphic helpers
32
 
  #
33
 
  # In addition to <tt>polymorphic_url</tt> and <tt>polymorphic_path</tt> methods, a
34
 
  # number of prefixed helpers are available as a shorthand to <tt>:action => "..."</tt>
35
 
  # in options. Those are:
36
 
  #
37
 
  # * <tt>edit_polymorphic_url</tt>, <tt>edit_polymorphic_path</tt>
38
 
  # * <tt>new_polymorphic_url</tt>, <tt>new_polymorphic_path</tt>
39
 
  #
40
 
  # Example usage:
41
 
  #
42
 
  #   edit_polymorphic_path(@post)              # => "/posts/1/edit"
43
 
  #   polymorphic_path(@post, :format => :pdf)  # => "/posts/1.pdf"
44
 
  module PolymorphicRoutes
45
 
    # Constructs a call to a named RESTful route for the given record and returns the
46
 
    # resulting URL string. For example:
47
 
    #
48
 
    #   # calls post_url(post)
49
 
    #   polymorphic_url(post) # => "http://example.com/posts/1"
50
 
    #   polymorphic_url([blog, post]) # => "http://example.com/blogs/1/posts/1"
51
 
    #   polymorphic_url([:admin, blog, post]) # => "http://example.com/admin/blogs/1/posts/1"
52
 
    #   polymorphic_url([user, :blog, post]) # => "http://example.com/users/1/blog/posts/1"
53
 
    #
54
 
    # ==== Options
55
 
    #
56
 
    # * <tt>:action</tt> - Specifies the action prefix for the named route:
57
 
    #   <tt>:new</tt> or <tt>:edit</tt>. Default is no prefix.
58
 
    # * <tt>:routing_type</tt> - Allowed values are <tt>:path</tt> or <tt>:url</tt>.
59
 
    #   Default is <tt>:url</tt>.
60
 
    #
61
 
    # ==== Examples
62
 
    #
63
 
    #   # an Article record
64
 
    #   polymorphic_url(record)  # same as article_url(record)
65
 
    #
66
 
    #   # a Comment record
67
 
    #   polymorphic_url(record)  # same as comment_url(record)
68
 
    #
69
 
    #   # it recognizes new records and maps to the collection
70
 
    #   record = Comment.new
71
 
    #   polymorphic_url(record)  # same as comments_url()
72
 
    #
73
 
    def polymorphic_url(record_or_hash_or_array, options = {})
74
 
      if record_or_hash_or_array.kind_of?(Array)
75
 
        record_or_hash_or_array = record_or_hash_or_array.compact
76
 
        record_or_hash_or_array = record_or_hash_or_array[0] if record_or_hash_or_array.size == 1
77
 
      end
78
 
 
79
 
      record = extract_record(record_or_hash_or_array)
80
 
 
81
 
      args = case record_or_hash_or_array
82
 
        when Hash;  [ record_or_hash_or_array ]
83
 
        when Array; record_or_hash_or_array.dup
84
 
        else        [ record_or_hash_or_array ]
85
 
      end
86
 
 
87
 
      inflection =
88
 
        case
89
 
        when options[:action].to_s == "new"
90
 
          args.pop
91
 
          :singular
92
 
        when record.respond_to?(:new_record?) && record.new_record?
93
 
          args.pop
94
 
          :plural
95
 
        else
96
 
          :singular
97
 
        end
98
 
 
99
 
      args.delete_if {|arg| arg.is_a?(Symbol) || arg.is_a?(String)}
100
 
      named_route = build_named_route_call(record_or_hash_or_array, inflection, options)
101
 
 
102
 
      url_options = options.except(:action, :routing_type)
103
 
      unless url_options.empty?
104
 
        args.last.kind_of?(Hash) ? args.last.merge!(url_options) : args << url_options
105
 
      end
106
 
 
107
 
      __send__(named_route, *args)
108
 
    end
109
 
 
110
 
    # Returns the path component of a URL for the given record. It uses
111
 
    # <tt>polymorphic_url</tt> with <tt>:routing_type => :path</tt>.
112
 
    def polymorphic_path(record_or_hash_or_array, options = {})
113
 
      options[:routing_type] = :path
114
 
      polymorphic_url(record_or_hash_or_array, options)
115
 
    end
116
 
 
117
 
    %w(edit new).each do |action|
118
 
      module_eval <<-EOT, __FILE__, __LINE__
119
 
        def #{action}_polymorphic_url(record_or_hash, options = {})         # def edit_polymorphic_url(record_or_hash, options = {})
120
 
          polymorphic_url(                                                  #   polymorphic_url(
121
 
            record_or_hash,                                                 #     record_or_hash,
122
 
            options.merge(:action => "#{action}"))                          #     options.merge(:action => "edit"))
123
 
        end                                                                 # end
124
 
                                                                            #
125
 
        def #{action}_polymorphic_path(record_or_hash, options = {})        # def edit_polymorphic_path(record_or_hash, options = {})
126
 
          polymorphic_url(                                                  #   polymorphic_url(
127
 
            record_or_hash,                                                 #     record_or_hash,
128
 
            options.merge(:action => "#{action}", :routing_type => :path))  #     options.merge(:action => "edit", :routing_type => :path))
129
 
        end                                                                 # end
130
 
      EOT
131
 
    end
132
 
 
133
 
    def formatted_polymorphic_url(record_or_hash, options = {})
134
 
      ActiveSupport::Deprecation.warn("formatted_polymorphic_url has been deprecated. Please pass :format to the polymorphic_url method instead", caller)
135
 
      options[:format] = record_or_hash.pop if Array === record_or_hash
136
 
      polymorphic_url(record_or_hash, options)
137
 
    end
138
 
 
139
 
    def formatted_polymorphic_path(record_or_hash, options = {})
140
 
      ActiveSupport::Deprecation.warn("formatted_polymorphic_path has been deprecated. Please pass :format to the polymorphic_path method instead", caller)
141
 
      options[:format] = record_or_hash.pop if record_or_hash === Array
142
 
      polymorphic_url(record_or_hash, options.merge(:routing_type => :path))
143
 
    end
144
 
 
145
 
    private
146
 
      def action_prefix(options)
147
 
        options[:action] ? "#{options[:action]}_" : ''
148
 
      end
149
 
 
150
 
      def routing_type(options)
151
 
        options[:routing_type] || :url
152
 
      end
153
 
 
154
 
      def build_named_route_call(records, inflection, options = {})
155
 
        unless records.is_a?(Array)
156
 
          record = extract_record(records)
157
 
          route  = ''
158
 
        else
159
 
          record = records.pop
160
 
          route = records.inject("") do |string, parent|
161
 
            if parent.is_a?(Symbol) || parent.is_a?(String)
162
 
              string << "#{parent}_"
163
 
            else
164
 
              string << RecordIdentifier.__send__("plural_class_name", parent).singularize
165
 
              string << "_"
166
 
            end
167
 
          end
168
 
        end
169
 
 
170
 
        if record.is_a?(Symbol) || record.is_a?(String)
171
 
          route << "#{record}_"
172
 
        else
173
 
          route << RecordIdentifier.__send__("plural_class_name", record)
174
 
          route = route.singularize if inflection == :singular
175
 
          route << "_"
176
 
        end
177
 
 
178
 
        action_prefix(options) + route + routing_type(options).to_s
179
 
      end
180
 
 
181
 
      def extract_record(record_or_hash_or_array)
182
 
        case record_or_hash_or_array
183
 
          when Array; record_or_hash_or_array.last
184
 
          when Hash;  record_or_hash_or_array[:id]
185
 
          else        record_or_hash_or_array
186
 
        end
187
 
      end
188
 
  end
189
 
end