Skip to content
This repository
Browse code

Add CHANGELOG entry and documentation for Routing Concerns

  • Loading branch information...
commit 0bd7b07dff253e0fc2d01644371680bafa0df372 1 parent 546497d
Rafael Mendonça França authored August 10, 2012
30  actionpack/CHANGELOG.md
Source Rendered
... ...
@@ -1,5 +1,35 @@
1 1
 ## Rails 4.0.0 (unreleased) ##
2 2
 
  3
+*   Add Routing Concerns to declare common routes that can be reused inside
  4
+    others resources and routes.
  5
+
  6
+    Code before:
  7
+
  8
+        resources :messages do
  9
+          resources :comments
  10
+        end
  11
+
  12
+        resources :posts do
  13
+          resources :comments
  14
+          resources :images, only: :index
  15
+        end
  16
+
  17
+    Code after:
  18
+
  19
+        concern :commentable do
  20
+          resources :comments
  21
+        end
  22
+
  23
+        concern :image_attachable do
  24
+          resources :images, only: :index
  25
+        end
  26
+
  27
+        resources :messages, concerns: :commentable
  28
+
  29
+        resources :posts, concerns: [:commentable, :image_attachable]
  30
+
  31
+    *David Heinemeier Hansson + Rafael Mendonça França*
  32
+
3 33
 *   Add start_hour and end_hour options to the select_hour helper. *Evan Tann*
4 34
 
5 35
 *   Raises an ArgumentError when the first argument in `form_for` contain `nil`
38  actionpack/lib/action_dispatch/routing/mapper.rb
@@ -1584,11 +1584,49 @@ def name_for_action(as, action) #:nodoc:
1584 1584
           end
1585 1585
       end
1586 1586
 
  1587
+      # Routing Concerns allows you to declare common routes that can be reused
  1588
+      # inside others resources and routes.
  1589
+      #
  1590
+      #   concern :commentable do
  1591
+      #     resources :comments
  1592
+      #   end
  1593
+      #
  1594
+      #   concern :image_attachable do
  1595
+      #     resources :images, only: :index
  1596
+      #   end
  1597
+      #
  1598
+      # These concerns are used in Resources routing:
  1599
+      #
  1600
+      #   resources :messages, concerns: [:commentable, :image_attachable]
  1601
+      #
  1602
+      # or in a scope or namespace:
  1603
+      #
  1604
+      #   namespace :posts do
  1605
+      #     concerns :commentable
  1606
+      #   end
1587 1607
       module Concerns
  1608
+        # Define a routing concern using a name.
  1609
+        #
  1610
+        #   concern :commentable do
  1611
+        #     resources :comments
  1612
+        #   end
  1613
+        #
  1614
+        # Any routing helpers can be used inside a concern.
1588 1615
         def concern(name, &block)
1589 1616
           @concerns[name] = block
1590 1617
         end
1591 1618
 
  1619
+        # Use the named concerns
  1620
+        #
  1621
+        #   resources :posts do
  1622
+        #     concerns :commentable
  1623
+        #   end
  1624
+        #
  1625
+        # concerns also work in any routes helper that you want to use:
  1626
+        #
  1627
+        #   namespace :posts do
  1628
+        #     concerns :commentable
  1629
+        #   end
1592 1630
         def concerns(*names)
1593 1631
           names.flatten.each do |name|
1594 1632
             if concern = @concerns[name]
30  guides/source/routing.textile
Source Rendered
@@ -273,6 +273,36 @@ The corresponding route helper would be +publisher_magazine_photo_url+, requirin
273 273
 
274 274
 TIP: _Resources should never be nested more than 1 level deep._
275 275
 
  276
+h4. Routing concerns
  277
+
  278
+Routing Concerns allows you to declare common routes that can be reused inside others resources and routes.
  279
+
  280
+<ruby>
  281
+concern :commentable do
  282
+  resources :comments
  283
+end
  284
+
  285
+concern :image_attachable do
  286
+  resources :images, only: :index
  287
+end
  288
+</ruby>
  289
+
  290
+These concerns can be used in resources to avoid code duplication and share behavior across routes.
  291
+
  292
+<ruby>
  293
+resources :messages, concerns: :commentable
  294
+
  295
+resources :posts, concerns: [:commentable, :image_attachable]
  296
+</ruby>
  297
+
  298
+Also you can use them in any place that you want inside the routes, for example in a scope or namespace call:
  299
+
  300
+<ruby>
  301
+namespace :posts do
  302
+  concerns :commentable
  303
+end
  304
+</ruby>
  305
+
276 306
 h4. Creating Paths and URLs From Objects
277 307
 
278 308
 In addition to using the routing helpers, Rails can also create paths and URLs from an array of parameters. For example, suppose you have this set of routes:

1 note on commit 0bd7b07

Vijay Dev
Collaborator

@rafaelfranca some release notes love please :)

Please sign in to comment.
Something went wrong with that request. Please try again.