mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Add --no-html to scaffold generator
This commit is contained in:
parent
7173c4f931
commit
cb025f850c
5 changed files with 54 additions and 8 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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}" %>]
|
||||
|
|
|
@ -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"]
|
||||
|
||||
|
|
|
@ -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"
|
||||
|
|
Loading…
Reference in a new issue