1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Include jbuilder by default and rely on its scaffold generator to show json API. The default scaffold will now just create HTML if the user actively opts out of jbuilder

This commit is contained in:
David Heinemeier Hansson 2013-01-10 20:21:22 +01:00
parent 6572499b07
commit 3bfd99defb
6 changed files with 14 additions and 99 deletions

View file

@ -9,12 +9,12 @@ source 'https://rubygems.org'
<%= assets_gemfile_entry %> <%= assets_gemfile_entry %>
<%= javascript_gemfile_entry -%> <%= javascript_gemfile_entry -%>
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder'
# To use ActiveModel has_secure_password # To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0' # gem 'bcrypt-ruby', '~> 3.0.0'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
# gem 'jbuilder'
# Use unicorn as the app server # Use unicorn as the app server
# gem 'unicorn' # gem 'unicorn'

View file

@ -9,13 +9,8 @@ module Rails
class_option :stylesheets, type: :boolean, desc: "Generate Stylesheets" class_option :stylesheets, type: :boolean, desc: "Generate Stylesheets"
class_option :stylesheet_engine, desc: "Engine for 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 def handle_skip
if !options[:html] || !options[:stylesheets] @options = @options.merge(stylesheet_engine: false) if !options[:stylesheets]
@options = @options.merge(stylesheet_engine: false)
end
end end
hook_for :scaffold_controller, required: true hook_for :scaffold_controller, required: true

View file

@ -10,17 +10,8 @@ module Rails
class_option :orm, banner: "NAME", type: :string, required: true, class_option :orm, banner: "NAME", type: :string, required: true,
desc: "ORM to generate the controller for" 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" 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 def create_controller_files
template "controller.rb", File.join('app/controllers', class_path, "#{controller_file_name}_controller.rb") template "controller.rb", File.join('app/controllers', class_path, "#{controller_file_name}_controller.rb")
end end

View file

@ -7,95 +7,47 @@ class <%= controller_class_name %>Controller < ApplicationController
before_action :set_<%= singular_table_name %>, only: [:show, :edit, :update, :destroy] before_action :set_<%= singular_table_name %>, only: [:show, :edit, :update, :destroy]
# GET <%= route_url %> # GET <%= route_url %>
# GET <%= route_url %>.json
def index def index
@<%= plural_table_name %> = <%= orm_class.all(class_name) %> @<%= plural_table_name %> = <%= orm_class.all(class_name) %>
respond_to do |format|
<%- if options[:html] -%>
format.html # index.html.erb
<%- end -%>
format.json { render json: <%= "@#{plural_table_name}" %> }
end
end end
# GET <%= route_url %>/1 # GET <%= route_url %>/1
# GET <%= route_url %>/1.json
def show def show
respond_to do |format|
<%- if options[:html] -%>
format.html # show.html.erb
<%- end -%>
format.json { render json: <%= "@#{singular_table_name}" %> }
end
end end
<%- if options[:html] -%>
# GET <%= route_url %>/new # GET <%= route_url %>/new
# GET <%= route_url %>/new.json
def new def new
@<%= singular_table_name %> = <%= orm_class.build(class_name) %> @<%= singular_table_name %> = <%= orm_class.build(class_name) %>
respond_to do |format|
format.html # new.html.erb
format.json { render json: <%= "@#{singular_table_name}" %> }
end
end end
# GET <%= route_url %>/1/edit # GET <%= route_url %>/1/edit
def edit def edit
end end
<%- end -%>
# POST <%= route_url %> # POST <%= route_url %>
# POST <%= route_url %>.json
def create def create
@<%= singular_table_name %> = <%= orm_class.build(class_name, "#{singular_table_name}_params") %> @<%= singular_table_name %> = <%= orm_class.build(class_name, "#{singular_table_name}_params") %>
respond_to do |format|
if @<%= orm_instance.save %> if @<%= orm_instance.save %>
<%- if options[:html] -%> redirect_to @<%= singular_table_name %>, notice: <%= "'#{human_name} was successfully created.'" %>
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 else
<%- if options[:html] -%> render action: "new"
format.html { render action: "new" }
<%- end -%>
format.json { render json: <%= "@#{orm_instance.errors}" %>, status: :unprocessable_entity }
end
end end
end end
# PATCH/PUT <%= route_url %>/1 # PATCH/PUT <%= route_url %>/1
# PATCH/PUT <%= route_url %>/1.json
def update def update
respond_to do |format|
if @<%= orm_instance.update("#{singular_table_name}_params") %> if @<%= orm_instance.update("#{singular_table_name}_params") %>
<%- if options[:html] -%> redirect_to @<%= singular_table_name %>, notice: <%= "'#{human_name} was successfully updated.'" %>
format.html { redirect_to @<%= singular_table_name %>, notice: <%= "'#{human_name} was successfully updated.'" %> }
<%- end -%>
format.json { head :no_content }
else else
<%- if options[:html] -%> render action: "edit"
format.html { render action: "edit" }
<%- end -%>
format.json { render json: <%= "@#{orm_instance.errors}" %>, status: :unprocessable_entity }
end
end end
end end
# DELETE <%= route_url %>/1 # DELETE <%= route_url %>/1
# DELETE <%= route_url %>/1.json
def destroy def destroy
@<%= orm_instance.destroy %> @<%= orm_instance.destroy %>
redirect_to <%= index_helper %>_url
respond_to do |format|
<%- if options[:html] -%>
format.html { redirect_to <%= index_helper %>_url }
<%- end -%>
format.json { head :no_content }
end
end end
private private

View file

@ -31,12 +31,10 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase
assert_instance_method :create, content do |m| assert_instance_method :create, content do |m|
assert_match(/@user = User\.new\(user_params\)/, m) assert_match(/@user = User\.new\(user_params\)/, m)
assert_match(/@user\.save/, m) assert_match(/@user\.save/, m)
assert_match(/@user\.errors/, m)
end end
assert_instance_method :update, content do |m| assert_instance_method :update, content do |m|
assert_match(/@user\.update\(user_params\)/, m) assert_match(/@user\.update\(user_params\)/, m)
assert_match(/@user\.errors/, m)
end end
assert_instance_method :destroy, content do |m| assert_instance_method :destroy, content do |m|
@ -127,18 +125,6 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase
assert_no_file "app/views/layouts/users.html.erb" assert_no_file "app/views/layouts/users.html.erb"
end 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 def test_default_orm_is_used
run_generator ["User", "--orm=unknown"] run_generator ["User", "--orm=unknown"]
@ -176,7 +162,7 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase
def test_new_hash_style def test_new_hash_style
run_generator run_generator
assert_file "app/controllers/users_controller.rb" do |content| assert_file "app/controllers/users_controller.rb" do |content|
assert_match(/\{ render action: "new" \}/, content) assert_match(/render action: "new"/, content)
end end
end end
end end

View file

@ -41,12 +41,10 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase
assert_instance_method :create, content do |m| assert_instance_method :create, content do |m|
assert_match(/@product_line = ProductLine\.new\(product_line_params\)/, m) assert_match(/@product_line = ProductLine\.new\(product_line_params\)/, m)
assert_match(/@product_line\.save/, m) assert_match(/@product_line\.save/, m)
assert_match(/@product_line\.errors/, m)
end end
assert_instance_method :update, content do |m| assert_instance_method :update, content do |m|
assert_match(/@product_line\.update\(product_line_params\)/, m) assert_match(/@product_line\.update\(product_line_params\)/, m)
assert_match(/@product_line\.errors/, m)
end end
assert_instance_method :destroy, content do |m| assert_instance_method :destroy, content do |m|
@ -158,12 +156,10 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase
assert_instance_method :create, content do |m| assert_instance_method :create, content do |m|
assert_match(/@admin_role = Admin::Role\.new\(admin_role_params\)/, m) assert_match(/@admin_role = Admin::Role\.new\(admin_role_params\)/, m)
assert_match(/@admin_role\.save/, m) assert_match(/@admin_role\.save/, m)
assert_match(/@admin_role\.errors/, m)
end end
assert_instance_method :update, content do |m| assert_instance_method :update, content do |m|
assert_match(/@admin_role\.update\(admin_role_params\)/, m) assert_match(/@admin_role\.update\(admin_role_params\)/, m)
assert_match(/@admin_role\.errors/, m)
end end
assert_instance_method :destroy, content do |m| assert_instance_method :destroy, content do |m|
@ -257,11 +253,6 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase
assert_no_file "app/assets/stylesheets/posts.css" assert_no_file "app/assets/stylesheets/posts.css"
end 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 def test_scaffold_generator_no_javascripts
run_generator [ "posts", "--no-javascripts" ] run_generator [ "posts", "--no-javascripts" ]
assert_file "app/assets/stylesheets/scaffold.css" assert_file "app/assets/stylesheets/scaffold.css"