Skip to content

Commit db68f6b

Browse files
committed
Generate api controller if Rails API option is enabled
1 parent 259cacb commit db68f6b

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<% if namespaced? -%>
2+
require_dependency "<%= namespaced_file_path %>/application_controller"
3+
4+
<% end -%>
5+
<% module_namespacing do -%>
6+
class <%= controller_class_name %>Controller < ApplicationController
7+
before_action :set_<%= singular_table_name %>, only: [:show, :update, :destroy]
8+
9+
# GET <%= route_url %>
10+
# GET <%= route_url %>.json
11+
def index
12+
@<%= plural_table_name %> = <%= orm_class.all(class_name) %>
13+
end
14+
15+
# GET <%= route_url %>/1
16+
# GET <%= route_url %>/1.json
17+
def show
18+
end
19+
20+
# POST <%= route_url %>
21+
# POST <%= route_url %>.json
22+
def create
23+
@<%= singular_table_name %> = <%= orm_class.build(class_name, "#{singular_table_name}_params") %>
24+
25+
if @<%= orm_instance.save %>
26+
render :show, status: :created, location: <%= "@#{singular_table_name}" %>
27+
else
28+
render json: <%= "@#{orm_instance.errors}" %>, status: :unprocessable_entity
29+
end
30+
end
31+
32+
# PATCH/PUT <%= route_url %>/1
33+
# PATCH/PUT <%= route_url %>/1.json
34+
def update
35+
if @<%= orm_instance.update("#{singular_table_name}_params") %>
36+
render :show, status: :ok, location: <%= "@#{singular_table_name}" %>
37+
else
38+
render json: <%= "@#{orm_instance.errors}" %>, status: :unprocessable_entity
39+
end
40+
end
41+
42+
# DELETE <%= route_url %>/1
43+
# DELETE <%= route_url %>/1.json
44+
def destroy
45+
@<%= orm_instance.destroy %>
46+
end
47+
48+
private
49+
# Use callbacks to share common setup or constraints between actions.
50+
def set_<%= singular_table_name %>
51+
@<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
52+
end
53+
54+
# Never trust parameters from the scary internet, only allow the white list through.
55+
def <%= "#{singular_table_name}_params" %>
56+
<%- if attributes_names.empty? -%>
57+
params[<%= ":#{singular_table_name}" %>]
58+
<%- else -%>
59+
params.require(<%= ":#{singular_table_name}" %>).permit(<%= attributes_names.map { |name| ":#{name}" }.join(', ') %>)
60+
<%- end -%>
61+
end
62+
end
63+
<% end -%>

lib/jbuilder/railtie.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class Railtie < ::Rails::Railtie
1313
ActiveSupport.on_load :action_controller do
1414
include ActionView::Rendering
1515
include ActionController::Helpers
16+
include ActionController::ImplicitRender
1617
end
1718
end
1819
end
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
require 'test_helper'
2+
require 'rails/generators/test_case'
3+
require 'generators/rails/scaffold_controller_generator'
4+
5+
if Rails::VERSION::MAJOR > 4
6+
7+
class ScaffoldApiControllerGeneratorTest < Rails::Generators::TestCase
8+
tests Rails::Generators::ScaffoldControllerGenerator
9+
arguments %w(Post title body:text --api)
10+
destination File.expand_path('../tmp', __FILE__)
11+
setup :prepare_destination
12+
13+
test 'controller content' do
14+
run_generator
15+
16+
assert_file 'app/controllers/posts_controller.rb' do |content|
17+
assert_instance_method :index, content do |m|
18+
assert_match /@posts = Post\.all/, m
19+
end
20+
21+
assert_instance_method :show, content do |m|
22+
assert m.blank?
23+
end
24+
25+
assert_instance_method :create, content do |m|
26+
assert_match /@post = Post\.new\(post_params\)/, m
27+
assert_match /@post\.save/, m
28+
assert_match /render :show, status: :created, location: @post/, m
29+
assert_match /render json: @post\.errors, status: :unprocessable_entity/, m
30+
end
31+
32+
assert_instance_method :update, content do |m|
33+
assert_match /render :show, status: :ok, location: @post/, m
34+
assert_match /render json: @post.errors, status: :unprocessable_entity/, m
35+
end
36+
37+
assert_instance_method :destroy, content do |m|
38+
assert_match /@post\.destroy/, m
39+
end
40+
41+
assert_match(/def post_params/, content)
42+
assert_match(/params\.require\(:post\)\.permit\(:title, :body\)/, content)
43+
end
44+
end
45+
end
46+
end

0 commit comments

Comments
 (0)