Work-in-progress scaffolder for resources [DHH/Rick Olson]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5132 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
David Heinemeier Hansson
2006-09-17 22:20:18 +00:00
parent caa8a00520
commit 8f2221da85
13 changed files with 343 additions and 0 deletions

View File

@@ -0,0 +1,8 @@
Description:
Explain the generator
Example:
./script/generate resource_generator Thing
This will create:
what/will/it/create

View File

@@ -0,0 +1,121 @@
class ScaffoldResourceGenerator < Rails::Generator::NamedBase
attr_reader :controller_name,
:controller_class_path,
:controller_file_path,
:controller_class_nesting,
:controller_class_nesting_depth,
:controller_class_name,
:controller_singular_name,
:controller_plural_name
alias_method :controller_file_name, :controller_singular_name
alias_method :controller_table_name, :controller_plural_name
def initialize(runtime_args, runtime_options = {})
super
@controller_name = @name.pluralize
base_name, @controller_class_path, @controller_file_path, @controller_class_nesting, @controller_class_nesting_depth = extract_modules(@controller_name)
@controller_class_name_without_nesting, @controller_singular_name, @controller_plural_name = inflect_names(base_name)
if @controller_class_nesting.empty?
@controller_class_name = @controller_class_name_without_nesting
else
@controller_class_name = "#{@controller_class_nesting}::#{@controller_class_name_without_nesting}"
end
end
def manifest
recorded_session = 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}"
# Controller, helper, views, and test directories.
m.directory File.join('app/models', class_path)
m.directory File.join('app/controllers', controller_class_path)
m.directory File.join('app/helpers', controller_class_path)
m.directory File.join('app/views', controller_class_path, controller_file_name)
m.directory File.join('test/functional', controller_class_path)
m.directory File.join('test/unit', class_path)
scaffold_views.each do |action|
m.template "view_#{action}.rhtml",
File.join('app/views',
controller_class_path,
controller_file_name,
"#{action}.rhtml"),
:assigns => { :action => action }
end
m.template 'model.rb',
File.join('app/models',
class_path,
"#{file_name}.rb")
m.template 'controller.rb',
File.join('app/controllers',
controller_class_path,
"#{controller_file_name}_controller.rb")
m.template 'functional_test.rb',
File.join('test/functional',
controller_class_path,
"#{controller_file_name}_controller_test.rb")
m.template 'helper.rb',
File.join('app/helpers',
controller_class_path,
"#{controller_file_name}_helper.rb")
m.template 'unit_test.rb',
File.join('test/unit',
class_path,
"#{file_name}_test.rb")
m.template 'fixtures.yml',
File.join('test/fixtures',
"#{table_name}.yml")
unless options[:skip_migration]
m.migration_template 'migration.rb', 'db/migrate', :assigns => {
:migration_name => "Create#{class_name.pluralize.gsub(/::/, '')}"
}, :migration_file_name => "create_#{file_path.gsub(/\//, '_').pluralize}"
end
# View template for each action.
[:index, :new, :edit, :show].each do |action|
path = File.join('app/views', class_path, table_name, "#{action}.rhtml")
m.template 'view.rhtml',
path,
:assigns => { :action => action, :path => path }
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
end
protected
# Override with your own usage banner.
def banner
"Usage: #{$0} scaffold_resources ModelName"
end
def scaffold_views
%w[ index show new edit ]
end
def model_name
class_name.demodulize
end
end

View File

@@ -0,0 +1,82 @@
class <%= controller_class_name %>Controller < ApplicationController
# GET /<%= table_name %>
# GET /<%= table_name %>.xml
def index
@<%= table_name %> = <%= class_name %>.find(:all)
respond_to do |format|
format.html # index.rhtml
format.xml { render :xml => @<%= table_name %>.to_xml }
end
end
# GET /<%= table_name %>/1
# GET /<%= table_name %>/1.xml
def show
@<%= file_name %> = <%= class_name %>.find(params[:id])
respond_to do |format|
format.html # show.rhtml
format.xml { render :xml => @<%= file_name %>.to_xml }
end
end
# GET /<%= table_name %>/new
def new
@<%= file_name %> = <%= class_name %>.new
end
# GET /<%= table_name %>/1;edit
def edit
@<%= file_name %> = <%= class_name %>.find(params[:id])
end
# POST /<%= table_name %>
# POST /<%= table_name %>.xml
def create
@<%= file_name %> = <%= class_name %>.new(params[:<%= file_name %>])
respond_to do |format|
if @<%= file_name %>.save
flash[:notice] = '<%= class_name %> was successfully created.'
format.html { redirect_to <%= file_name %>_url(@<%= file_name %>) }
format.xml do
headers["Location"] = <%= file_name %>_url(@<%= file_name %>)
render :nothing => true, :status => "201 Created"
end
else
format.html { render :action => "new" }
format.xml { render :xml => @<%= file_name %>.errors.to_xml }
end
end
end
# PUT /<%= table_name %>/1
# PUT /<%= table_name %>/1.xml
def update
@<%= file_name %> = <%= class_name %>.find(params[:id])
respond_to do |format|
if @<%= file_name %>.update_attributes(params[:<%= file_name %>])
format.html { redirect_to <%= file_name %>_url(@<%= file_name %>) }
format.xml { render :nothing => true }
else
format.html { render :action => "edit" }
format.xml { render :xml => @<%= file_name %>.errors.to_xml }
end
end
end
# DELETE /<%= table_name %>/1
# DELETE /<%= table_name %>/1.xml
def destroy
@<%= file_name %> = <%= class_name %>.find(params[:id])
@<%= file_name %>.destroy
respond_to do |format|
format.html { redirect_to <%= table_name %>_url }
format.xml { render :nothing => true }
end
end
end

View File

@@ -0,0 +1,5 @@
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
first:
id: 1
another:
id: 2

View File

@@ -0,0 +1,57 @@
require File.dirname(__FILE__) + '<%= '/..' * controller_class_nesting_depth %>/../test_helper'
require '<%= controller_file_path %>_controller'
# Re-raise errors caught by the controller.
class <%= controller_class_name %>Controller; def rescue_action(e) raise e end; end
class <%= controller_class_name %>ControllerTest < Test::Unit::TestCase
fixtures :<%= table_name %>
def setup
@controller = <%= controller_class_name %>Controller.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
end
def test_should_get_index
get :index
assert_response :success
assert assigns(:<%= table_name %>)
end
def test_should_get_new
get :new
assert_response :success
end
def test_should_create_<%= file_name %>
old_count = <%= class_name %>.count
post :create, :<%= file_name %> => { }
assert_equal old_count+1, <%= class_name %>.count
assert_redirected_to <%= file_name %>_path(assigns(:<%= file_name %>))
end
def test_should_show_<%= file_name %>
get :show, :id => 1
assert_response :success
end
def test_should_get_edit
get :edit, :id => 1
assert_response :success
end
def test_should_update_<%= file_name %>
put :update, :id => 1, :<%= file_name %> => { }
assert_redirected_to <%= file_name %>_path(assigns(:<%= file_name %>))
end
def test_should_destroy_<%= file_name %>
old_count = <%= class_name %>.count
delete :destroy, :id => 1
assert_equal old_count-1, <%= class_name %>.count
assert_redirected_to <%= table_name %>_path
end
end

View File

@@ -0,0 +1,2 @@
module <%= controller_class_name %>Helper
end

View File

@@ -0,0 +1,13 @@
class <%= migration_name %> < ActiveRecord::Migration
def self.up
create_table :<%= table_name %> do |t|
t.column :name, :string
t.column :description, :text
t.column :created_at, :datetime
end
end
def self.down
drop_table :<%= table_name %>
end
end

View File

@@ -0,0 +1,2 @@
class <%= class_name %> < ActiveRecord::Base
end

View File

@@ -0,0 +1,10 @@
require File.dirname(__FILE__) + '<%= '/..' * class_nesting_depth %>/../test_helper'
class <%= class_name %>Test < Test::Unit::TestCase
fixtures :<%= table_name %>
# Replace this with your real tests.
def test_truth
assert true
end
end

View File

@@ -0,0 +1,6 @@
<h1>Editing <%= singular_name %></h1>
<%%= form(@<%= singular_name %>) %>
<%%= link_to 'Show', <%= singular_name %>_path(@<%= singular_name %>) %> |
<%%= link_to 'Back', <%= plural_name %>_path %>

View File

@@ -0,0 +1,24 @@
<h1>Listing <%= plural_name %></h1>
<table>
<tr>
<%% for column in <%= model_name %>.content_columns %>
<th><%%= column.human_name %></th>
<%% end %>
</tr>
<%% for <%= singular_name %> in @<%= plural_name %> %>
<tr>
<%% for column in <%= model_name %>.content_columns %>
<td><%%=h <%= singular_name %>.send(column.name) %></td>
<%% end %>
<td><%%= link_to 'Show', <%= singular_name %>_path(<%= singular_name %>) %></td>
<td><%%= link_to 'Edit', edit_<%= singular_name %>_path(<%= singular_name %>) %></td>
<td><%%= link_to 'Destroy', <%= singular_name %>_path(<%= singular_name %>), :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<%% end %>
</table>
<br />
<%%= link_to 'New <%= singular_name %>', new_<%= singular_name %>_path %>

View File

@@ -0,0 +1,5 @@
<h1>New <%= singular_name %></h1>
<%%= form(@<%= singular_name %>) %>
<%%= link_to 'Back', <%= plural_name %>_path %>

View File

@@ -0,0 +1,8 @@
<%% for column in <%= model_name %>.content_columns %>
<p>
<b><%%= column.human_name %>:</b> <%%=h @<%= singular_name %>.send(column.name) %>
</p>
<%% end %>
<%%= link_to 'Edit', edit_<%= singular_name %>_path(@<%= singular_name %>) %> |
<%%= link_to 'Back', <%= plural_name %>_path %>