-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRakefile
190 lines (157 loc) · 5.98 KB
/
Rakefile
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
require ::File.expand_path('../config/environment', __FILE__)
# Include all of ActiveSupport's core class extensions, e.g., String#camelize
require 'active_support/core_ext'
namespace :generate do
desc "Create an empty model in app/models, e.g., rake generate:model NAME=User"
task :model do
unless ENV.has_key?('NAME')
raise "Must specificy model name, e.g., rake generate:model NAME=User"
end
model_name = ENV['NAME'].camelize
model_filename = ENV['NAME'].underscore + '.rb'
model_path = APP_ROOT.join('app', 'models', model_filename)
if File.exist?(model_path)
raise "ERROR: Model file '#{model_path}' already exists"
end
puts "Creating #{model_path}"
File.open(model_path, 'w+') do |f|
f.write(<<-EOF.strip_heredoc)
class #{model_name} < ActiveRecord::Base
# Remember to create a migration!
end
EOF
end
end
task :resource do
desc "Create a resource file in app/controllers, e.g., rake generate:resource NAME=User"
unless ENV.has_key?('NAME')
raise "Must specificy resource name, e.g., rake generate:resource NAME=User"
end
resource_name = ENV['NAME'].downcase
resource_filename = ENV['NAME'].underscore + '.rb'
resource_path = APP_ROOT.join('app', 'controllers', resource_filename)
plural_resource_name = resource_name.pluralize
cap_resource_name = ENV['NAME'].capitalize
puts "Creating #{resource_name}"
File.open(resource_path, 'w+') do |f|
f.write(<<-EOF.strip_heredoc)
get '/#{plural_resource_name}' do
# display a list of all #{plural_resource_name}
# @#{plural_resource_name} = #{cap_resource_name}.all
# erb :'#{plural_resource_name}/index'
end
get '/#{plural_resource_name}/new' do
# return an HTML form for creating a new #{resource_name}
# erb :'#{plural_resource_name}/new'
end
post '/#{plural_resource_name}' do
# create a new #{resource_name}
# @#{resource_name} = #{cap_resource_name}.create(params[:#{resource_name}])
# redirect '/#{plural_resource_name}'
end
get '/#{plural_resource_name}/:id' do |id|
# display a specific #{resource_name}
# @#{resource_name} = #{cap_resource_name}.find id
# erb :'#{plural_resource_name}/single'
end
get '/#{plural_resource_name}/:id/edit' do |id|
# return an HTML form for editing a #{resource_name}
# @#{resource_name} = #{cap_resource_name}.find id
# erb :'#{plural_resource_name}/edit'
end
put '/#{plural_resource_name}/:id' do |id|
# update a specific #{resource_name}
# @#{resource_name} = #{cap_resource_name}.find id
# @#{resource_name}.update(params[:#{resource_name}])
redirect 'entries/\#\{@#{resource_name}.id\}'
end
delete '/#{plural_resource_name}/:id' do |id|
# delete a specific #{resource_name}
# @#{resource_name} = #{cap_resource_name}.find id
# @#{resource_name}.destroy
# redirect '/#{plural_resource_name}'
end
EOF
end
end
desc "Create an empty migration in db/migrate, e.g., rake generate:migration NAME=create_tasks"
task :migration do
unless ENV.has_key?('NAME')
raise "Must specificy migration name, e.g., rake generate:migration NAME=create_tasks"
end
name = ENV['NAME'].camelize
filename = "%s_%s.rb" % [Time.now.strftime('%Y%m%d%H%M%S'), ENV['NAME'].underscore]
path = APP_ROOT.join('db', 'migrate', filename)
if File.exist?(path)
raise "ERROR: File '#{path}' already exists"
end
puts "Creating #{path}"
File.open(path, 'w+') do |f|
f.write(<<-EOF.strip_heredoc)
class #{name} < ActiveRecord::Migration
def change
end
end
EOF
end
end
desc "Create an empty model spec in spec, e.g., rake generate:spec NAME=user"
task :spec do
unless ENV.has_key?('NAME')
raise "Must specificy migration name, e.g., rake generate:spec NAME=user"
end
name = ENV['NAME'].camelize
filename = "%s_spec.rb" % ENV['NAME'].underscore
path = APP_ROOT.join('spec', filename)
if File.exist?(path)
raise "ERROR: File '#{path}' already exists"
end
puts "Creating #{path}"
File.open(path, 'w+') do |f|
f.write(<<-EOF.strip_heredoc)
require 'spec_helper'
describe #{name} do
pending "add some examples to (or delete) #{__FILE__}"
end
EOF
end
end
end
namespace :db do
desc "Create the database at #{DB_NAME}"
task :create do
puts "Creating database #{DB_NAME} if it doesn't exist..."
exec("createdb #{DB_NAME}")
end
desc "Drop the database at #{DB_NAME}"
task :drop do
puts "Dropping database #{DB_NAME}..."
exec("dropdb #{DB_NAME}")
end
desc "Migrate the database (options: VERSION=x, VERBOSE=false, SCOPE=blog)."
task :migrate do
ActiveRecord::Migrator.migrations_paths << File.dirname(__FILE__) + 'db/migrate'
ActiveRecord::Migration.verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] == "true" : true
ActiveRecord::Migrator.migrate(ActiveRecord::Migrator.migrations_paths, ENV["VERSION"] ? ENV["VERSION"].to_i : nil) do |migration|
ENV["SCOPE"].blank? || (ENV["SCOPE"] == migration.scope)
end
end
desc "Populate the database with dummy data by running db/seeds.rb"
task :seed do
require APP_ROOT.join('db', 'seeds.rb')
end
desc "rollback your migration--use STEPS=number to step back multiple times"
task :rollback do
steps = (ENV['STEPS'] || 1).to_i
ActiveRecord::Migrator.rollback('db/migrate', steps)
Rake::Task['db:version'].invoke if Rake::Task['db:version']
end
desc "Returns the current schema version number"
task :version do
puts "Current version: #{ActiveRecord::Migrator.current_version}"
end
end
desc 'Start IRB with application environment loaded'
task "console" do
exec "irb -r./config/environment"
end