Skip to content

Instantly share code, notes, and snippets.

@zhangyuan
Last active November 9, 2020 06:44
Show Gist options
  • Save zhangyuan/8631257 to your computer and use it in GitHub Desktop.
Save zhangyuan/8631257 to your computer and use it in GitHub Desktop.
Rails Template
# encoding: utf-8
git :init
remove_file ".gitignore"
file '.gitignore', <<-IGNORE
*.rbc
*.sassc
.sass-cache
capybara-*.html
.rspec
/log
/tmp
/db/*.sqlite3
/public/system
/coverage/
/spec/tmp
**.orig
rerun.txt
pickle-email-*.html
config/secrets.yml
## Environment normalisation:
/.bundle
/vendor/bundle
# these should all be checked in to normalise the environment:
# Gemfile.lock, .ruby-version, .ruby-gemset
*.swo
*.swp
db/schema.rb
config/settings.local.yml
config/settings/*.local.yml
config/environments/*.local.yml
public/uploads/
config/database.yml
public/assets
config/unicorn.rb
config/nginx.conf
config/unicorn_init
/tags
IGNORE
remove_file "app/assets/images/rails.png"
gem 'slim-rails'
gem 'rails_config'
gem 'carrierwave'
gem 'mini_magick'
gem 'unicorn', require: false
gem 'kaminari-bootstrap', '~> 3.0.1'
gem 'capistrano', '~> 3.0.1', require: false
gem 'capistrano-bundler', require: false
gem_group :development, :test do
gem 'rspec-rails'
gem 'factory_girl_rails'
end
gem_group :assets do
gem 'bootstrap-sass', '~> 3.0.3.0'
gem 'compass-rails'
end
gem_group :development do
gem 'thin'
gem 'quiet_assets'
gem 'better_errors'
end
run "bundle"
run "cp config/database.yml config/database.yml.example"
generate("rails_config:install")
generate("rspec:install")
environment %q(config.autoload_paths += %W(#{config.root}/lib #{config.root}/app/workers/))
environment %q(config.time_zone = 'Beijing')
environment %q(config.active_record.default_timezone = :local)
environment %q(config.active_record.time_zone_aware_attributes = false)
environment %q(config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}').to_s])
environment %q(config.i18n.default_locale = 'zh-CN')
environment %q(config.action_controller.include_all_helpers = false)
remove_file ".rspec"
file ".rspec", <<-RSPEC
--color --format documentation
RSPEC
run "bundle exec cap install"
uncomment_lines "Capfile", /require.*capistrano\/bundler/
file 'config/nginx.conf.example', <<-NGINX
upstream #{app_name}_unicorn_server {
server unix:/tmp/#{app_name}.sock fail_timeout=0;
}
server {
listen 80;
server_name www.example.com;
client_max_body_size 5M;
root /home/deployer/apps/#{app_path}/current/public;
location ~ ^/(assets)/ {
gzip_static on; # to serve pre-gzipped version
expires max;
add_header Cache-Control public;
}
gzip on;
gzip_http_version 1.1;
gzip_vary on;
gzip_comp_level 6;
gzip_disable "MSIE [1-6].(?!.*SV1)";
location / {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_buffering on;
if (!-f $request_filename) {
proxy_pass http://#{app_name}_unicorn_server;
break;
}
}
access_log /home/deployer/apps/#{app_name}/shared/log/access.log;
error_log /home/deployer/apps/#{app_name}/shared/log/error.log;
}
NGINX
file "config/unicorn.rb.example", <<-UNICORN
# -*- encoding : utf-8 -*-
worker_processes 3
timeout 30
listen File.expand_path("/tmp/#{app_name}.sock"), :backlog => 64
preload_app true
pid File.expand_path('../../tmp/pids/unicorn.pid', __FILE__)
before_exec do |server|
ENV["BUNDLE_GEMFILE"] = File.expand_path('../../Gemfile', __FILE__)
end
before_fork do |server, worker|
defined?(ActiveRecord::Base) and
ActiveRecord::Base.connection.disconnect!
defined?(Redis) and
Redis.current.client.disconnect
old_pid = "\#{server.config[:pid]}.oldbin"
if File.exists?(old_pid) && server.pid != old_pid
begin
Process.kill("QUIT", File.read(old_pid).to_i)
rescue Errno::ENOENT, Errno::ESRCH
end
end
end
after_fork do |server, worker|
defined?(ActiveRecord::Base) and
ActiveRecord::Base.establish_connection
defined?(Redis) and
Redis.current.client.reconnect
end
UNICORN
file "config/unicorn_init.example", <<-INIT
#!/bin/sh
### BEGIN INIT INFO
# Provides: unicorn_#{app_name}
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Manage unicorn server
# Description: Start, stop, restart unicorn server for a specific application.
### END INIT INFO
set -e
# Feel free to change any of the following variables for your app:
TIMEOUT=${TIMEOUT-60}
APP_ROOT=/home/deployer/apps/#{app_name}/current
PID=$APP_ROOT/tmp/pids/unicorn.pid
CMD="cd $APP_ROOT; bundle exec unicorn -D -c $APP_ROOT/config/unicorn.rb -E production"
AS_USER=deployer
set -u
OLD_PIN="$PID.oldbin"
sig () {
test -s "$PID" && kill -$1 `cat $PID`
}
oldsig () {
test -s $OLD_PIN && kill -$1 `cat $OLD_PIN`
}
run () {
if [ "$(id -un)" = "$AS_USER" ]; then
eval $1
else
su -c "$1" - $AS_USER
fi
}
case "$1" in
start)
sig 0 && echo >&2 "Already running" && exit 0
run "$CMD"
;;
stop)
sig QUIT && exit 0
echo >&2 "Not running"
;;
force-stop)
sig TERM && exit 0
echo >&2 "Not running"
;;
restart|reload)
sig HUP && echo reloaded OK && exit 0
echo >&2 "Couldn't reload, starting '$CMD' instead"
run "$CMD"
;;
upgrade)
if sig USR2 && sleep 2 && sig 0 && oldsig QUIT
then
n=$TIMEOUT
while test -s $OLD_PIN && test $n -ge 0
do
printf '.' && sleep 1 && n=$(( $n - 1 ))
done
echo
if test $n -lt 0 && test -s $OLD_PIN
then
echo >&2 "$OLD_PIN still exists after $TIMEOUT seconds"
exit 1
fi
exit 0
fi
echo >&2 "Couldn't upgrade, starting '$CMD' instead"
run "$CMD"
;;
reopen-logs)
sig USR1
;;
*)
echo >&2 "Usage: $0 <start|stop|restart|upgrade|force-stop|reopen-logs>"
exit 1
;;
esac
INIT
remove_file "config/deploy.rb"
file "config/deploy.rb", <<-DEPLOY
set :application, '#{app_name}'
# set :repo_url, ''
# ask :branch, proc { `git rev-parse --abbrev-ref HEAD`.chomp }
set :user, 'deployer'
set :use_sudo, false
set :deploy_to, "/home/\#{fetch :user}/apps/\#{fetch :application}"
set :scm, :git
set :format, :pretty
set :log_level, :debug
set :pty, true
set :linked_files, %w{
config/settings.local.yml config/database.yml config/unicorn.rb
config/unicorn_init config/nginx.conf
}
set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system public/uploads}
set :default_env, { path: "$HOME/.rbenv/shims:$HOME/.rbenv/bin:$PATH" }
set :keep_releases, 5
remote_file "config/settings.local.yml" => "config/settings.local.yml.example"
remote_file "config/database.yml" => "config/database.yml.example"
remote_file "config/unicorn.rb" => "config/unicorn.rb.example"
remote_file "config/unicorn_init" => "config/unicorn_init.example"
remote_file "config/nginx.conf" => "config/nginx.conf.example"
namespace :deploy do
namespace :check do
task :linked_files => fetch(:linked_files)
end
desc "precompile asseets and sync to web server"
task :assets_sync do
system('bundle exec rake assets:precompile')
on roles(:web) do |server|
system "rsync -vr --exclude='.DS_Store' public/assets \#{fetch :user}@\#{server}:\#{release_path}/public/"
end
system('rm -rf public/assets')
end
desc "rake db:migrate"
task :db_migrate do
on roles(:db) do
execute "cd \#{release_path} && bundle exec rake db:migrate RAILS_ENV=production"
end
end
after 'deploy:updated', 'deploy:db_migrate'
after 'deploy:updated', 'deploy:assets_sync'
desc "Start application"
task :start do
on roles(:app) do
execute "cd \#{deploy_to}/current && bundle exec unicorn_rails -c config/unicorn.rb -E production -D"
end
end
desc 'Restart application'
task :restart do
on roles(:app), in: :sequence, wait: 5 do
execute "kill -s USR2 `cat \#{deploy_to}/current/tmp/pids/unicorn.pid`"
end
end
desc 'Stop application'
task :stop do
on roles(:app), in: :sequence, wait: 5 do
execute "kill -s QUIT `cat \#{deploy_to}/current/tmp/pids/unicorn.pid`"
end
end
after :finishing, 'deploy:cleanup'
end
DEPLOY
remove_file "public/index.html"
remove_file "app/views/layouts/application.html.erb"
file "app/views/layouts/application.html.slim", <<-LAYOUT
doctype html
html lang="zh_CN"
head
meta charset="utf-8"
meta http-equiv="X-UA-Compatible" content="IE=edge"
meta name="viewport" content="width=device-width, initial-scale=1.0"
= csrf_meta_tags
= stylesheet_link_tag "application"
= javascript_include_tag "application"
= yield :header
title #{app_name}
body
.navbar.navbar-inverse.navbar-fixed-top role="navigation"
.container
.navbar-header
button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"
span class="sr-only" Toggle navigation
span class="icon-bar"
span class="icon-bar"
span class="icon-bar"
.navbar-collapse.collapse
= link_to 'Home', '/', class: 'navbar-brand'
.row
- if flash[:notice]
.win-row
.alert.alert-info= flash[:notice]
- if flash[:alert]
.win-row
.alert.alert-danger= flash[:alert]
.col-md-3
= yield :left
.col-md-9
= yield
.row
LAYOUT
remove_file "app/assets/stylesheets/application.css"
file "app/assets/stylesheets/application.css.scss", <<-APPLICATION
@import "bootstrap";
@import "bootstrap/theme";
body {
position: relative;
padding-top: 50px;
}
APPLICATION
remove_file "app/assets/javascripts/application.js"
file "app/assets/javascripts/application.js", <<-APPLICATION
//= require jquery
//= require jquery_ujs
APPLICATION
file "config/locales/zh-CN.yml", <<-I18N
zh-CN:
date:
abbr_day_names:
- 日
- 一
- 二
- 三
- 四
- 五
- 六
abbr_month_names:
-
- 1月
- 2月
- 3月
- 4月
- 5月
- 6月
- 7月
- 8月
- 9月
- 10月
- 11月
- 12月
day_names:
- 星期日
- 星期一
- 星期二
- 星期三
- 星期四
- 星期五
- 星期六
formats:
default: ! '%Y-%m-%d'
long: ! '%Y年%b%d日'
short: ! '%b%d日'
month_names:
-
- 一月
- 二月
- 三月
- 四月
- 五月
- 六月
- 七月
- 八月
- 九月
- 十月
- 十一月
- 十二月
order:
- :year
- :month
- :day
datetime:
distance_in_words:
about_x_hours:
one: 大约一小时
other: 大约 %{count} 小时
about_x_months:
one: 大约一个月
other: 大约 %{count} 个月
about_x_years:
one: 大约一年
other: 大约 %{count} 年
almost_x_years:
one: 接近一年
other: 接近 %{count} 年
half_a_minute: 半分钟
less_than_x_minutes:
one: 不到一分钟
other: 不到 %{count} 分钟
less_than_x_seconds:
one: 不到一秒
other: 不到 %{count} 秒
over_x_years:
one: 一年多
other: ! '%{count} 年多'
x_days:
one: 一天
other: ! '%{count} 天'
x_minutes:
one: 一分钟
other: ! '%{count} 分钟'
x_months:
one: 一个月
other: ! '%{count} 个月'
x_seconds:
one: 一秒
other: ! '%{count} 秒'
prompts:
day: 日
hour: 时
minute: 分
month: 月
second: 秒
year: 年
errors: &errors
format: ! '%{attribute}%{message}'
messages:
accepted: 必须是可被接受的
blank: 不能为空字符
confirmation: 与确认值不匹配
empty: 不能留空
equal_to: 必须等于 %{count}
even: 必须为双数
exclusion: 是保留关键字
greater_than: 必须大于 %{count}
greater_than_or_equal_to: 必须大于或等于 %{count}
inclusion: 不包含于列表中
invalid: 是无效的
less_than: 必须小于 %{count}
less_than_or_equal_to: 必须小于或等于 %{count}
not_a_number: 不是数字
not_an_integer: 必须是整数
odd: 必须为单数
record_invalid: ! '验证失败: %{errors}'
taken: 已经被使用
too_long: 过长(最长为 %{count} 个字符)
too_short: 过短(最短为 %{count} 个字符)
wrong_length: 长度非法(必须为 %{count} 个字符)
template:
body: 如下字段出现错误:
header:
one: 有 1 个错误发生导致「%{model}」无法被保存。
other: 有 %{count} 个错误发生导致「%{model}」无法被保存。
helpers:
select:
prompt: 请选择
submit:
create: 新增%{model}
submit: 储存%{model}
update: 更新%{model}
number:
currency:
format:
delimiter: ! ','
format: ! '%u %n'
precision: 2
separator: .
significant: false
strip_insignificant_zeros: false
unit: CN¥
format:
delimiter: ! ','
precision: 3
separator: .
significant: false
strip_insignificant_zeros: false
human:
decimal_units:
format: ! '%n %u'
units:
billion: 十亿
million: 百万
quadrillion: 千兆
thousand: 千
trillion: 兆
unit: ''
format:
delimiter: ''
precision: 1
significant: false
strip_insignificant_zeros: false
storage_units:
format: ! '%n %u'
units:
byte:
one: Byte
other: Bytes
gb: GB
kb: KB
mb: MB
tb: TB
percentage:
format:
delimiter: ''
precision:
format:
delimiter: ''
support:
array:
last_word_connector: ! ', 和 '
two_words_connector: ! ' 和 '
words_connector: ! ', '
time:
am: 上午
formats:
default: ! '%Y年%b%d日 %A %H:%M:%S %Z'
long: ! '%Y年%b%d日 %H:%M'
short: ! '%b%d日 %H:%M'
pm: 下午
# remove these aliases after 'activemodel' and 'activerecord' namespaces are removed from Rails repository
activemodel:
errors:
<<: *errors
activerecord:
errors:
<<: *errors
I18N
git add: "."
git commit: %Q{ -m 'Initial commit' }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment