Skip to content

Commit

Permalink
Merge pull request #73 from plaredspear/master
Browse files Browse the repository at this point in the history
Custom Generator : Controller
  • Loading branch information
rorlab committed Apr 22, 2014
2 parents 6ba903e + 1a8843c commit 532337b
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/generators/api_controller/USAGE
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Description:
Explain the generator

Example:
rails generate api_controller Thing

This will create:
what/will/it/create
27 changes: 27 additions & 0 deletions lib/generators/api_controller/api_controller_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class ApiControllerGenerator < Rails::Generators::Base
source_root File.expand_path('../templates', __FILE__)
argument :name, type: :string, default: "apicontroller"

def generate_controller
template "api_controller.rb", "app/controllers/#{controller_name}_controller.rb"
template "api_controller_spec.rb", "spec/controllers/#{controller_name}_controller_spec.rb"
end

private

def controller_name
name.underscore
end

def controller_class
controller_name.capitalize + "Controller"
end

def model_name
controller_name[0...-1]
end

def model_class
model_name.capitalize
end
end
38 changes: 38 additions & 0 deletions lib/generators/api_controller/templates/api_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
class <%= controller_class %> < ApplicationController
before_action :set_<%= model_name %>, only: [:show, :update, :destroy]

def index
@<%= model_name %>s = <%= model_class %>.all
render json: @<%= model_name %>s
end
def show
render json: @<%= model_name %>
end

def create
@<%= model_name %> = <%= model_class %>.create(<%= model_name %>_params)
render json: @<%= model_name %>
end

def update
@<%= model_name %>.update(<%= model_name %>_params)
render json: @<%= model_name %>
end

def destroy
@<%= model_name %>.destroy
render json: { message: "destoryed" }
end
private
def set_<%= model_name %>
@<%= model_name %> = <%= model_class %>.find(params[:id])
end

def <%= model_name %>_params
params.require(:<%= model_name %>).permit()
end
end
90 changes: 90 additions & 0 deletions lib/generators/api_controller/templates/api_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
require 'spec_helper'

describe <%= controller_class %> do

let(:<%= model_name %>) { create(:<%= model_name %>) }
let(:valid_attributes) { attributes_for(:<%= model_name %>) }
let(:update_attributes) { }
let(:invalid_attributes) { }

describe 'GET #index' do
it "> 모든 <%= model_name %>(으)로 instance 변수를 할당한다." do
<%= model_name %>s = [<%= model_name %>, create(:<%= model_name %>)]
get :index
expect(assigns(:<%= model_name %>s)).to match_array(<%= model_name %>s)
end
end

describe 'GET #show' do
it "> 요청한 <%= model_name %>(으)로 instance 변수를 할당한다." do
get :show, id: <%= model_name %>
expect(assigns(:<%= model_name %>)).to eq <%= model_name %>
end
end
describe 'POST #create' do
context "1) params가 유효할 때" do
it "> 새로운 <%= model_name %>() 생성한다." do
expect{
post :create, <%= model_name %>: valid_attributes
}.to change(<%= model_class %>, :count).by(1)
end
it "> 새로운 <%= model_name %>() instance 변수를 할당한다." do
post :create, <%= model_name %>: valid_attributes
expect(assigns(:<%= model_name %>)).to be_a(<%= model_class %>)
expect(assigns(:<%= model_name %>)).to_not be_new_record
end
end
context "2) params가 유효하지 않을 " do
it "> 새로운 <%= model_name %>() 생성하지 않는다." do
expect{
post :create, <%= model_name %>: valid_attributes
}.to change(<%= model_class %>, :count)
end
it "> 새로운 <%= model_name %>() instance 변수를 할당한다." do
post :create, <%= model_name %>: invalid_attributes
expect(assigns(:<%= model_name %>)).to be_a(<%= model_class %>)
expect(assigns(:<%= model_name %>)).to be_new_record
end
end
end
describe 'PUT #update' do
context "1) params가 유효할 " do
it "> 요청한 <%= model_name %>() 업데이트한다." do
put :update, id: <%= model_name %>, <%= model_name %>: update_attributes
<%= model_name %>.reload
expect(<%= model_name %>).to eq <%= model_name %>
end
it "> 요청한 <%= model_name %>() instance 변수를 할당한다." do
patch :update, id: <%= model_name %>, <%= model_name %>: update_attributes
<%= model_name %>.reload
expect(assigns(:<%= model_name %>)).to eq <%= model_name %>
end
end
context "2) params가 유효하지 않을 " do
it "> 요청한 <%= model_name %>() 업데이트하지 않는다."
it "> 요청한 <%= model_name %>() instance 변수를 할당한다." do
patch :update, id: <%= model_name %>, <%= model_name %>: invalid_attributes
<%= model_name %>.reload
expect(assigns(:<%= model_name %>)).to eq <%= model_name %>
end
end
end
describe 'DELETE #destroy' do
it "> 요청한 <%= model_name %>() 삭제한다." do
<%= model_name %>
expect {
delete :destroy, id: <%= model_name %>
}.to change(<%= model_class %>, :count).by(-1)
end
end

end

0 comments on commit 532337b

Please sign in to comment.