aboutsummaryrefslogtreecommitdiffstats
path: root/railties/lib
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2006-11-01 01:57:06 +0000
committerJeremy Kemper <jeremy@bitsweat.net>2006-11-01 01:57:06 +0000
commitc5dcec789e5513ef1cfd8ef468cd2a46cdaf01f5 (patch)
treef3a166ae1d6f2c69619871b4c326f70cf706a0d7 /railties/lib
parent98a9b6521886009187afd324e0cdad64902e0e97 (diff)
downloadrails-c5dcec789e5513ef1cfd8ef468cd2a46cdaf01f5.tar.gz
rails-c5dcec789e5513ef1cfd8ef468cd2a46cdaf01f5.tar.bz2
rails-c5dcec789e5513ef1cfd8ef468cd2a46cdaf01f5.zip
resource and scaffold_resource generators add a restful route to config/routes.rb
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5379 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'railties/lib')
-rw-r--r--railties/lib/rails_generator/commands.rb28
-rw-r--r--railties/lib/rails_generator/generators/components/resource/resource_generator.rb15
-rw-r--r--railties/lib/rails_generator/generators/components/scaffold_resource/scaffold_resource_generator.rb15
3 files changed, 34 insertions, 24 deletions
diff --git a/railties/lib/rails_generator/commands.rb b/railties/lib/rails_generator/commands.rb
index 0d039838d3..9bc20b1c79 100644
--- a/railties/lib/rails_generator/commands.rb
+++ b/railties/lib/rails_generator/commands.rb
@@ -83,6 +83,12 @@ module Rails
"%.#{padding}d" % next_migration_number
end
+ def gsub_file(relative_destination, regexp, *args, &block)
+ path = destination_path(relative_destination)
+ content = File.read(path).gsub(regexp, *args, &block)
+ File.open(path, 'wb') { |file| file.write(content) }
+ end
+
private
# Ask the user interactively whether to force collision.
def force_file_collision?(destination)
@@ -316,6 +322,16 @@ module Rails
template(relative_source, "#{relative_destination}/#{next_migration_string}_#{migration_file_name}.rb", template_options)
end
+ def route_resources(*resources)
+ resource_list = resources.map { |r| r.to_sym.inspect }.join(', ')
+ sentinel = 'ActionController::Routing::Routes.draw do |map|'
+
+ logger.route "map.resources #{resource_list}"
+ gsub_file 'config/routes.rb', /(#{Regexp.escape(sentinel)})/mi do |match|
+ "#{match}\n map.resources #{resource_list}\n"
+ end
+ end
+
private
# Raise a usage error with an informative WordNet suggestion.
# Thanks to Florian Gross (flgr).
@@ -438,6 +454,13 @@ end_message
file(relative_source, file_path, template_options)
end
end
+
+ def route_resources(*resources)
+ resource_list = resources.map { |r| r.to_sym.inspect }.join(', ')
+ look_for = "\n map.resources #{resource_list}\n"
+ logger.route "map.resources #{resource_list}"
+ gsub_file 'config/routes.rb', /(#{look_for})/mi, ''
+ end
end
@@ -475,6 +498,11 @@ end_message
migration_directory relative_destination
logger.migration_template file_name
end
+
+ def route_resources(*resources)
+ resource_list = resources.map { |r| r.to_sym.inspect }.join(', ')
+ logger.route "map.resources #{resource_list}"
+ end
end
# Update generator's action manifest.
diff --git a/railties/lib/rails_generator/generators/components/resource/resource_generator.rb b/railties/lib/rails_generator/generators/components/resource/resource_generator.rb
index 34abc6c97b..fd834bb149 100644
--- a/railties/lib/rails_generator/generators/components/resource/resource_generator.rb
+++ b/railties/lib/rails_generator/generators/components/resource/resource_generator.rb
@@ -26,7 +26,7 @@ class ResourceGenerator < Rails::Generator::NamedBase
end
def manifest
- recorded_session = record do |m|
+ record do |m|
# Check for class naming collisions.
m.class_collisions(controller_class_path, "#{controller_class_name}Controller", "#{controller_class_name}Helper")
m.class_collisions(class_path, "#{class_name}")
@@ -60,18 +60,9 @@ class ResourceGenerator < Rails::Generator::NamedBase
:migration_file_name => "create_#{file_path.gsub(/\//, '_').pluralize}"
)
end
- end
-
- puts
- puts ("-" * 70)
- puts "Don't forget the restful route in config/routes.rb"
- puts
- puts " map.resources :#{controller_file_name}"
- puts
- puts ("-" * 70)
- puts
- recorded_session
+ m.route_resources controller_file_name
+ end
end
protected
diff --git a/railties/lib/rails_generator/generators/components/scaffold_resource/scaffold_resource_generator.rb b/railties/lib/rails_generator/generators/components/scaffold_resource/scaffold_resource_generator.rb
index ca6d997a9a..b0d9af7033 100644
--- a/railties/lib/rails_generator/generators/components/scaffold_resource/scaffold_resource_generator.rb
+++ b/railties/lib/rails_generator/generators/components/scaffold_resource/scaffold_resource_generator.rb
@@ -26,7 +26,7 @@ class ScaffoldResourceGenerator < Rails::Generator::NamedBase
end
def manifest
- recorded_session = record do |m|
+ record do |m|
# Check for class naming collisions.
m.class_collisions(controller_class_path, "#{controller_class_name}Controller", "#{controller_class_name}Helper")
m.class_collisions(class_path, "#{class_name}")
@@ -67,18 +67,9 @@ class ScaffoldResourceGenerator < Rails::Generator::NamedBase
:migration_file_name => "create_#{file_path.gsub(/\//, '_').pluralize}"
)
end
- end
-
- puts
- puts ("-" * 70)
- puts "Don't forget the restful route in config/routes.rb"
- puts
- puts " map.resources :#{controller_file_name}"
- puts
- puts ("-" * 70)
- puts
- recorded_session
+ m.route_resources controller_file_name
+ end
end
protected