aboutsummaryrefslogtreecommitdiffstats
path: root/railties
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2007-05-16 01:23:34 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2007-05-16 01:23:34 +0000
commit4f2a950ce2f29d4c6ce777d5a949440b6c2a440f (patch)
treef8138e47643edf98835ee3d85366b58b36795c97 /railties
parent73fbf7771bbfc6c3c8c4ddd8f4f84eb6bfac504d (diff)
downloadrails-4f2a950ce2f29d4c6ce777d5a949440b6c2a440f.tar.gz
rails-4f2a950ce2f29d4c6ce777d5a949440b6c2a440f.tar.bz2
rails-4f2a950ce2f29d4c6ce777d5a949440b6c2a440f.zip
Make way for the new and improved scaffold
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6745 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'railties')
-rw-r--r--railties/lib/rails_generator/generators/components/scaffold/USAGE32
-rw-r--r--railties/lib/rails_generator/generators/components/scaffold/scaffold_generator.rb193
-rw-r--r--railties/lib/rails_generator/generators/components/scaffold/templates/controller.rb58
-rw-r--r--railties/lib/rails_generator/generators/components/scaffold/templates/form.html.erb3
-rw-r--r--railties/lib/rails_generator/generators/components/scaffold/templates/form_scaffolding.html.erb1
-rw-r--r--railties/lib/rails_generator/generators/components/scaffold/templates/functional_test.rb102
-rw-r--r--railties/lib/rails_generator/generators/components/scaffold/templates/helper.rb2
-rw-r--r--railties/lib/rails_generator/generators/components/scaffold/templates/layout.html.erb17
-rw-r--r--railties/lib/rails_generator/generators/components/scaffold/templates/style.css74
-rw-r--r--railties/lib/rails_generator/generators/components/scaffold/templates/view_edit.html.erb9
-rw-r--r--railties/lib/rails_generator/generators/components/scaffold/templates/view_list.html.erb27
-rw-r--r--railties/lib/rails_generator/generators/components/scaffold/templates/view_new.html.erb8
-rw-r--r--railties/lib/rails_generator/generators/components/scaffold/templates/view_show.html.erb8
13 files changed, 0 insertions, 534 deletions
diff --git a/railties/lib/rails_generator/generators/components/scaffold/USAGE b/railties/lib/rails_generator/generators/components/scaffold/USAGE
deleted file mode 100644
index 1b6eaa2dd9..0000000000
--- a/railties/lib/rails_generator/generators/components/scaffold/USAGE
+++ /dev/null
@@ -1,32 +0,0 @@
-Description:
- The scaffold generator creates a controller to interact with a model.
- If the model does not exist, it creates the model as well. The generated
- code is equivalent to the "scaffold :model" declaration, making it easy
- to migrate when you wish to customize your controller and views.
-
- The generator takes a model name, an optional controller name, and a
- list of views as arguments. Scaffolded actions and views are created
- automatically. Any views left over generate empty stubs.
-
- The scaffolded actions and views are:
- index, list, show, new, create, edit, update, destroy
-
- If a controller name is not given, the plural form of the model name
- will be used. The model and controller names may be given in CamelCase
- or under_score and should not be suffixed with 'Model' or 'Controller'.
- Both model and controller names may be prefixed with a module like a
- file path; see the Modules Example for usage.
-
-Example:
- ./script/generate scaffold Account Bank debit credit
-
- This will generate an Account model and BankController with a full test
- suite and a basic user interface. Now create the accounts table in your
- database and browse to http://localhost/bank/ -- voila, you're on Rails!
-
-Modules Example:
- ./script/generate scaffold CreditCard 'admin/credit_card' suspend late_fee
-
- This will generate a CreditCard model and CreditCardController controller
- in the admin module.
-
diff --git a/railties/lib/rails_generator/generators/components/scaffold/scaffold_generator.rb b/railties/lib/rails_generator/generators/components/scaffold/scaffold_generator.rb
deleted file mode 100644
index a3650c7e3a..0000000000
--- a/railties/lib/rails_generator/generators/components/scaffold/scaffold_generator.rb
+++ /dev/null
@@ -1,193 +0,0 @@
-class ScaffoldingSandbox
- include ActionView::Helpers::ActiveRecordHelper
-
- attr_accessor :form_action, :singular_name, :suffix, :model_instance
-
- def sandbox_binding
- binding
- end
-
- def default_input_block
- Proc.new { |record, column| "<p><label for=\"#{record}_#{column.name}\">#{column.human_name}</label><br/>\n#{input(record, column.name)}</p>\n" }
- end
-
-end
-
-class ActionView::Helpers::InstanceTag
- def to_input_field_tag(field_type, options={})
- field_meth = "#{field_type}_field"
- "<%= #{field_meth} '#{@object_name}', '#{@method_name}' #{options.empty? ? '' : ', '+options.inspect} %>"
- end
-
- def to_text_area_tag(options = {})
- "<%= text_area '#{@object_name}', '#{@method_name}' #{options.empty? ? '' : ', '+ options.inspect} %>"
- end
-
- def to_date_select_tag(options = {})
- "<%= date_select '#{@object_name}', '#{@method_name}' #{options.empty? ? '' : ', '+ options.inspect} %>"
- end
-
- def to_datetime_select_tag(options = {})
- "<%= datetime_select '#{@object_name}', '#{@method_name}' #{options.empty? ? '' : ', '+ options.inspect} %>"
- end
-
- def to_time_select_tag(options = {})
- "<%= time_select '#{@object_name}', '#{@method_name}' #{options.empty? ? '' : ', '+ options.inspect} %>"
- end
-end
-
-class ScaffoldGenerator < 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
-
- # Take controller name from the next argument. Default to the pluralized model name.
- @controller_name = args.shift
- @controller_name ||= ActiveRecord::Base.pluralize_table_names ? @name.pluralize : @name
-
- 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
- record do |m|
- # Check for class naming collisions.
- m.class_collisions controller_class_path, "#{controller_class_name}Controller", "#{controller_class_name}ControllerTest", "#{controller_class_name}Helper"
-
- # Controller, helper, views, and test directories.
- 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('app/views/layouts', controller_class_path)
- m.directory File.join('test/functional', controller_class_path)
-
- # Depend on model generator but skip if the model exists.
- m.dependency 'model', [singular_name], :collision => :skip, :skip_migration => true
-
- # Scaffolded forms.
- m.complex_template "form.html.erb",
- File.join('app/views',
- controller_class_path,
- controller_file_name,
- "_form.html.erb"),
- :insert => 'form_scaffolding.html.erb',
- :sandbox => lambda { create_sandbox },
- :begin_mark => 'form',
- :end_mark => 'eoform',
- :mark_id => singular_name
-
-
- # Scaffolded views.
- scaffold_views.each do |action|
- m.template "view_#{action}.html.erb",
- File.join('app/views',
- controller_class_path,
- controller_file_name,
- "#{action}.html.erb"),
- :assigns => { :action => action }
- end
-
- # Controller class, functional test, helper, and views.
- 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")
-
- # Layout and stylesheet.
- m.template 'layout.html.erb',
- File.join('app/views/layouts',
- controller_class_path,
- "#{controller_file_name}.html.erb")
-
- m.template 'style.css', 'public/stylesheets/scaffold.css'
-
-
- # Unscaffolded views.
- unscaffolded_actions.each do |action|
- path = File.join('app/views',
- controller_class_path,
- controller_file_name,
- "#{action}.html.erb")
- m.template "controller:view.html.erb", path,
- :assigns => { :action => action, :path => path}
- end
- end
- end
-
- protected
- # Override with your own usage banner.
- def banner
- "Usage: #{$0} scaffold ModelName [ControllerName] [action, ...]"
- end
-
- def scaffold_views
- %w(list show new edit)
- end
-
- def scaffold_actions
- scaffold_views + %w(index create update destroy)
- end
-
- def model_name
- class_name.demodulize
- end
-
- def unscaffolded_actions
- args - scaffold_actions
- end
-
- def suffix
- "_#{singular_name}" if options[:suffix]
- end
-
- def create_sandbox
- sandbox = ScaffoldingSandbox.new
- sandbox.singular_name = singular_name
- begin
- sandbox.model_instance = model_instance
- sandbox.instance_variable_set("@#{singular_name}", sandbox.model_instance)
- rescue ActiveRecord::StatementInvalid => e
- logger.error "Before updating scaffolding from new DB schema, try creating a table for your model (#{class_name}) named #{class_name.tableize}."
- raise SystemExit
- end
- sandbox.suffix = suffix
- sandbox
- end
-
- def model_instance
- base = class_nesting.split('::').inject(Object) do |base, nested|
- break base.const_get(nested) if base.const_defined?(nested)
- base.const_set(nested, Module.new)
- end
- unless base.const_defined?(@class_name_without_nesting)
- base.const_set(@class_name_without_nesting, Class.new(ActiveRecord::Base))
- end
- class_name.constantize.new
- end
-end
diff --git a/railties/lib/rails_generator/generators/components/scaffold/templates/controller.rb b/railties/lib/rails_generator/generators/components/scaffold/templates/controller.rb
deleted file mode 100644
index c059e74536..0000000000
--- a/railties/lib/rails_generator/generators/components/scaffold/templates/controller.rb
+++ /dev/null
@@ -1,58 +0,0 @@
-class <%= controller_class_name %>Controller < ApplicationController
-<% unless suffix -%>
- def index
- list
- render :action => 'list'
- end
-<% end -%>
-
-<% for action in unscaffolded_actions -%>
- def <%= action %><%= suffix %>
- end
-
-<% end -%>
- # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
- verify :method => :post, :only => [ :destroy<%= suffix %>, :create<%= suffix %>, :update<%= suffix %> ],
- :redirect_to => { :action => :list<%= suffix %> }
-
- def list<%= suffix %>
- @<%= singular_name %>_pages, @<%= plural_name %> = paginate :<%= plural_name %>, :per_page => 10
- end
-
- def show<%= suffix %>
- @<%= singular_name %> = <%= model_name %>.find(params[:id])
- end
-
- def new<%= suffix %>
- @<%= singular_name %> = <%= model_name %>.new
- end
-
- def create<%= suffix %>
- @<%= singular_name %> = <%= model_name %>.new(params[:<%= singular_name %>])
- if @<%= singular_name %>.save
- flash[:notice] = '<%= model_name %> was successfully created.'
- redirect_to :action => 'list<%= suffix %>'
- else
- render :action => 'new<%= suffix %>'
- end
- end
-
- def edit<%= suffix %>
- @<%= singular_name %> = <%= model_name %>.find(params[:id])
- end
-
- def update
- @<%= singular_name %> = <%= model_name %>.find(params[:id])
- if @<%= singular_name %>.update_attributes(params[:<%= singular_name %>])
- flash[:notice] = '<%= model_name %> was successfully updated.'
- redirect_to :action => 'show<%= suffix %>', :id => @<%= singular_name %>
- else
- render :action => 'edit<%= suffix %>'
- end
- end
-
- def destroy<%= suffix %>
- <%= model_name %>.find(params[:id]).destroy
- redirect_to :action => 'list<%= suffix %>'
- end
-end
diff --git a/railties/lib/rails_generator/generators/components/scaffold/templates/form.html.erb b/railties/lib/rails_generator/generators/components/scaffold/templates/form.html.erb
deleted file mode 100644
index d15f0d4ec2..0000000000
--- a/railties/lib/rails_generator/generators/components/scaffold/templates/form.html.erb
+++ /dev/null
@@ -1,3 +0,0 @@
-<%%= error_messages_for '<%= singular_name %>' %>
-
-<%= template_for_inclusion %>
diff --git a/railties/lib/rails_generator/generators/components/scaffold/templates/form_scaffolding.html.erb b/railties/lib/rails_generator/generators/components/scaffold/templates/form_scaffolding.html.erb
deleted file mode 100644
index c7a87553ac..0000000000
--- a/railties/lib/rails_generator/generators/components/scaffold/templates/form_scaffolding.html.erb
+++ /dev/null
@@ -1 +0,0 @@
-<%= all_input_tags(@model_instance, @singular_name, {}) %> \ No newline at end of file
diff --git a/railties/lib/rails_generator/generators/components/scaffold/templates/functional_test.rb b/railties/lib/rails_generator/generators/components/scaffold/templates/functional_test.rb
deleted file mode 100644
index ed7136bb52..0000000000
--- a/railties/lib/rails_generator/generators/components/scaffold/templates/functional_test.rb
+++ /dev/null
@@ -1,102 +0,0 @@
-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
-
- @first_id = <%= plural_name %>(:first).id
- end
-
-<% for action in unscaffolded_actions -%>
- def test_<%= action %>
- get :<%= action %>
- assert_response :success
- assert_template '<%= action %>'
- end
-
-<% end -%>
-<% unless suffix -%>
- def test_index
- get :index
- assert_response :success
- assert_template 'list'
- end
-
-<% end -%>
- def test_list<%= suffix %>
- get :list<%= suffix %>
-
- assert_response :success
- assert_template 'list<%= suffix %>'
-
- assert_not_nil assigns(:<%= plural_name %>)
- end
-
- def test_show<%= suffix %>
- get :show<%= suffix %>, :id => @first_id
-
- assert_response :success
- assert_template 'show'
-
- assert_not_nil assigns(:<%= singular_name %>)
- assert assigns(:<%= singular_name %>).valid?
- end
-
- def test_new<%= suffix %>
- get :new<%= suffix %>
-
- assert_response :success
- assert_template 'new<%= suffix %>'
-
- assert_not_nil assigns(:<%= singular_name %>)
- end
-
- def test_create
- num_<%= plural_name %> = <%= model_name %>.count
-
- post :create<%= suffix %>, :<%= singular_name %> => {}
-
- assert_response :redirect
- assert_redirected_to :action => 'list<%= suffix %>'
-
- assert_equal num_<%= plural_name %> + 1, <%= model_name %>.count
- end
-
- def test_edit<%= suffix %>
- get :edit<%= suffix %>, :id => @first_id
-
- assert_response :success
- assert_template 'edit<%= suffix %>'
-
- assert_not_nil assigns(:<%= singular_name %>)
- assert assigns(:<%= singular_name %>).valid?
- end
-
- def test_update<%= suffix %>
- post :update<%= suffix %>, :id => @first_id
- assert_response :redirect
- assert_redirected_to :action => 'show<%= suffix %>', :id => @first_id
- end
-
- def test_destroy<%= suffix %>
- assert_nothing_raised {
- <%= model_name %>.find(@first_id)
- }
-
- post :destroy, :id => @first_id
- assert_response :redirect
- assert_redirected_to :action => 'list<%= suffix %>'
-
- assert_raise(ActiveRecord::RecordNotFound) {
- <%= model_name %>.find(@first_id)
- }
- end
-end
diff --git a/railties/lib/rails_generator/generators/components/scaffold/templates/helper.rb b/railties/lib/rails_generator/generators/components/scaffold/templates/helper.rb
deleted file mode 100644
index 9bd821b1b2..0000000000
--- a/railties/lib/rails_generator/generators/components/scaffold/templates/helper.rb
+++ /dev/null
@@ -1,2 +0,0 @@
-module <%= controller_class_name %>Helper
-end
diff --git a/railties/lib/rails_generator/generators/components/scaffold/templates/layout.html.erb b/railties/lib/rails_generator/generators/components/scaffold/templates/layout.html.erb
deleted file mode 100644
index 5c1f304232..0000000000
--- a/railties/lib/rails_generator/generators/components/scaffold/templates/layout.html.erb
+++ /dev/null
@@ -1,17 +0,0 @@
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
-<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
-<head>
- <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
- <title><%= controller_class_name %>: <%%= controller.action_name %></title>
- <%%= stylesheet_link_tag 'scaffold' %>
-</head>
-<body>
-
-<p style="color: green"><%%= flash[:notice] %></p>
-
-<%%= yield %>
-
-</body>
-</html>
diff --git a/railties/lib/rails_generator/generators/components/scaffold/templates/style.css b/railties/lib/rails_generator/generators/components/scaffold/templates/style.css
deleted file mode 100644
index 8f239a3597..0000000000
--- a/railties/lib/rails_generator/generators/components/scaffold/templates/style.css
+++ /dev/null
@@ -1,74 +0,0 @@
-body { background-color: #fff; color: #333; }
-
-body, p, ol, ul, td {
- font-family: verdana, arial, helvetica, sans-serif;
- font-size: 13px;
- line-height: 18px;
-}
-
-pre {
- background-color: #eee;
- padding: 10px;
- font-size: 11px;
-}
-
-a { color: #000; }
-a:visited { color: #666; }
-a:hover { color: #fff; background-color:#000; }
-
-.fieldWithErrors {
- padding: 2px;
- background-color: red;
- display: table;
-}
-
-#errorExplanation {
- width: 400px;
- border: 2px solid red;
- padding: 7px;
- padding-bottom: 12px;
- margin-bottom: 20px;
- background-color: #f0f0f0;
-}
-
-#errorExplanation h2 {
- text-align: left;
- font-weight: bold;
- padding: 5px 5px 5px 15px;
- font-size: 12px;
- margin: -7px;
- background-color: #c00;
- color: #fff;
-}
-
-#errorExplanation p {
- color: #333;
- margin-bottom: 0;
- padding: 5px;
-}
-
-#errorExplanation ul li {
- font-size: 12px;
- list-style: square;
-}
-
-div.uploadStatus {
- margin: 5px;
-}
-
-div.progressBar {
- margin: 5px;
-}
-
-div.progressBar div.border {
- background-color: #fff;
- border: 1px solid grey;
- width: 100%;
-}
-
-div.progressBar div.background {
- background-color: #333;
- height: 18px;
- width: 0%;
-}
-
diff --git a/railties/lib/rails_generator/generators/components/scaffold/templates/view_edit.html.erb b/railties/lib/rails_generator/generators/components/scaffold/templates/view_edit.html.erb
deleted file mode 100644
index a86aa0b0d5..0000000000
--- a/railties/lib/rails_generator/generators/components/scaffold/templates/view_edit.html.erb
+++ /dev/null
@@ -1,9 +0,0 @@
-<h1>Editing <%= singular_name %></h1>
-
-<%% form_tag :action => 'update<%= @suffix %>', :id => @<%= singular_name %> do %>
- <%%= render :partial => 'form' %>
- <%%= submit_tag 'Edit' %>
-<%% end %>
-
-<%%= link_to 'Show', :action => 'show<%= suffix %>', :id => @<%= singular_name %> %> |
-<%%= link_to 'Back', :action => 'list<%= suffix %>' %>
diff --git a/railties/lib/rails_generator/generators/components/scaffold/templates/view_list.html.erb b/railties/lib/rails_generator/generators/components/scaffold/templates/view_list.html.erb
deleted file mode 100644
index 3e2a414485..0000000000
--- a/railties/lib/rails_generator/generators/components/scaffold/templates/view_list.html.erb
+++ /dev/null
@@ -1,27 +0,0 @@
-<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', :action => 'show<%= suffix %>', :id => <%= singular_name %> %></td>
- <td><%%= link_to 'Edit', :action => 'edit<%= suffix %>', :id => <%= singular_name %> %></td>
- <td><%%= link_to 'Destroy', { :action => 'destroy<%= suffix %>', :id => <%= singular_name %> }, :confirm => 'Are you sure?', :method => :post %></td>
- </tr>
-<%% end %>
-</table>
-
-<%%= link_to 'Previous page', { :page => @<%= singular_name %>_pages.current.previous } if @<%= singular_name %>_pages.current.previous %>
-<%%= link_to 'Next page', { :page => @<%= singular_name %>_pages.current.next } if @<%= singular_name %>_pages.current.next %>
-
-<br />
-
-<%%= link_to 'New <%= singular_name %>', :action => 'new<%= suffix %>' %>
diff --git a/railties/lib/rails_generator/generators/components/scaffold/templates/view_new.html.erb b/railties/lib/rails_generator/generators/components/scaffold/templates/view_new.html.erb
deleted file mode 100644
index 8222ea622f..0000000000
--- a/railties/lib/rails_generator/generators/components/scaffold/templates/view_new.html.erb
+++ /dev/null
@@ -1,8 +0,0 @@
-<h1>New <%= singular_name %></h1>
-
-<%% form_tag :action => 'create<%= @suffix %>' do %>
- <%%= render :partial => 'form' %>
- <%%= submit_tag "Create" %>
-<%% end %>
-
-<%%= link_to 'Back', :action => 'list<%= suffix %>' %>
diff --git a/railties/lib/rails_generator/generators/components/scaffold/templates/view_show.html.erb b/railties/lib/rails_generator/generators/components/scaffold/templates/view_show.html.erb
deleted file mode 100644
index c9245cdfda..0000000000
--- a/railties/lib/rails_generator/generators/components/scaffold/templates/view_show.html.erb
+++ /dev/null
@@ -1,8 +0,0 @@
-<%% for column in <%= model_name %>.content_columns %>
-<p>
- <b><%%= column.human_name %>:</b> <%%=h @<%= singular_name %>.send(column.name) %>
-</p>
-<%% end %>
-
-<%%= link_to 'Edit', :action => 'edit<%= suffix %>', :id => @<%= singular_name %> %> |
-<%%= link_to 'Back', :action => 'list<%= suffix %>' %>