-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #73 from plaredspear/master
Custom Generator : Controller
- Loading branch information
Showing
4 changed files
with
163 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
90
lib/generators/api_controller/templates/api_controller_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |