yfactorial / rails-templates

Our particular collection of Rails app generator templates

This URL has Read+Write access

rails-templates / standard_template.rb
100644 349 lines (272 sloc) 8.237 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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
gem 'haml', :version => '2.1.0'
gem "yfactorial-utility_scopes", :lib => 'utility_scopes',
  :source => 'http://gems.github.com/'
gem 'mislav-will_paginate', :lib => 'will_paginate',
  :source => 'http://gems.github.com'
  
plugin "rspec", :git => "git://github.com/dchelimsky/rspec.git"
plugin "rspec-rails", :git => "git://github.com/dchelimsky/rspec-rails.git"
plugin 'exception_notifier', :git => 'git://github.com/rails/exception_notification.git'
plugin "dataset", :git => "git://github.com/aiwilliams/dataset.git"
plugin "make_resourceful", :git => "git://github.com/hcatlin/make_resourceful.git"
 
generate :rspec
run "haml --rails ."
  
rake "gems:install"
rake "gems:unpack"
rake "rails:freeze:gems"
 
file ".gitignore", <<-END
.DS_Store
log/*.log
tmp/**/*
config/database.yml
db/*.sqlite3
*.tmproj
END
 
generate :controller, "root"
route "map.root :controller => :root"
 
route "map.login 'login', :controller => :sessions, :action => 'new'"
route "map.logout 'logout', :controller => :sessions, :action => 'destroy', :conditions => { :method => :delete }"
 
generate :resource, "user", "login:string", "email:string", "salt:string", "crypted_password:string"
route "map.register 'register', :controller => :users, :action => 'new'"
 
route "map.resources :sessions, :only => [:new, :create, :destroy]"
route "map.resources :users, :only => [:new, :create]"
 
initializer 'will_paginate.rb', <<-CODE
ActiveRecord::Base.class_eval { def self.per_page; 10; end }
CODE
 
rakefile("db.rake") do
  <<-TASK
namespace :db do
desc "Drop the dbs, and does a full migrate to bring it back up"
task :revert => ['db:drop', 'db:create', 'db:migrate']
end
TASK
end
 
# TODO: We should put this crap into a generator
file "app/models/authentication/user_auth.rb", <<-END
require 'digest/sha2'
 
module Authentication
module User
module ClassMethods
def digest(password, salt)
Digest::SHA512.hexdigest("\#{password}\#{salt}")
end
def authenticate(login, password)
u = find_by_login(login)
return (u && u.crypted_password == digest(password, u.salt)) ? u : nil
end
end
module InstanceMethods
def hash_password
if password_changed?
self.salt = ActiveSupport::SecureRandom.hex(10) if !salt
self.crypted_password = self.class.digest(password, salt)
end
end
def password_changed?
new_record? or !password.blank? or !password_confirmation.blank?
end
end
end
end
END
 
file "app/models/user.rb", <<-END
require 'authentication/user_auth'
 
class User < ActiveRecord::Base
attr_accessor :password, :password_confirmation
extend Authentication::User::ClassMethods
include Authentication::User::InstanceMethods
validates_presence_of :password, :if => :password_changed?
validates_confirmation_of :password, :if => :password_changed?
before_validation :hash_password
end
END
 
file "app/controllers/sessions_controller.rb", <<-END
class SessionsController < ApplicationController
make_resourceful do
actions :new, :create, :destroy
response_for :create do |wants|
wants.html do
flash[:notice] = "You've successfully logged in."
redirect_back_or home_path
end
end
response_for :create_fails do |wants|
wants.html do
flash[:error] = "Could not find a user with that login and password. Please try again or register as a new user."
render :action => 'new'
end
end
response_for :destroy do |wants|
wants.html do
flash[:notice] = "You've successfully logged out."
redirect_to root_path
end
end
end
def create
u = User.authenticate(params[:email], params[:password])
if u
login(u)
response_for :create
else
response_for :create_fails
end
end
def destroy
logout
response_for :destroy
end
end
END
 
file "app/controllers/application_controller.rb", <<-END
class ApplicationController < ActionController::Base
include ExceptionNotifiable
helper :all
protect_from_forgery
filter_parameter_logging :password
# Let our views use these methods
helper_method :current_user, :logged_in?
# Always watch for redirect_tos
before_filter :save_redirect_to
#-- Authentication support
 
def current_user
@current_user ||= logged_in? ? User.find(session[:user_id]) : nil
end
def logged_in?
(@logged_in ||= (!session[:user_id].blank? and User.exists?(session[:user_id])) ? 1 : 0) > 0
end
# Log this user in
def login(user)
session[:user_id] = user.id
@logged_in = nil
end
def logout
session[:user_id] = nil
@logged_in = nil
end
#-- Session state helpers
def redirect_back_or(location)
back = session[:redirect_to]
redirect_to(back || location)
session[:redirect_to] = nil
end
#-- Paging
def paging_params
{ :page => params[:page] || 1 }
end
#-- Common filters
def save_redirect_to
session[:redirect_to] = params[:redirect_to] if params[:redirect_to]
end
def require_logged_in
if not logged_in?
respond_to do |wants|
wants.html do
flash[:error] = "You must be logged-in to access that page."
session[:redirect_to] = request.request_uri
redirect_to login_path
end
wants.js { head :status => 401 }
end
end
end
 
end
END
 
file "app/helpers/application_helper", <<-END
module ApplicationHelper
 
# Print out all flash messages in a span of the same
# class as the message type
def flash_messages
html = flash.collect do |type, message|
content_tag(:div, message, :class => type)
end
flash.clear # Not sure why we have to manually do this sometimes
html
end
# Are there any flash messages to display?
def flash_messages?; flash.any?; end
# Set the page title
def page_title(title)
content_for :page_title, title
end
# Set the html header title
def head_title(title)
content_for :head_title, title
end
# Get the authenticity token (useful for an ajax call)
def auth_token
(protect_against_forgery? ? form_authenticity_token : nil)
end
end
END
 
file "app/views/layouts/application.html.haml", <<-END
!!! Strict
%html{:xmlns=>"http://www.w3.org/1999/xhtml"}
%head
%title= "AppName: \#{(yield :head_title) || (yield :page_title)}"
%body
#container
#page
#masthead
%h1= link_to('AppName', root_path)
%ul#user_nav
- if logged_in?
%li= link_to 'Logout', logout_path, :method => :delete
%li= "You are logged in as #{current_user}"
- else
%li= link_to 'Login', login_path
#page-title
%h3= "\#{yield :page_title}"
 
- if flash_messages?
#messages
= flash_messages
= yield
.clear
END
 
file "app/views/sessions/new.html.haml", <<-END
- page_title 'Login'
 
- form_tag sessions_path do
 
= label_tag :login
= text_field_tag :login, params[:login]
%br/
= label_tag :password
= password_field_tag :password, params[:password]
%br/
 
.submit
= submit_tag "Login"
= link_to 'Register', register_path
END
 
file "app/controllers/users_controller.rb", <<-END
class UsersController < ApplicationController
make_resourceful do
actions :new, :create
response_for :create do |wants|
wants.html do
flash[:notice] = "You have been logged in."
redirect_to root_path
end
end
end
end
END
 
file "app/views/users/new.html.haml", <<-END
- page_title "Register"
 
- form_for @user do |f|
 
= f.error_messages
 
= f.label :login
= f.text_field :login
%br/
= f.label :password
= f.password_field :password
%br/
 
= f.label 'Confirm Password:'
= f.password_field :password_confirmation
%br/
.submit
= f.submit "Register"
= link_to 'Cancel', root_path
END
 
generate :migration, "release001"
 
run "rm -rf test"
run "rm public/index.html"
run "cp config/database.yml config/example_database.yml"
run "git add * .gitignore"