diff options
author | José Valim <jose.valim@gmail.com> | 2009-07-01 13:30:07 +0200 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2009-07-02 10:27:39 +0200 |
commit | 9541977e049d236564f34cf58660b61e154ccb32 (patch) | |
tree | 46a4828844eea7f3a0ff6eef69f938fcd0af54b7 /railties | |
parent | bf672a12a3726a74a436746f4a82fefd9527686c (diff) | |
download | rails-9541977e049d236564f34cf58660b61e154ccb32.tar.gz rails-9541977e049d236564f34cf58660b61e154ccb32.tar.bz2 rails-9541977e049d236564f34cf58660b61e154ccb32.zip |
Make scaffold controller generate agnostic code and mark some hooks as required.
Diffstat (limited to 'railties')
10 files changed, 105 insertions, 10 deletions
diff --git a/railties/lib/generators/active_record.rb b/railties/lib/generators/active_record.rb index c7316ed38b..6640988c40 100644 --- a/railties/lib/generators/active_record.rb +++ b/railties/lib/generators/active_record.rb @@ -1,5 +1,6 @@ require 'generators/named_base' require 'generators/migration' +require 'generators/action_orm' require 'active_record' module ActiveRecord diff --git a/railties/lib/generators/rails/app/app_generator.rb b/railties/lib/generators/rails/app/app_generator.rb index 6bd464a16c..1971d150c8 100644 --- a/railties/lib/generators/rails/app/app_generator.rb +++ b/railties/lib/generators/rails/app/app_generator.rb @@ -20,13 +20,13 @@ module Rails::Generators class_option :with_dispatchers, :type => :boolean, :aliases => "-D", :default => false, :desc => "Add CGI/FastCGI/mod_ruby dispatchers code" - class_option :skip_activerecord, :type => :boolean, :aliases => "-A", :default => false, + class_option :skip_activerecord, :type => :boolean, :aliases => "-O", :default => false, :desc => "Skip ActiveRecord files" - class_option :skip_testunit, :type => :boolean, :aliases => "-U", :default => false, + class_option :skip_testunit, :type => :boolean, :aliases => "-T", :default => false, :desc => "Skip TestUnit files" - class_option :skip_prototype, :type => :boolean, :aliases => "-P", :default => false, + class_option :skip_prototype, :type => :boolean, :aliases => "-J", :default => false, :desc => "Skip Prototype files" # Add Rails options diff --git a/railties/lib/generators/rails/migration/migration_generator.rb b/railties/lib/generators/rails/migration/migration_generator.rb index 29d1238072..4e2db5e441 100644 --- a/railties/lib/generators/rails/migration/migration_generator.rb +++ b/railties/lib/generators/rails/migration/migration_generator.rb @@ -2,7 +2,7 @@ module Rails module Generators class MigrationGenerator < NamedBase #metagenerator argument :attributes, :type => :hash, :default => {}, :banner => "field:type field:type" - hook_for :orm + hook_for :orm, :required => true end end end diff --git a/railties/lib/generators/rails/model/model_generator.rb b/railties/lib/generators/rails/model/model_generator.rb index 2e13f6f3cb..189f072365 100644 --- a/railties/lib/generators/rails/model/model_generator.rb +++ b/railties/lib/generators/rails/model/model_generator.rb @@ -2,7 +2,7 @@ module Rails module Generators class ModelGenerator < NamedBase #metagenerator argument :attributes, :type => :hash, :default => {}, :banner => "field:type field:type" - hook_for :orm + hook_for :orm, :required => true end end end diff --git a/railties/lib/generators/rails/observer/observer_generator.rb b/railties/lib/generators/rails/observer/observer_generator.rb index 9332c3542a..f5cedee91f 100644 --- a/railties/lib/generators/rails/observer/observer_generator.rb +++ b/railties/lib/generators/rails/observer/observer_generator.rb @@ -1,7 +1,7 @@ module Rails module Generators class ObserverGenerator < NamedBase #metagenerator - hook_for :orm + hook_for :orm, :required => true end end end diff --git a/railties/lib/generators/rails/resource/resource_generator.rb b/railties/lib/generators/rails/resource/resource_generator.rb index c8748aecf4..8ff6d820f2 100644 --- a/railties/lib/generators/rails/resource/resource_generator.rb +++ b/railties/lib/generators/rails/resource/resource_generator.rb @@ -3,7 +3,7 @@ require 'generators/rails/model/model_generator' module Rails module Generators class ResourceGenerator < ModelGenerator #metagenerator - hook_for :resource_controller do |base, controller| + hook_for :resource_controller, :required => true do |base, controller| base.invoke controller, [ base.name.pluralize, base.options[:actions] ] end diff --git a/railties/lib/generators/rails/scaffold/scaffold_generator.rb b/railties/lib/generators/rails/scaffold/scaffold_generator.rb index c641165e3d..874ef98057 100644 --- a/railties/lib/generators/rails/scaffold/scaffold_generator.rb +++ b/railties/lib/generators/rails/scaffold/scaffold_generator.rb @@ -6,7 +6,7 @@ module Rails class_option :test_framework, :banner => "NAME", :desc => "Test framework to be invoked" remove_hook_for :actions, :resource_controller - hook_for :scaffold_controller + hook_for :scaffold_controller, :required => true end end end diff --git a/railties/lib/generators/rails/scaffold_controller/scaffold_controller_generator.rb b/railties/lib/generators/rails/scaffold_controller/scaffold_controller_generator.rb index e7f4fdde89..181d21ba35 100644 --- a/railties/lib/generators/rails/scaffold_controller/scaffold_controller_generator.rb +++ b/railties/lib/generators/rails/scaffold_controller/scaffold_controller_generator.rb @@ -4,6 +4,7 @@ module Rails include ControllerNamedBase check_class_collision :suffix => "Controller" + class_option :orm, :desc => "ORM to generate the controller for", :banner => "NAME", :type => :string class_option :singleton, :type => :boolean, :desc => "Supply to create a singleton controller" def create_controller_files @@ -12,6 +13,16 @@ module Rails hook_for :template_engine, :test_framework, :as => :scaffold invoke_if :helper + + protected + + def orm_class + @orm_class ||= "#{options[:orm].to_s.classify}::Generators::ActionORM".constantize + end + + def orm_instance + @orm_instance ||= @orm_class.new(file_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 index 6bf38cb9b4..307638acf8 100644 --- a/railties/lib/generators/rails/scaffold_controller/templates/controller.rb +++ b/railties/lib/generators/rails/scaffold_controller/templates/controller.rb @@ -1,2 +1,85 @@ -class <%= class_name %>Controller < ApplicationController +class <%= controller_class_name %>Controller < ApplicationController + # 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 + + # 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 diff --git a/railties/lib/generators/rails/session_migration/session_migration_generator.rb b/railties/lib/generators/rails/session_migration/session_migration_generator.rb index 5812c40e55..8f05d7b9c7 100644 --- a/railties/lib/generators/rails/session_migration/session_migration_generator.rb +++ b/railties/lib/generators/rails/session_migration/session_migration_generator.rb @@ -2,7 +2,7 @@ module Rails module Generators class SessionMigrationGenerator < NamedBase #metagenerator argument :name, :type => :string, :default => "add_session_table" - hook_for :orm + hook_for :orm, :required => true end end end |