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:
parent
6572499b07
commit
3bfd99defb
6 changed files with 14 additions and 99 deletions
|
@ -9,12 +9,12 @@ source 'https://rubygems.org'
|
|||
<%= assets_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
|
||||
# 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
|
||||
# gem 'unicorn'
|
||||
|
||||
|
|
|
@ -9,13 +9,8 @@ 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
|
||||
@options = @options.merge(stylesheet_engine: false) if !options[:stylesheets]
|
||||
end
|
||||
|
||||
hook_for :scaffold_controller, required: true
|
||||
|
|
|
@ -10,17 +10,8 @@ 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
|
||||
|
|
|
@ -7,95 +7,47 @@ class <%= controller_class_name %>Controller < ApplicationController
|
|||
before_action :set_<%= singular_table_name %>, only: [:show, :edit, :update, :destroy]
|
||||
|
||||
# GET <%= route_url %>
|
||||
# GET <%= route_url %>.json
|
||||
def index
|
||||
@<%= 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
|
||||
|
||||
# GET <%= route_url %>/1
|
||||
# GET <%= route_url %>/1.json
|
||||
def show
|
||||
respond_to do |format|
|
||||
<%- 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
|
||||
@<%= 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
|
||||
|
||||
# GET <%= route_url %>/1/edit
|
||||
def edit
|
||||
end
|
||||
<%- end -%>
|
||||
|
||||
# POST <%= route_url %>
|
||||
# POST <%= route_url %>.json
|
||||
def create
|
||||
@<%= singular_table_name %> = <%= orm_class.build(class_name, "#{singular_table_name}_params") %>
|
||||
|
||||
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}" %> }
|
||||
redirect_to @<%= singular_table_name %>, notice: <%= "'#{human_name} was successfully created.'" %>
|
||||
else
|
||||
<%- if options[:html] -%>
|
||||
format.html { render action: "new" }
|
||||
<%- end -%>
|
||||
format.json { render json: <%= "@#{orm_instance.errors}" %>, status: :unprocessable_entity }
|
||||
end
|
||||
render action: "new"
|
||||
end
|
||||
end
|
||||
|
||||
# PATCH/PUT <%= route_url %>/1
|
||||
# PATCH/PUT <%= route_url %>/1.json
|
||||
def update
|
||||
respond_to do |format|
|
||||
if @<%= orm_instance.update("#{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 }
|
||||
redirect_to @<%= singular_table_name %>, notice: <%= "'#{human_name} was successfully updated.'" %>
|
||||
else
|
||||
<%- if options[:html] -%>
|
||||
format.html { render action: "edit" }
|
||||
<%- end -%>
|
||||
format.json { render json: <%= "@#{orm_instance.errors}" %>, status: :unprocessable_entity }
|
||||
end
|
||||
render action: "edit"
|
||||
end
|
||||
end
|
||||
|
||||
# DELETE <%= route_url %>/1
|
||||
# DELETE <%= route_url %>/1.json
|
||||
def destroy
|
||||
@<%= orm_instance.destroy %>
|
||||
|
||||
respond_to do |format|
|
||||
<%- if options[:html] -%>
|
||||
format.html { redirect_to <%= index_helper %>_url }
|
||||
<%- end -%>
|
||||
format.json { head :no_content }
|
||||
end
|
||||
redirect_to <%= index_helper %>_url
|
||||
end
|
||||
|
||||
private
|
||||
|
|
|
@ -31,12 +31,10 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase
|
|||
assert_instance_method :create, content do |m|
|
||||
assert_match(/@user = User\.new\(user_params\)/, m)
|
||||
assert_match(/@user\.save/, m)
|
||||
assert_match(/@user\.errors/, m)
|
||||
end
|
||||
|
||||
assert_instance_method :update, content do |m|
|
||||
assert_match(/@user\.update\(user_params\)/, m)
|
||||
assert_match(/@user\.errors/, m)
|
||||
end
|
||||
|
||||
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"
|
||||
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"]
|
||||
|
||||
|
@ -176,7 +162,7 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase
|
|||
def test_new_hash_style
|
||||
run_generator
|
||||
assert_file "app/controllers/users_controller.rb" do |content|
|
||||
assert_match(/\{ render action: "new" \}/, content)
|
||||
assert_match(/render action: "new"/, content)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -41,12 +41,10 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase
|
|||
assert_instance_method :create, content do |m|
|
||||
assert_match(/@product_line = ProductLine\.new\(product_line_params\)/, m)
|
||||
assert_match(/@product_line\.save/, m)
|
||||
assert_match(/@product_line\.errors/, m)
|
||||
end
|
||||
|
||||
assert_instance_method :update, content do |m|
|
||||
assert_match(/@product_line\.update\(product_line_params\)/, m)
|
||||
assert_match(/@product_line\.errors/, m)
|
||||
end
|
||||
|
||||
assert_instance_method :destroy, content do |m|
|
||||
|
@ -158,12 +156,10 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase
|
|||
assert_instance_method :create, content do |m|
|
||||
assert_match(/@admin_role = Admin::Role\.new\(admin_role_params\)/, m)
|
||||
assert_match(/@admin_role\.save/, m)
|
||||
assert_match(/@admin_role\.errors/, m)
|
||||
end
|
||||
|
||||
assert_instance_method :update, content do |m|
|
||||
assert_match(/@admin_role\.update\(admin_role_params\)/, m)
|
||||
assert_match(/@admin_role\.errors/, m)
|
||||
end
|
||||
|
||||
assert_instance_method :destroy, content do |m|
|
||||
|
@ -257,11 +253,6 @@ 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