diff options
author | Yehuda Katz <wycats@gmail.com> | 2012-12-22 12:18:13 -0800 |
---|---|---|
committer | Yehuda Katz <wycats@gmail.com> | 2012-12-22 12:18:13 -0800 |
commit | cb025f850c45f26355892961d5cf05145d247a4d (patch) | |
tree | 9d85fce7d2cff6c43036e396e11b9c688feca962 | |
parent | 7173c4f931c9026bd3bc0b644d93dae744433f09 (diff) | |
download | rails-cb025f850c45f26355892961d5cf05145d247a4d.tar.gz rails-cb025f850c45f26355892961d5cf05145d247a4d.tar.bz2 rails-cb025f850c45f26355892961d5cf05145d247a4d.zip |
Add --no-html to scaffold generator
5 files changed, 54 insertions, 8 deletions
diff --git a/railties/lib/rails/generators/rails/scaffold/scaffold_generator.rb b/railties/lib/rails/generators/rails/scaffold/scaffold_generator.rb index 60d202c5ef..dd636ed3cf 100644 --- a/railties/lib/rails/generators/rails/scaffold/scaffold_generator.rb +++ b/railties/lib/rails/generators/rails/scaffold/scaffold_generator.rb @@ -9,6 +9,15 @@ module Rails class_option :stylesheets, type: :boolean, desc: "Generate Stylesheets" class_option :stylesheet_engine, desc: "Engine for Stylesheets" + class_option :html, type: :boolean, default: true, + desc: "Generate a scaffold with HTML output" + + def handle_skip + if !options[:html] || !options[:stylesheets] + @options = @options.merge(stylesheet_engine: false) + end + end + hook_for :scaffold_controller, required: true hook_for :assets do |assets| @@ -16,7 +25,9 @@ module Rails end hook_for :stylesheet_engine do |stylesheet_engine| - invoke stylesheet_engine, [controller_name] if options[:stylesheets] && behavior == :invoke + if behavior == :invoke + invoke stylesheet_engine, [controller_name] + end end end end diff --git a/railties/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb b/railties/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb index 4f36b612ae..32fa54a362 100644 --- a/railties/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb +++ b/railties/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb @@ -10,8 +10,17 @@ module Rails class_option :orm, banner: "NAME", type: :string, required: true, desc: "ORM to generate the controller for" + class_option :html, type: :boolean, default: true, + desc: "Generate a scaffold with HTML output" + argument :attributes, type: :array, default: [], banner: "field:type field:type" + def handle_skip + unless options[:html] + @options = @options.merge(template_engine: false, helper: false) + end + end + def create_controller_files template "controller.rb", File.join('app/controllers', class_path, "#{controller_file_name}_controller.rb") end diff --git a/railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb b/railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb index 4d08b01e60..8f2967bc61 100644 --- a/railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb +++ b/railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb @@ -12,7 +12,7 @@ class <%= controller_class_name %>Controller < ApplicationController @<%= plural_table_name %> = <%= orm_class.all(class_name) %> respond_to do |format| - format.html # index.html.erb + <%- if options[:html] -%>format.html # index.html.erb<%- end -%> format.json { render json: <%= "@#{plural_table_name}" %> } end end @@ -21,11 +21,12 @@ class <%= controller_class_name %>Controller < ApplicationController # GET <%= route_url %>/1.json def show respond_to do |format| - format.html # show.html.erb + <%- if options[:html] -%>format.html # show.html.erb<%- end -%> format.json { render json: <%= "@#{singular_table_name}" %> } end end + <%- if options[:html] -%> # GET <%= route_url %>/new # GET <%= route_url %>/new.json def new @@ -40,6 +41,7 @@ class <%= controller_class_name %>Controller < ApplicationController # GET <%= route_url %>/1/edit def edit end + <%- end -%> # POST <%= route_url %> # POST <%= route_url %>.json @@ -48,10 +50,12 @@ class <%= controller_class_name %>Controller < ApplicationController respond_to do |format| if @<%= orm_instance.save %> + <%- if options[:html] -%> format.html { redirect_to @<%= singular_table_name %>, notice: <%= "'#{human_name} was successfully created.'" %> } + <%- end -%> format.json { render json: <%= "@#{singular_table_name}" %>, status: :created, location: <%= "@#{singular_table_name}" %> } else - format.html { render action: "new" } + <%- if options[:html] -%>format.html { render action: "new" }<%- end -%> format.json { render json: <%= "@#{orm_instance.errors}" %>, status: :unprocessable_entity } end end @@ -62,10 +66,12 @@ class <%= controller_class_name %>Controller < ApplicationController def update respond_to do |format| if @<%= orm_instance.update_attributes("#{singular_table_name}_params") %> + <%- if options[:html] -%> format.html { redirect_to @<%= singular_table_name %>, notice: <%= "'#{human_name} was successfully updated.'" %> } + <%- end -%> format.json { head :no_content } else - format.html { render action: "edit" } + <%- if options[:html] -%>format.html { render action: "edit" }<%- end -%> format.json { render json: <%= "@#{orm_instance.errors}" %>, status: :unprocessable_entity } end end @@ -77,7 +83,7 @@ class <%= controller_class_name %>Controller < ApplicationController @<%= orm_instance.destroy %> respond_to do |format| - format.html { redirect_to <%= index_helper %>_url } + <%- if options[:html] -%>format.html { redirect_to <%= index_helper %>_url }<%- end -%> format.json { head :no_content } end end @@ -88,8 +94,11 @@ class <%= controller_class_name %>Controller < ApplicationController @<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %> end - # Use this method to whitelist the permissible parameters. Example: params.require(:person).permit(:name, :age) - # Also, you can specialize this method with per-user checking of permissible attributes. + # Use this method to whitelist the permissible parameters. Example: + # params.require(:person).permit(:name, :age) + # + # Also, you can specialize this method with per-user checking of permissible + # attributes. def <%= "#{singular_table_name}_params" %> <%- if attributes_names.empty? -%> params[<%= ":#{singular_table_name}" %>] diff --git a/railties/test/generators/scaffold_controller_generator_test.rb b/railties/test/generators/scaffold_controller_generator_test.rb index 57a12d0457..f13305adb0 100644 --- a/railties/test/generators/scaffold_controller_generator_test.rb +++ b/railties/test/generators/scaffold_controller_generator_test.rb @@ -127,6 +127,18 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase assert_no_file "app/views/layouts/users.html.erb" end + def test_skip_html_if_required + run_generator [ "User", "name:string", "age:integer", "--no-html" ] + assert_no_file "app/helpers/users_helper.rb" + assert_no_file "app/views/users" + + assert_file "app/controllers/users_controller.rb" do |content| + assert_no_match(/format\.html/, content) + assert_no_match(/def edit/, content) + assert_no_match(/def new/, content) + end + end + def test_default_orm_is_used run_generator ["User", "--orm=unknown"] diff --git a/railties/test/generators/scaffold_generator_test.rb b/railties/test/generators/scaffold_generator_test.rb index de62fdb1ea..f3f2c170ee 100644 --- a/railties/test/generators/scaffold_generator_test.rb +++ b/railties/test/generators/scaffold_generator_test.rb @@ -257,6 +257,11 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase assert_no_file "app/assets/stylesheets/posts.css" end + def test_scaffold_generator_no_html + run_generator [ "posts", "--no-html" ] + assert_no_file "app/assets/stylesheets/scaffold.css" + end + def test_scaffold_generator_no_javascripts run_generator [ "posts", "--no-javascripts" ] assert_file "app/assets/stylesheets/scaffold.css" |