-
Notifications
You must be signed in to change notification settings - Fork 0
/
rails-app-template-no-reg.rb
299 lines (243 loc) · 7.53 KB
/
rails-app-template-no-reg.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
# >----------------------------[ Initial Setup ]------------------------------<
@repo = 'https://github.com/jdugarte/rails-app-template'
@repo_raw = 'https://raw.github.com/jdugarte/rails-app-template/master/files2/'
def copy_from_repo(filename)
source_filename = @repo_raw + filename
destination_filename = filename
remove_file(destination_filename) if File.exists?(destination_filename)
begin
get source_filename, destination_filename
rescue OpenURI::HTTPError
say "Unable to obtain #{filename} from the repo #{@repo}"
end
end
# >---------------------------------[ init ]----------------------------------<
# create repository
git :init
append_file '.gitignore' do
<<-FILE
/coverage
/public/system
/public/assets
FILE
end
# commit basic rails app
git :add => "."
git :commit => "-a -m 'initial rails application'"
# >---------------------------------[ gems ]----------------------------------<
# create rvmrc file
create_file ".rvmrc", "rvm gemset use #{app_name} --create"
# add gems
gsub_file 'Gemfile', /(gem 'sqlite3')/, '# \1'
gem 'jquery-ui-rails'
gem 'devise'
gem 'rails-i18n'
gem 'globalize3'
gem "paperclip", "~> 3.0"
gem 'http_accept_language'
gem 'turbolinks'
gem 'rvm-capistrano'
gem_group :development do
gem "debugger"
gem 'sqlite3'
end
gem_group :test do
gem 'simplecov', :require => false
gem 'sqlite3'
gem "mocha", :require => false
end
gem_group :production do
gem "mysql2"
end
# create gemset
run "rvm rvmrc trust #{app_name}"
run "rvm gemset use #{app_name} --create"
run 'bundle install --without production'
# commit gems
git :add => "."
git :commit => "-a -m 'add gems'"
# >---------------------------------[ devise ]-----------------------------------<
generate "devise:install"
# generate "devise User"
# >---------------------------------[ files from repo ]--------------------------<
copy_from_repo "files.txt"
File.readlines('files.txt').each do |file|
file.chomp!
copy_from_repo(file) unless file.empty?
end
remove_file "files.txt"
# >---------------------------------[ edit files ]-------------------------------<
# app/assets/javascripts/application.js
inject_into_file 'app/assets/javascripts/application.js', :after => "//= require jquery_ujs\n" do
<<-FILE
//= require jquery.ui.sortable
//= require turbolinks
FILE
end
append_file 'app/assets/javascripts/application.js' do
<<-FILE
var triggerEvent;
$(document).ready(function() {
return triggerEvent("page:change");
});
triggerEvent = function(name) {
var event;
event = document.createEvent('Events');
event.initEvent(name, true, true);
return document.dispatchEvent(event);
};
$(document).on('page:fetch', function(name) {
$(".loading").show();
});
$(document).on('page:change', function(name) {
$(".loading").hide();
});
filterTable = function(selector, query) {
query = $.trim(query); //trim white space
query = query.replace(/ /gi, '|'); //add OR for regex query
$(selector).each(function() {
($(this).text().search(new RegExp(query, "i")) < 0) ? $(this).hide() : $(this).show();
});
};
FILE
end
# app/assets/stylesheets/application.css
inject_into_file 'app/assets/stylesheets/application.css', :after => " *= require_self\n" do
<<-FILE
*= require jquery.ui.selectable
FILE
end
# config/application.rb
inject_into_file 'config/application.rb', :after => "# config.i18n.default_locale = :de\n" do
<<-FILE
config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '*.{rb,yml}').to_s]
config.i18n.default_locale = :en
FILE
end
# config/database.yml
gsub_file 'config/database.yml', /production:.*/m do
<<-FILE
production:
adapter: mysql2
encoding: utf8
username: username
password: password
host: localhost
port: 3306
database: #{app_name}
FILE
end
# config/environment.rb
append_file 'config/environment.rb' do
<<-FILE
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
if html_tag =~ /<label/
%|<div class="fieldWithErrors">\#{html_tag} <span class="error">\#{[instance.error_message].join(', ')}</span></div>|.html_safe
else
html_tag
end
end
FILE
end
# config/environments/development.rb
inject_into_file 'config/environments/development.rb', :after => "config.action_mailer.raise_delivery_errors = false\n" do
<<-FILE
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "localhost",
:port => 25
}
config.action_mailer.perform_deliveries = true
FILE
end
inject_into_file 'config/environments/development.rb', :before => "\nend" do
<<-FILE
# ImageMagick location, for Paperclip
Paperclip.options[:command_path] = "/usr/local/bin/"
# Avoid double remote calls
config.assets.debug = true
config.serve_static_assets = true
# Availales locales
config.i18n.available_locales = [:en, :es, :fr]
FILE
end
# config/environments/production.rb
gsub_file 'config/environments/production.rb', /# config\.assets\.precompile \+= %w\( search\.js \)/, 'config.assets.precompile += %w( common.css )'
inject_into_file 'config/environments/production.rb', :before => "\nend" do
<<-FILE
# action_mailer set up
config.action_mailer.raise_delivery_errors = false
config.action_mailer.default_url_options = { :host => '#{app_name}.sytes.net' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "localhost",
:port => 25
}
config.action_mailer.perform_deliveries = true
# ImageMagick location, for Paperclip
Paperclip.options[:command_path] = "/usr/local/bin/"
# Availales locales
config.i18n.available_locales = [:en, :es, :fr]
FILE
end
# config/environments/test.rb
inject_into_file 'config/environments/test.rb', :after => "config.action_mailer.delivery_method = :test\n" do
<<-FILE
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
FILE
end
inject_into_file 'config/environments/test.rb', :before => "\nend" do
<<-FILE
# Availales locales
config.i18n.available_locales = [:en, :es, :fr]
FILE
end
# config/initializers/devise.rb
gsub_file 'config/initializers/devise.rb', /# config\.authentication_keys = \[ :email \]/, 'config.authentication_keys = [ :username ]'
gsub_file 'config/initializers/devise.rb', /\[ :email \]/, '[ :username ]'
gsub_file 'config/initializers/devise.rb', /# config\.http_authenticatable_on_xhr = true/, 'config.http_authenticatable_on_xhr = false'
# test/test_helper.rb
append_file 'test/test_helper.rb' do
<<-FILE
require 'simplecov'
SimpleCov.start
class ActionController::TestCase
include Devise::TestHelpers
end
require "mocha/setup"
def assert_js_redirect_to_sign_in
assert_equal "text/javascript", response.content_type
assert_equal "window.location = '\#{new_user_session_path}'", response.body
end
FILE
end
# db/seeds.rb
append_file 'db/seeds.rb' do
<<-FILE
user = User.create :username => "admin", :name => 'Admin', :email => '[email protected]', :password => '123123',
:password_confirmation => '123123', :admin => true
user.confirm!
FILE
end
# APPNAME
%w{
config/routes.rb
app/views/layouts/application.html.erb
config/deploy.rb
config/locales/en.yml
config/locales/es.yml
config/locales/fr.yml
}.each do |file|
gsub_file file, /APPNAME/, app_name.capitalize
end
# >---------------------------------[ clean up ]-----------------------------<
remove_file 'public/index.html'
remove_file 'app/assets/images/rails.png'
# >---------------------------------[ finish ]-------------------------------<
# migrate
rake "db:migrate"
rake "db:seed"
# commit all changes
git :add => "."
git :commit => "-a -m 'add template changes'"