Skip to content
This repository
Browse code

resource and scaffold_resource generators add a restful route to conf…

…ig/routes.rb

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5379 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit c5dcec789e5513ef1cfd8ef468cd2a46cdaf01f5 1 parent 98a9b65
Jeremy Kemper authored
2  railties/CHANGELOG
... ...
@@ -1,5 +1,7 @@
1 1
 *SVN*
2 2
 
  3
+* resource and scaffold_resource generators add a restful route to config/routes.rb  [Jeremy Kemper]
  4
+
3 5
 * Revert environment changes for autoload_paths. [Koz]
4 6
 
5 7
 * Update to latest Prototype, which doesn't serialize disabled form elements, adds clone() to arrays, empty/non-string Element.update() and adds a fixes excessive error reporting in WebKit beta versions [Thomas Fuchs]
28  railties/lib/rails_generator/commands.rb
@@ -83,6 +83,12 @@ def next_migration_string(padding = 3)
83 83
             "%.#{padding}d" % next_migration_number
84 84
           end
85 85
 
  86
+          def gsub_file(relative_destination, regexp, *args, &block)
  87
+            path = destination_path(relative_destination)
  88
+            content = File.read(path).gsub(regexp, *args, &block)
  89
+            File.open(path, 'wb') { |file| file.write(content) }
  90
+          end
  91
+
86 92
         private
87 93
           # Ask the user interactively whether to force collision.
88 94
           def force_file_collision?(destination)
@@ -316,6 +322,16 @@ def migration_template(relative_source, relative_destination, template_options =
316 322
           template(relative_source, "#{relative_destination}/#{next_migration_string}_#{migration_file_name}.rb", template_options)
317 323
         end
318 324
 
  325
+        def route_resources(*resources)
  326
+          resource_list = resources.map { |r| r.to_sym.inspect }.join(', ')
  327
+          sentinel = 'ActionController::Routing::Routes.draw do |map|'
  328
+
  329
+          logger.route "map.resources #{resource_list}"
  330
+          gsub_file 'config/routes.rb', /(#{Regexp.escape(sentinel)})/mi do |match|
  331
+            "#{match}\n  map.resources #{resource_list}\n"
  332
+          end
  333
+        end
  334
+
319 335
         private
320 336
           # Raise a usage error with an informative WordNet suggestion.
321 337
           # Thanks to Florian Gross (flgr).
@@ -438,6 +454,13 @@ def migration_template(relative_source, relative_destination, template_options =
438 454
             file(relative_source, file_path, template_options)
439 455
           end
440 456
         end
  457
+
  458
+        def route_resources(*resources)
  459
+          resource_list = resources.map { |r| r.to_sym.inspect }.join(', ')
  460
+          look_for = "\n  map.resources #{resource_list}\n"
  461
+          logger.route "map.resources #{resource_list}"
  462
+          gsub_file 'config/routes.rb', /(#{look_for})/mi, ''
  463
+        end
441 464
       end
442 465
 
443 466
 
@@ -475,6 +498,11 @@ def migration_template(relative_source, relative_destination, options = {})
475 498
           migration_directory relative_destination
476 499
           logger.migration_template file_name
477 500
         end
  501
+
  502
+        def route_resources(*resources)
  503
+          resource_list = resources.map { |r| r.to_sym.inspect }.join(', ')
  504
+          logger.route "map.resources #{resource_list}"
  505
+        end
478 506
       end
479 507
 
480 508
       # Update generator's action manifest.
15  railties/lib/rails_generator/generators/components/resource/resource_generator.rb
@@ -26,7 +26,7 @@ def initialize(runtime_args, runtime_options = {})
26 26
   end
27 27
 
28 28
   def manifest
29  
-    recorded_session = record do |m|
  29
+    record do |m|
30 30
       # Check for class naming collisions.
31 31
       m.class_collisions(controller_class_path, "#{controller_class_name}Controller", "#{controller_class_name}Helper")
32 32
       m.class_collisions(class_path, "#{class_name}")
@@ -60,18 +60,9 @@ def manifest
60 60
           :migration_file_name => "create_#{file_path.gsub(/\//, '_').pluralize}"
61 61
         )
62 62
       end
63  
-    end
64  
-
65  
-    puts
66  
-    puts ("-" * 70)
67  
-    puts "Don't forget the restful route in config/routes.rb"
68  
-    puts
69  
-    puts "  map.resources :#{controller_file_name}"
70  
-    puts
71  
-    puts ("-" * 70)
72  
-    puts
73 63
 
74  
-    recorded_session
  64
+      m.route_resources controller_file_name
  65
+    end
75 66
   end
76 67
 
77 68
   protected
15  railties/lib/rails_generator/generators/components/scaffold_resource/scaffold_resource_generator.rb
@@ -26,7 +26,7 @@ def initialize(runtime_args, runtime_options = {})
26 26
   end
27 27
 
28 28
   def manifest
29  
-    recorded_session = record do |m|
  29
+    record do |m|
30 30
       # Check for class naming collisions.
31 31
       m.class_collisions(controller_class_path, "#{controller_class_name}Controller", "#{controller_class_name}Helper")
32 32
       m.class_collisions(class_path, "#{class_name}")
@@ -67,18 +67,9 @@ def manifest
67 67
           :migration_file_name => "create_#{file_path.gsub(/\//, '_').pluralize}"
68 68
         )
69 69
       end
70  
-    end
71  
-
72  
-    puts
73  
-    puts ("-" * 70)
74  
-    puts "Don't forget the restful route in config/routes.rb"
75  
-    puts
76  
-    puts "  map.resources :#{controller_file_name}"
77  
-    puts
78  
-    puts ("-" * 70)
79  
-    puts
80 70
 
81  
-    recorded_session
  71
+      m.route_resources controller_file_name
  72
+    end
82 73
   end
83 74
 
84 75
   protected

0 notes on commit c5dcec7

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