aboutsummaryrefslogtreecommitdiffstats
path: root/railties/lib/generators/rails/scaffold_controller
diff options
context:
space:
mode:
Diffstat (limited to 'railties/lib/generators/rails/scaffold_controller')
-rw-r--r--railties/lib/generators/rails/scaffold_controller/USAGE20
-rw-r--r--railties/lib/generators/rails/scaffold_controller/scaffold_controller_generator.rb27
-rw-r--r--railties/lib/generators/rails/scaffold_controller/templates/controller.rb87
3 files changed, 134 insertions, 0 deletions
diff --git a/railties/lib/generators/rails/scaffold_controller/USAGE b/railties/lib/generators/rails/scaffold_controller/USAGE
new file mode 100644
index 0000000000..d60a3c3680
--- /dev/null
+++ b/railties/lib/generators/rails/scaffold_controller/USAGE
@@ -0,0 +1,20 @@
+Description:
+ Stubs out a scaffolded controller and its views. Pass the model name,
+ either CamelCased or under_scored, and a list of views as arguments.
+ The controller name is retrieved as a pluralized version of the model
+ name.
+
+ To create a controller within a module, specify the model name as a
+ path like 'parent_module/controller_name'.
+
+ This generates a controller class in app/controllers and invokes helper,
+ template engine and test framework generators.
+
+Example:
+ `./script/generate scaffold_controller CreditCard`
+
+ Credit card controller with URLs like /credit_card/debit.
+ Controller: app/controllers/credit_cards_controller.rb
+ Functional Test: test/functional/credit_cards_controller_test.rb
+ Views: app/views/credit_cards/index.html.erb [...]
+ Helper: app/helpers/credit_cards_helper.rb
diff --git a/railties/lib/generators/rails/scaffold_controller/scaffold_controller_generator.rb b/railties/lib/generators/rails/scaffold_controller/scaffold_controller_generator.rb
new file mode 100644
index 0000000000..c7c777014e
--- /dev/null
+++ b/railties/lib/generators/rails/scaffold_controller/scaffold_controller_generator.rb
@@ -0,0 +1,27 @@
+module Rails
+ module Generators
+ class ScaffoldControllerGenerator < NamedBase
+ # Add controller methods and ActionORM settings.
+ include ScaffoldBase
+
+ check_class_collision :suffix => "Controller"
+
+ class_option :orm, :banner => "NAME", :type => :string, :required => true,
+ :desc => "ORM to generate the controller for"
+
+ class_option :singleton, :type => :boolean, :desc => "Supply to create a singleton controller"
+
+ def create_controller_files
+ template 'controller.rb', File.join('app/controllers', class_path, "#{controller_file_name}_controller.rb")
+ end
+
+ hook_for :template_engine, :test_framework, :as => :scaffold
+
+ # Invoke the helper using the controller (pluralized) name.
+ #
+ invoke_if :helper do |base, invoked|
+ base.invoke invoked, [ base.controller_name ]
+ end
+ end
+ end
+end
diff --git a/railties/lib/generators/rails/scaffold_controller/templates/controller.rb b/railties/lib/generators/rails/scaffold_controller/templates/controller.rb
new file mode 100644
index 0000000000..3cc8bbf8e7
--- /dev/null
+++ b/railties/lib/generators/rails/scaffold_controller/templates/controller.rb
@@ -0,0 +1,87 @@
+class <%= controller_class_name %>Controller < ApplicationController
+<% unless options[:singleton] -%>
+ # GET /<%= table_name %>
+ # GET /<%= table_name %>.xml
+ def index
+ @<%= table_name %> = <%= orm_class.all(class_name) %>
+
+ respond_to do |format|
+ format.html # index.html.erb
+ format.xml { render :xml => @<%= table_name %> }
+ end
+ end
+<% end -%>
+
+ # GET /<%= table_name %>/1
+ # GET /<%= table_name %>/1.xml
+ def show
+ @<%= file_name %> = <%= orm_class.find(class_name, "params[:id]") %>
+
+ respond_to do |format|
+ format.html # show.html.erb
+ format.xml { render :xml => @<%= file_name %> }
+ end
+ end
+
+ # GET /<%= table_name %>/new
+ # GET /<%= table_name %>/new.xml
+ def new
+ @<%= file_name %> = <%= orm_class.build(class_name) %>
+
+ respond_to do |format|
+ format.html # new.html.erb
+ format.xml { render :xml => @<%= file_name %> }
+ end
+ end
+
+ # GET /<%= table_name %>/1/edit
+ def edit
+ @<%= file_name %> = <%= orm_class.find(class_name, "params[:id]") %>
+ end
+
+ # POST /<%= table_name %>
+ # POST /<%= table_name %>.xml
+ def create
+ @<%= file_name %> = <%= orm_class.build(class_name, "params[:#{file_name}]") %>
+
+ respond_to do |format|
+ if @<%= orm_instance.save %>
+ flash[:notice] = '<%= class_name %> was successfully created.'
+ format.html { redirect_to(@<%= file_name %>) }
+ format.xml { render :xml => @<%= file_name %>, :status => :created, :location => @<%= file_name %> }
+ else
+ format.html { render :action => "new" }
+ format.xml { render :xml => @<%= orm_instance.errors %>, :status => :unprocessable_entity }
+ end
+ end
+ end
+
+ # PUT /<%= table_name %>/1
+ # PUT /<%= table_name %>/1.xml
+ def update
+ @<%= file_name %> = <%= orm_class.find(class_name, "params[:id]") %>
+
+ respond_to do |format|
+ if @<%= orm_instance.update_attributes("params[:#{file_name}]") %>
+ flash[:notice] = '<%= class_name %> was successfully updated.'
+ format.html { redirect_to(@<%= file_name %>) }
+ format.xml { head :ok }
+ else
+ format.html { render :action => "edit" }
+ format.xml { render :xml => @<%= orm_instance.errors %>, :status => :unprocessable_entity }
+ end
+ end
+ end
+
+ # DELETE /<%= table_name %>/1
+ # DELETE /<%= table_name %>/1.xml
+ def destroy
+ @<%= file_name %> = <%= orm_class.find(class_name, "params[:id]") %>
+ @<%= orm_instance.destroy %>
+
+ respond_to do |format|
+ format.html { redirect_to(<%= table_name %>_url) }
+ format.xml { head :ok }
+ end
+ end
+end