tcocca (owner)

Revisions

gist: 201506 Download_button fork
public
Description:
My standard rails app template
Public Clone URL: git://gist.github.com/201506.git
Embed All Files: show embed
rails_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
# Tom's standard rails template
 
# Application Gems
gem "formtastic",:source => 'http://gemcutter.org', :version => '>= 0.9.4'
gem "clearance", :source => 'http://gemcutter.org', :version => '0.8.3'
gem 'will_paginate', :source => 'http://gemcutter.org', :version => '~> 2.3.11'
gem "acts-as-taggable-on", :source => "http://gemcutter.org", :version => '>= 1.0.10'
gem "paperclip", :source => 'http://gemcutter.org', :version => '>= 2.3.1.1'
gem "friendly_id", :source => 'http://gemcutter.org', :version => '>= 2.2.4'
gem "inherited_resources", :source => 'http://gemcutter.org', :version => '>= 0.9.3'
gem "thinking-sphinx", :lib => 'thinking_sphinx/0.9.8', :source => 'http://gemcutter.org', :version => '>= 1.3.6'
gem "whenever", :lib => false, :source => 'http://gemcutter.org', :version => '>= 0.4.0'
gem "acts_as_audited", :lib => false, :source => "http://gemcutter.org", :version => '>= 1.1.0'
gem "delayed_job", :lib => "delayed_job", :source => "http://gemcutter.org", :version => '>= 1.8.4'
 
# Development Gems
gem "rails-footnotes", :source => "http://gemcutter.org", :version => '>= 3.6.3', :env => "development"
 
# Test Gems
gem "shoulda", :lib => "shoulda", :source => "http://gemcutter.org", :version => '>= 2.10.2', :env => "test"
gem "factory_girl", :lib => "factory_girl", :source => "http://gemcutter.org", :version => '>= 1.2.3', :env => "test"
gem "cucumber", :source => "http://gemcutter.org", :version => "0.4.4", :env => "test"
gem "webrat", :source => "http://gemcutter.org", :version => "0.5.3", :env => "test"
gem "jnunemaker-matchy", :lib => "matchy", :source => "http://gems.github.com", :env => "test"
 
# Standard Plugins
plugin 'exception_notifier', :git => 'git://github.com/rails/exception_notification.git'
plugin 'asset_packager', :git => 'git://github.com/sbecker/asset_packager.git'
plugin 'jrails', :git => 'git://github.com/aaronchi/jrails.git'
plugin 'limerick_rake', :git => 'git://github.com/thoughtbot/limerick_rake.git'
plugin 'acts_as_list', :git => 'git://github.com/rails/acts_as_list.git'
 
# Install and unpack gems
rake("gems:install", :sudo => true)
rake("gems:unpack:dependencies")
rake("gems:install RAILS_ENV=development", :sudo => true)
rake("gems:unpack:dependencies RAILS_ENV=development")
rake("gems:install RAILS_ENV=test", :sudo => true)
rake("gems:unpack:dependencies RAILS_ENV=test")
 
# Freeze Rails?
rake("rails:freeze:gems") if yes?("Freeze rails gems ?")
 
# Gem and Plugin configuration and installation
rake("jrails:js:scrub")
rake("asset:packager:create_yml")
 
generate("clearance")
generate("clearance_views")
generate("cucumber")
generate("clearance_features")
generate("formtastic")
generate("acts_as_taggable_on_migration")
generate("audited_migration add_audits_table")
generate("delayed_job")
 
run "wheneverize ."
 
# Set the Time Zone
environment( "config.time_zone = 'Eastern Time (US & Canada)'" )
 
# Create DB example for SVN
inside('config') do
  run "cp database.yml database.yml.example"
end
 
# Create the staging environment
inside('config/environments') do
  run 'cp production.rb staging.rb'
end
 
# Capistrano
capify!
 
file "config/deploy.rb", <<-DEPLOY
require 'capistrano/ext/multistage'
 
set :stages, %w(staging production) # [optional] defaults to
set :default_stage, "staging"
 
set :application, ""
set :scm_user, "deploy"
set :repository, Proc.new { "" }
 
# : : : : : : : : : : : : : : : : : : : : : : : : : : :
 
after "deploy:finalize_update", "deploy:cleanup"
 
desc "Tasks to execute after code update"
task :after_update_code, :roles => [:app] do
# build the asset packages
#run "cd \#{current_path} && rake asset:packager:build_all"
# relink database configuration
run "cp \#{release_path}/config/database.yml.example \#{release_path}/config/database.yml"
 
# clear the frags directories
run "rm -rf \#{shared_path}/system/frags"
run "mkdir \#{shared_path}/system/frags"
run "ln -nfs \#{shared_path}/system/frags \#{release_path}/public/system/frags"
end
 
# : : : : : : : Delayed Job
 
after "deploy:start", "delayed_job:start"
after "deploy:stop", "delayed_job:stop"
after "deploy:restart", "delayed_job:restart"
 
namespace :deploy do
desc "Restarting mod_rails with restart.txt"
task :restart, :roles => :app, :except => { :no_release => true } do
run "touch \#{current_path}/tmp/restart.txt"
end
 
[:start, :stop].each do |t|
desc "\#{t} task is a no-op with mod_rails"
task t, :roles => :app do ; end
end
end
DEPLOY
 
file "config/deploy/staging.rb", <<-DEPLOY
puts "-- -- staging -- --"
role :web, "ip"
role :app, "ip"
role :db, "ip", :primary => true
 
set :deploy_to, "/home/deploy/rails/application"
set :user, "deploy"
 
set :stage, "staging"
ENV['RAILS_ENV'] = stage
set :env, stage
set :environment, env
set :rails_env, stage
 
desc "Set the Staging robots.txt file and Re-generate sitemap after deployment"
task :after_deploy, :roles => [:app, :db, :web] do
run "rm \#{release_path}/public/robots.txt"
run "cp \#{release_path}/public/robots_staging.txt \#{release_path}/public/robots.txt"
end
DEPLOY
 
file "config/deploy/production.rb", <<-DEPLOY
puts "-- -- production -- --"
role :web, "ip"
role :app, "ip"
role :db, "ip", :primary => true
 
set :deploy_to, "/home/deploy/rails/application"
set :user, "deploy"
 
set :stage, "production"
set :rails_env, stage
DEPLOY
 
# Remove the rails files
run "rm public/index.html"
run 'rm -f public/images/rails.png'
 
# Caching
initializer 'caching.rb', <<-CODE
ActionController::Base.cache_store = :file_store, "\#{RAILS_ROOT}/public/system/frags"
CODE
 
# Fix the way rails generates fields with errors
initializer 'field_errors.rb', <<-CODE
ActionView::Base.field_error_proc = Proc.new{ |html_tag, instance| "<span class=\"fieldWithErrors\">\#{html_tag}</span>" }
CODE
 
# Ability to override emails in non-production environments
initializer 'staged_email.rb', <<-CODE
# Forces all outgoing mail from dev/staging environments to go to developers
module ActionMailer
class Base
protected
def staged_email(email_address)
ENV['RAILS_ENV'] == 'production' ? email_address : SYSTEM_TO_EMAIL
end
end
end
CODE
 
# Mail initializer setup
smtp_address = ask("What is your smtp address ?")
smtp_port = ask("What is your smtp port ?")
smtp_domain = ask("What is your smtp domain ?")
smtp_username = ask("What is your smtp username ?")
smtp_password = ask("What is your smtp password ?")
smtp_authentication = ask("What is your smtp authentication (plain / login) ?")
smtp_tls = ask("What enable tls (true / false) ?")
 
en_recipients = ask("Who are the exception recipients ?")
en_sender = ask("Who is the exception sender ?")
en_prefix = ask("What is the exception prefix ?")
 
initializer 'mail.rb', <<-CODE
ActionMailer::Base.smtp_settings = {
:address => "#{smtp_address}",
:port => #{smtp_port},
:domain => "#{smtp_domain}",
:user_name => "#{smtp_username}",
:password => "#{smtp_password}",
:authentication => :#{smtp_authentication},
:enable_starttls_auto => #{smtp_tls == "true" ? true : false}
}
 
ExceptionNotifier.exception_recipients = %w{#{en_recipients}}
ExceptionNotifier.sender_address = %W{#{en_sender}}
ExceptionNotifier.email_prefix = "[#{en_prefix}\#{"_\#{RAILS_ENV}" if RAILS_ENV != 'production'}] "
 
DO_NOT_REPLY = "donotreply@example.com"
CODE
 
# Clearance current_user footnote setup
file 'lib/footnotes/notes/current_user_note.rb', <<-CODE
module Footnotes
module Notes
class CurrentUserNote < AbstractNote
# This method always receives a controller
#
def initialize(controller)
@current_user = controller.instance_variable_get("@current_user")
end
 
# The name that will appear as legend in fieldsets
#
def legend
"Current user: \#{@current_user.email}"
end
 
# This Note is only valid if we actually found an user
# If it's not valid, it won't be displayed
#
def valid?
@current_user
end
 
# The fieldset content
#
def content
escape(@current_user.inspect)
end
end
end
end
CODE
 
# Default application layout
file 'app/views/layouts/application.html.erb', <<-LAYOUT
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title></title>
<meta name="Description" content="" />
<meta name="Keywords" content="" />
<meta name="verify-v1" content="" />
<%= javascript_include_merged :base %>
<%= stylesheet_link_merged :base %>
<%#= stylesheet_link_merged :print, :media => "print" %>
<%= yield :head_content %>
<%= render :partial => "shared/google_analytics" %>
</head>
 
<body>
<%= yield %>
<%= yield :last_content %>
</body>
</html>
LAYOUT
 
# Google Analytics
file 'app/views/shared/_google_analytics.html.erb', <<-ANALYTICS
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("<%= RAILS_ENV == "production" ? 'production_code' : 'other_code' %>");
pageTracker._trackPageview();
} catch(err) {}</script>
ANALYTICS
 
# Staging Robots.txt
file 'public/robots_staging.txt', <<-ROBOTS
User-agent: *
Disallow: /
 
User-agent: Googlebot
Disallow: /
ROBOTS
 
# Factory Girl Uploads helper
file 'test/factory_attachment.rb', <<-UPLOADS
require 'action_controller/test_process'
Factory.class_eval do
def attachment(name, path, content_type = nil)
path_with_rails_root = "\#{RAILS_ROOT}/\#{path}"
uploaded_file = if content_type
ActionController::TestUploadedFile.new(path_with_rails_root, content_type)
else
ActionController::TestUploadedFile.new(path_with_rails_root)
end
add_attribute name, uploaded_file
end
end
UPLOADS
 
# Environment specific settings
dev_email = ask("What is the development email address ?")
 
append_file 'config/environments/development.rb', <<-DEVELOPMENT
\n
HOST = 'localhost'
 
SYSTEM_TO_EMAIL = "#{dev_email}"
SYSTEM_DOMAIN = "http://localhost:3000"
DEVELOPMENT
 
append_file 'config/environments/test.rb', <<-TEST
\n
HOST = 'localhost'
 
SYSTEM_TO_EMAIL = "#{dev_email}"
SYSTEM_DOMAIN = "http://localhost:3000"
TEST
 
append_file 'config/environments/staging.rb', <<-STAGING
\n
HOST = 'localhost'
 
SYSTEM_TO_EMAIL = "#{dev_email}"
SYSTEM_DOMAIN = "http://localhost:3000"
STAGING
 
append_file 'config/environments/production.rb', <<-PRODUCTION
\n
HOST = 'localhost'
 
SYSTEM_TO_EMAIL = "#{dev_email}"
SYSTEM_DOMAIN = "http://localhost:3000"
PRODUCTION
 
append_file 'Rakefile', <<-RAKEFILE
\n
require 'thinking_sphinx/0.9.8'
require 'thinking_sphinx/tasks'
\n
begin
require 'delayed/tasks'
rescue LoadError
STDERR.puts "Run `rake gems:install` to install delayed_job"
end
RAKEFILE