-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRakefile
More file actions
121 lines (106 loc) · 3.42 KB
/
Copy pathRakefile
File metadata and controls
121 lines (106 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
require 'rake'
require 'rake/testtask'
require 'sinatra/activerecord/rake'
require 'yard'
ENV['RACK_ENV'] ||= 'development'
task :environment do
require File.expand_path '../config/boot.rb', __FILE__
end
namespace :db do
task :migrate => [:environment] do
Rake::Task['db:migrate'].invoke
end
end
desc 'Start an app console (can also just call racksh)'
task :console do |t|
exec 'racksh'
end
desc 'Generate project API documentation with Yard'
task :yard => [:environment] do |t|
exec "yardoc #{LOAD_PATHS.join(' ')}"
end
desc 'Start an app server'
task :server do |t|
exec "RACK_ENV=#{ENV['RACK_ENV']} rackup"
end
Rake::TestTask.new do |t|
t.pattern = 'test/**/*_test.rb'
end
namespace :generate do
desc 'Generate an empty model and migration'
task :model => [:environment] do
unless ENV['NAME']
puts 'Please provide the model name with NAME='
return
end
name = ENV['NAME'].dup
puts "Generating a migration for #{name}"
ENV['NAME'] = "create_#{name.underscore}"
Rake::Task['db:create_migration'].invoke
ENV['NAME'] = name
generate_model name
end
desc 'Generate a model, migration, and scaffold controller.'
task :resource => [:environment, 'generate:model'] do
generate_resource ENV['NAME']
end
def generate_resource(name)
rsrc_name = name.camelize
rsrc_template = <<-TPL.gsub(/^ {6}/, '')
module Controllers
class #{rsrc_name}Controller < ApplicationController
# @!method get_index
# @overload get '/'
# Gets a paginated list of JSON-encoded{#{rsrc_name}}s
get '/' do
json #{rsrc_name}.page(params['page']).per(params['per'])
end
# @!method get_show
# @overload get '/:id'
# Gets a JSON-encoded {#{rsrc_name}} by id
get '/:id' do
json #{rsrc_name}.find params[:id]
end
# @!method post_create
# @overload post '/'
# Creates a new {#{rsrc_name}} from the data in the request body
post '/' do
data = JSON.parse request.body.read.to_s
#{name} = #{rsrc_name}.new data
#{name}.save
json #{name}
end
# @!method put_update
# @overload put '/:id'
# Updates a {#{rsrc_name}} by id
put '/:id' do
#{name} = #{rsrc_name}.find params[:id]
data = JSON.parse request.body.read.to_s
#{name}.update_attributes data
json #{name}
end
# @!method delete_destroy
# @overload delete '/:id'
# Deletes a {#{rsrc_name}} by id
delete '/:id' do
json #{rsrc_name}.find(params[:id]).destroy
end
end
end
TPL
rsrc_file = "#{File.expand_path('../app/controllers', __FILE__)}/#{name.pluralize.underscore}_controller.rb"
puts "Generating controller scaffold for #{name} model at #{rsrc_file}"
File.open(rsrc_file, 'w') { |f| f.write rsrc_template }
end
def generate_model(name)
cls_template = <<-TPL.gsub(/^ {6}/, '')
class #{name.camelize} < ActiveRecord::Base
# @!attribute id
# @return [Integer] The primary key of the {#{name.camelize}}
end
TPL
cls_file = "#{File.expand_path('../app/models', __FILE__)}/#{name.underscore}.rb"
puts "Generating empty #{name} model at #{cls_file}"
File.open(cls_file, 'w') { |f| f.write cls_template }
end
end