Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add --no-html to scaffold generator

  • Loading branch information...
commit cb025f850c45f26355892961d5cf05145d247a4d 1 parent 7173c4f
@wycats wycats authored
View
13 railties/lib/rails/generators/rails/scaffold/scaffold_generator.rb
@@ -9,6 +9,15 @@ class ScaffoldGenerator < ResourceGenerator # :nodoc:
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 @@ class ScaffoldGenerator < ResourceGenerator # :nodoc:
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
View
9 railties/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb
@@ -10,8 +10,17 @@ class ScaffoldControllerGenerator < NamedBase # :nodoc:
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
View
23 railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb
@@ -12,7 +12,7 @@ def index
@<%= 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 @@ def index
# 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 @@ def new
# GET <%= route_url %>/1/edit
def edit
end
+ <%- end -%>
# POST <%= route_url %>
# POST <%= route_url %>.json
@@ -48,10 +50,12 @@ def create
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 @@ def create
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 @@ def destroy
@<%= 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 @@ def set_<%= singular_table_name %>
@<%= 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}" %>]
View
12 railties/test/generators/scaffold_controller_generator_test.rb
@@ -127,6 +127,18 @@ def test_skip_layout_if_required
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"]
View
5 railties/test/generators/scaffold_generator_test.rb
@@ -257,6 +257,11 @@ def test_scaffold_generator_no_stylesheets
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"
Please sign in to comment.
Something went wrong with that request. Please try again.