Skip to content

Commit

Permalink
Generate api controller if Rails API option is enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
jmbejar committed Jun 8, 2015
1 parent 259cacb commit db68f6b
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 0 deletions.
63 changes: 63 additions & 0 deletions lib/generators/rails/templates/api_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<% if namespaced? -%>
require_dependency "<%= namespaced_file_path %>/application_controller"
<% end -%>
<% module_namespacing do -%>
class <%= controller_class_name %>Controller < ApplicationController
before_action :set_<%= singular_table_name %>, only: [:show, :update, :destroy]
# GET <%= route_url %>
# GET <%= route_url %>.json
def index
@<%= plural_table_name %> = <%= orm_class.all(class_name) %>
end
# GET <%= route_url %>/1
# GET <%= route_url %>/1.json
def show
end
# POST <%= route_url %>
# POST <%= route_url %>.json
def create
@<%= singular_table_name %> = <%= orm_class.build(class_name, "#{singular_table_name}_params") %>

if @<%= orm_instance.save %>
render :show, status: :created, location: <%= "@#{singular_table_name}" %>
else
render json: <%= "@#{orm_instance.errors}" %>, status: :unprocessable_entity
end
end
# PATCH/PUT <%= route_url %>/1
# PATCH/PUT <%= route_url %>/1.json
def update
if @<%= orm_instance.update("#{singular_table_name}_params") %>
render :show, status: :ok, location: <%= "@#{singular_table_name}" %>
else
render json: <%= "@#{orm_instance.errors}" %>, status: :unprocessable_entity
end
end

# DELETE <%= route_url %>/1
# DELETE <%= route_url %>/1.json
def destroy
@<%= orm_instance.destroy %>
end
private
# Use callbacks to share common setup or constraints between actions.
def set_<%= singular_table_name %>
@<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
end

# Never trust parameters from the scary internet, only allow the white list through.
def <%= "#{singular_table_name}_params" %>
<%- if attributes_names.empty? -%>
params[<%= ":#{singular_table_name}" %>]
<%- else -%>
params.require(<%= ":#{singular_table_name}" %>).permit(<%= attributes_names.map { |name| ":#{name}" }.join(', ') %>)
<%- end -%>
end
end
<% end -%>
1 change: 1 addition & 0 deletions lib/jbuilder/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Railtie < ::Rails::Railtie
ActiveSupport.on_load :action_controller do
include ActionView::Rendering
include ActionController::Helpers
include ActionController::ImplicitRender
end
end
end
Expand Down
46 changes: 46 additions & 0 deletions test/scaffold_api_controller_generator_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require 'test_helper'
require 'rails/generators/test_case'
require 'generators/rails/scaffold_controller_generator'

if Rails::VERSION::MAJOR > 4

class ScaffoldApiControllerGeneratorTest < Rails::Generators::TestCase
tests Rails::Generators::ScaffoldControllerGenerator
arguments %w(Post title body:text --api)
destination File.expand_path('../tmp', __FILE__)
setup :prepare_destination

test 'controller content' do
run_generator

assert_file 'app/controllers/posts_controller.rb' do |content|
assert_instance_method :index, content do |m|
assert_match /@posts = Post\.all/, m
end

assert_instance_method :show, content do |m|
assert m.blank?
end

assert_instance_method :create, content do |m|
assert_match /@post = Post\.new\(post_params\)/, m
assert_match /@post\.save/, m
assert_match /render :show, status: :created, location: @post/, m
assert_match /render json: @post\.errors, status: :unprocessable_entity/, m
end

assert_instance_method :update, content do |m|
assert_match /render :show, status: :ok, location: @post/, m
assert_match /render json: @post.errors, status: :unprocessable_entity/, m
end

assert_instance_method :destroy, content do |m|
assert_match /@post\.destroy/, m
end

assert_match(/def post_params/, content)
assert_match(/params\.require\(:post\)\.permit\(:title, :body\)/, content)
end
end
end
end

0 comments on commit db68f6b

Please sign in to comment.