/
base.rb
208 lines (172 loc) · 5.57 KB
/
base.rb
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
run "rm public/index.html"
run "rm public/images/rails.png"
run "rm README"
run "cp config/database.yml config/database.yml.example"
run "rm public/favicon.ico"
run "rm public/robots.txt"
file '.gitignore', <<-END
.DS_Store
log/*.log
tmp/**/*
config/database.yml
db/*.sqlite3
END
git :init
git :add => "."
git :commit => '-m "Initial commit."'
plugin 'asset_packager', :git => 'git://github.com/sbecker/asset_packager.git'
plugin 'hoptoad_notifier', :git => 'git://github.com/thoughtbot/hoptoad_notifier.git'
hoptoad_key = ask("What is your Hoptoad API key?")
file 'config/initializers/hoptoad.rb', <<-CODE
HoptoadNotifier.configure do |config|
config.api_key = "#{hoptoad_key}"
end
CODE
gem 'authlogic'
gem 'cucumber'
gem 'rspec', :lib => false
gem 'rspec-rails', :lib => false
gem 'thoughtbot-factory_girl', :lib => 'factory_girl', :source => 'http://gems.github.com'
gem 'carlosbrando-remarkable', :lib => 'remarkable', :source => 'http://gems.github.com'
gem 'webrat'
gem 'mislav-will_paginate', :version => '~> 2.2.3',
:lib => 'will_paginate', :source => 'http://gems.github.com'
if yes?("Run rake gems:install? (yes/no)")
rake("gems:install", :sudo => true)
end
if yes?("Unpack gems? (yes/no)")
rake("gems:unpack")
end
if yes?("Freeze Rails? (yes/no)")
freeze!
end
run "git rm public/javascripts/controls.js public/javascripts/dragdrop.js public/javascripts/effects.js public/javascripts/prototype.js"
run "curl -L http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js > public/javascripts/jquery.js"
run "curl -L http://jqueryjs.googlecode.com/svn/trunk/plugins/form/jquery.form.js > public/javascripts/jquery.form.js"
generate('rspec')
generate('cucumber')
generate('session', 'user_session')
generate('model', 'User login:string crypted_password:string password_salt:string persistence_token:string login_count:integer last_request_at:datetime last_login_at:datetime current_login_at:datetime last_login_ip:string current_login_ip:string')
file 'app/models/user.rb', <<-CODE
class User < ActiveRecord::Base
acts_as_authentic
end
CODE
file 'app/controllers/user_sessions_controller.rb', <<-CODE
class UserSessionsController < ApplicationController
before_filter :require_no_user, :only => [:new, :create]
before_filter :require_user, :only => :destroy
def new
@user_session = UserSession.new
end
def create
@user_session = UserSession.new(params[:user_session])
if @user_session.save
flash[:notice] = "Login successful!"
redirect_back_or_default account_url
else
render :action => :new
end
end
def destroy
current_user_session.destroy
flash[:notice] = "Logout successful!"
redirect_back_or_default new_user_session_url
end
end
CODE
file 'app/controllers/users_controller.rb', <<-CODE
class UsersController < ApplicationController
before_filter :require_no_user, :only => [:new, :create]
before_filter :require_user, :only => [:show, :edit, :update]
def new
@user = User.new
end
def create
@user = User.new(params[:user])
if @user.save
flash[:notice] = "Account registered!"
redirect_back_or_default account_url
else
render :action => :new
end
end
def show
@user = @current_user
end
def edit
@user = @current_user
end
def update
@user = @current_user # makes our views "cleaner" and more consistent
if @user.update_attributes(params[:user])
flash[:notice] = "Account updated!"
redirect_to account_url
else
render :action => :edit
end
end
end
CODE
file 'app/controllers/application_controller.rb', <<-CODE
class ApplicationController < ActionController::Base
filter_parameter_logging :password, :password_confirmation
helper_method :current_user_session, :current_user
private
def current_user_session
return @current_user_session if defined?(@current_user_session)
@current_user_session = UserSession.find
end
def current_user
return @current_user if defined?(@current_user)
@current_user = current_user_session && current_user_session.user
end
def require_user
unless current_user
store_location
flash[:notice] = "You must be logged in to access this page"
redirect_to new_user_session_url
return false
end
end
def require_no_user
if current_user
store_location
flash[:notice] = "You must be logged out to access this page"
redirect_to account_url
return false
end
end
def store_location
session[:return_to] = request.request_uri
end
def redirect_back_or_default(default)
redirect_to(session[:return_to] || default)
session[:return_to] = nil
end
end
CODE
file 'public/javascripts/application.js', <<-CODE
jQuery.ajaxSetup({
'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")}
});
CODE
route 'map.resource :user_session'
route 'map.root :controller => "user_sessions", :action => "new"'
route 'map.resource :account, :controller => "users"'
route 'map.resources :users'
git :add => "."
git :commit => '-m "Adding templates, plugins and gems"'
if yes?("Create and migrate databases now? (yes/no)")
rake("db:create:all")
rake("db:migrate")
git :add => "."
git :commit => '-m "First migration adding users"'
end
puts "#" * 30
puts "TO-DO checklist:"
puts "\t* Write specs for the User model and controller"
puts "\t* Create views for Authlogic - see http://github.com/binarylogic/authlogic_example/tree/master/app/views for examples"
puts "\t* Test your Hoptoad installation with: rake hoptoad:test"
puts "\t* Generate your asset_packager config with: rake asset:packager:create_yml"
puts "#" * 30