Skip to content

Commit

Permalink
Add --no-html to scaffold generator
Browse files Browse the repository at this point in the history
  • Loading branch information
wycats committed Dec 22, 2012
1 parent 7173c4f commit cb025f8
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,25 @@ 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|
invoke assets, [controller_name]
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -40,6 +41,7 @@ def new
# GET <%= route_url %>/1/edit
def edit
end
<%- end -%>
# POST <%= route_url %>
# POST <%= route_url %>.json
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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}" %>]
Expand Down
12 changes: 12 additions & 0 deletions railties/test/generators/scaffold_controller_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

Expand Down
5 changes: 5 additions & 0 deletions railties/test/generators/scaffold_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit cb025f8

Please sign in to comment.