bskahan (owner)

Fork Of

gist: 75038 by langalex upstream rails application ...

Revisions

gist: 107371 Download_button fork
public
Public Clone URL: git://gist.github.com/107371.git
Embed All Files: show embed
upstream_rails_application_template.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
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
# rails application template for generating customized rails apps
#
# == requires ==
#
# * rails 2.3+, rspec, cucumber, culerity (langalex-culerity gem), machinist
#
# == a newly generated app using this template comes with ==
#
# * working user registration/login via authlogic, cucumber features to verify that it works
# * rspec/cucumber/culerity for testing
# * jquery set up
#
# == how to use ==
#
# * install the required gems
#
# == TODO ==
# * add forgot password method
# * make registration/login use resource_controller
 
app_name = `pwd`.split('/').last.strip
 
# ===========================================================================
# Remove Rails boilerplate
# ===========================================================================
 
run "rm README"
run "rm -rf test"
run "rm public/index.html"
run "rm public/favicon.ico"
run "rm public/robots.txt"
run "rm public/images/rails.png"
run "rm -f public/javascripts/*"
 
# ===========================================================================
# get jquery and plugins
# ===========================================================================
 
run "curl -L http://jqueryjs.googlecode.com/files/jquery-1.3.2.js > public/javascripts/jquery.js"
 
# ===========================================================================
# Set up environment, db, and routes
# ===========================================================================
 
file 'config/environment.rb', <<-FILE
RAILS_GEM_VERSION = '2.3.2' unless defined? RAILS_GEM_VERSION
require File.join(File.dirname(__FILE__), 'boot')
Rails::Initializer.run do |config|
# config.load_paths += %W( \#{RAILS_ROOT}/extras )
# config.plugins = [ :exception_notification, :ssl_requirement, :all ]
# config.frameworks -= [ :active_record, :active_resource, :action_mailer ]
# config.active_record.observers = :cacher, :garbage_collector, :forum_observer
# config.time_zone = 'UTC'
# config.i18n.default_locale = :de
end
FILE
 
run "cp config/database.yml config/database.yml.example"
rake 'db:create'
 
# routes
 
file 'config/routes.rb', <<-FILE
ActionController::Routing::Routes.draw do |map|
end
FILE
 
# ===========================================================================
# Set up default layout
# ===========================================================================
 
file 'app/views/layouts/default.html.erb', <<-FILE
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 
<head>
<title>#{app_name}</title>
<%= stylesheet_link_tag 'screen', :media => 'all' %>
<%= stylesheet_link_tag 'layout', :media => 'screen' %>
<%= stylesheet_link_tag 'print', :media => 'print' %>
 
<%= javascript_include_tag 'jquery', :cache => true %>
 
<%= yield(:head) %>
 
<script type="text/javascript">
$(function() {
<%= yield(:jquery) %>
});
</script>
</head>
 
<body id="www-#{app_name}-com">
 
<div id="AccessibilityLinks">
<a name="PageTop" id="PageTop"><!-- link target --></a>
<p><strong>Jump to:</strong>
<a href="#ContentTop" title="Skip to this page's main content">Page Content</a>,
<a href="#SiteNavigation" title="Skip to the site navigation">Site Navigation</a>,
<a href="#SiteSearch" title="Skip to the site-wide search form">Site Search</a>,
</p>
</div>
 
<!-- BEGIN: page -->
<div id="PageWrapper">
 
<!-- BEGIN: layout -->
<div id="LayoutWrapper">
 
<!-- BEGIN: header -->
<div id="HeaderWrapper">
 
</div>
<!-- END: header -->
 
<hr />
 
<!-- BEGIN: body -->
<div id="BodyWrapper">
<a id="ContentTop" name="ContentTop"><!-- link target --></a>
 
<!-- BEGIN: main column -->
<div id="MainColumn">
<%- if flash[:notice] -%>
<div id="flash"><%= flash[:notice] %></div>
<%- end -%>
<%= yield %>
</div>
<!-- END: main column -->
 
<!-- BEGIN: side column -->
<div id="SideColumn">
 
</div>
<!-- END: side column -->
 
</div>
<!-- END: body -->
 
<hr />
 
<!-- BEGIN: footer -->
<div id="FooterWrapper">
 
</div>
<!-- END: footer -->
 
</div>
<!-- END: layout -->
 
</div>
<!-- END: page -->
 
</body>
 
</html>
FILE
 
# ===========================================================================
# Initialize Git repository
# ===========================================================================
 
# newgit.rb
# from Joao Vitor
 
# Creates a new rails application using git
# Initializes the git based on the sake task published on
# http://gist.github.com/6750
# task 'git:rails:new_app', :needs => [ 'rails:rm_tmp_dirs', 'git:hold_empty_dirs' ]
 
# rails:rm_tmp_dirs
["./tmp/pids", "./tmp/sessions", "./tmp/sockets", "./tmp/cache"].each do |f|
  run("rmdir ./#{f}")
end
 
# git:hold_empty_dirs
run("find . \\( -type d -empty \\) -and \\( -not -regex ./\\.git.* \\) -exec touch {}/.gitignore \\;")
 
# git:rails:new_app
git :init
 
run "touch tmp/.gitignore log/.gitignore vendor/.gitignore"
run %{find . -type d -empty | grep -v "vendor" | grep -v ".git" | grep -v "tmp" | xargs -I xxx touch xxx/.gitignore}
file '.gitignore', <<-CODE
log/\\*.log
log/\\*.pid
db/\\*.db
db/\\*.sqlite3
db/schema.rb
tmp/\\*\\*/\\*
.DS_Store
doc/api
doc/app
config/database.yml
CODE
 
git :add => "."
 
git :commit => "-a -m 'Setting up a new rails app. Copy config/database.yml.example to config/database.yml and customize.'"
 
# gems
gem 'mislav-will_paginate', :version => '~> 2.2.3', :lib => 'will_paginate', :source => 'http://gems.github.com'
gem 'authlogic'
gem 'giraffesoft-resource_controller', :lib => 'resource_controller', :source => 'http://gems.github.com'
 
rake 'gems:install', :sudo => true
 
# generators
generate("rspec")
generate("rspec-rails")
 
# authlogic login/signup
 
generate("session user_session")
generate 'rspec_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'
 
 
route "map.root :controller => 'users', :action => 'new'"
route 'map.resource :user_session'
route "map.resource :account, :controller => 'users'"
route 'map.resources :users'
 
 
file "app/controllers/user_sessions_controller.rb", <<-FILE
class UserSessionsController < ApplicationController
require_user :only => :destroy
resource_controller
actions :new, :create
create do
flash "Login successful!"
success.wants.html do
redirect_back_or_default account_url
end
end
def destroy
current_user_session.destroy
flash[:notice] = "Logout successful!"
redirect_back_or_default new_user_session_url
end
end
FILE
 
file 'app/controllers/users_controller.rb', <<-FILE
class UsersController < ApplicationController
require_user :only => [:show, :edit, :update]
resource_controller
actions :new, :create, :show, :edit, :update
create do
flash "Account registered!"
success.wants.html do
redirect_back_or_default account_url
end
end
update.flash "Account updated!"
def object
@object ||= current_user
end
end
FILE
 
file 'app/models/user.rb', <<-FILE
class User < ActiveRecord::Base
acts_as_authentic
end
FILE
 
file 'app/controllers/application_controller.rb', <<-FILE
class ApplicationController < ActionController::Base
helper :all
protect_from_forgery
prepend_before_filter :activate_authlogic
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 self.require_user(options = {})
before_filter :require_user, options
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
FILE
 
file 'app/views/users/new.html.erb', <<-FILE
<h1>Register</h1>
<% form_for @user, :url => account_path do |f| %>
<%= f.error_messages %>
<%= render :partial => "form", :object => f %>
<%= f.submit "Register" %>
<% end %>
FILE
 
file 'app/views/users/_form.html.erb', <<-FILE
<%= form.label :login %><br />
<%= form.text_field :login %><br />
<br />
<%= form.label :password, form.object.new_record? ? nil : "Change password" %><br />
<%= form.password_field :password %><br />
<br />
<%= form.label :password_confirmation %><br />
<%= form.password_field :password_confirmation %><br />
<br />
FILE
 
file 'app/views/users/edit.html.erb', <<-FILE
<h1>Edit My Account</h1>
<% form_for @user, :url => account_path do |f| %>
<%= f.error_messages %>
<%= render :partial => "form", :object => f %>
<%= f.submit "Update" %>
<% end %>
<br /><%= link_to "My Profile", account_path %>
FILE
 
file 'app/views/users/show.html.erb', <<-FILE
<p>Welcome <%=h @user.login %></p>
<%= link_to 'Edit Account', edit_account_path %>
FILE
 
file 'app/views/user_sessions/new.html.erb', <<-FILE
<h1>Login</h1>
<% form_for @user_session, :url => user_session_path do |f| %>
<%= f.error_messages %>
<%= f.label :login %><br />
<%= f.text_field :login %><br />
<br />
<%= f.label :password %><br />
<%= f.password_field :password %><br />
<br />
<%= f.check_box :remember_me %><%= f.label :remember_me %><br />
<br />
<%= f.submit "Login" %>
<% end %>
FILE
 
# login/signup features
 
file 'features/log_in.feature', <<-FILE
Feature: log in
In order to use the system
As a user
I want to log in
Scenario: log in
Given a user "alex" with the password "testtest"
When I go to the start page
And I follow "Log in"
And I fill in "alex" for "Login"
And I fill in "testtest" for "Password"
And I press "Login"
Then I should see "Welcome alex"
And I should see "Login successful!"
 
Scenario: log out
Given a user "alex" with the password "testtest"
And "alex" is logged in
When I go to the account page
And I follow "Log out"
Then I should see "Log in"
And I should see "Logout successful!"
Scenario: edit account
Given a user "alex" with the password "testtest"
And "alex" is logged in
When I go to the account page
And I follow "Edit Account"
And I fill in "joe" for "Login"
And I press "Update"
Then I should see "Account updated!"
FILE
 
file 'features/sign_up.feature', <<-FILE
Feature: sign up
In order to use all the platform's features
As a user
I want to sign up
Scenario: sign up successfully
When I go to the start page
And I follow "Sign up"
And I fill in "alex" for "Login"
And I fill in "testtest" for "Password"
And I fill in "testtest" for "Password confirmation"
And I press "Register"
Then I should see "Welcome alex"
 
Scenario: signing up fails because login is taken
Given a user "alex"
When I go to the start page
And I follow "Sign up"
And I fill in "alex" for "Login"
And I fill in "testtest" for "Password"
And I fill in "testtest" for "Password confirmation"
And I press "Register"
Then I should not see "Welcome alex"
And I should see "Login ist bereits vergeben"
FILE
 
file 'features/step_definitions/user_steps.rb', <<-FILE
Before do
User.delete_all
end
 
Given /^a user "(.+)" with the password "(.+)"$/ do |login, password|
User.make :login => login, :password => password, :password_confirmation => password
end
 
Given /a user "([^"]+)"$/ do |login|
User.make :login => login
end
 
Given /^"([^"]+)" is logged in$/ do |login|
When 'I go to the start page'
When 'I follow "Log in"'
When "I fill in \\\"\#{login}\\\" for \\\"Login\\\""
When 'I fill in "testtest" for "Password"'
When 'I press "Login"'
end
FILE
 
file 'features/support/paths.rb', <<-FILE
def path_to(page_name)
case page_name
when /the start page/i
root_path
when /the account page/i
account_path
else
raise "Can't find mapping from \"\#{page_name}\" to a path."
end
end
FILE
 
# migrations
rake "db:migrate"
 
# Commit all work so far to the repository
git :init
git :add => '.'
git :commit => "-a -m 'Initial commit'"
 
rake "test"