Skip to content

Instantly share code, notes, and snippets.

@geoffgarside
Last active June 26, 2022 21:27
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save geoffgarside/6045922 to your computer and use it in GitHub Desktop.
Save geoffgarside/6045922 to your computer and use it in GitHub Desktop.
Rails 4 Application Template
# Rails 4 Application Template
# Remove normal files we don't want
remove_file "README.rdoc"
remove_file "public/index.html"
remove_file "app/assets/images/rails.png"
# Copy database.yml and secrets.yml to sample
inside "config" do
run "cp database.yml database.yml.sample"
run "cp secrets.yml secrets.yml.sample"
end
# Write our config/database.yml
if options[:database].to_s == "postgresql"
remove_file "config/database.yml"
create_file "config/database.yml", <<-EOF
# PostgreSQL. Versions 7.4 and 8.x are supported.
#
# Install the pg driver:
# gem install pg
# On Mac OS X with macports:
# gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config
# On Windows:
# gem install pg
# Choose the win32 build.
# Install PostgreSQL and put its /bin directory on your path.
#
# Configure Using Gemfile
# gem 'pg'
#
login: &login
adapter: postgresql
encoding: unicode
username: #{ENV['USER']}
timeout: 5000
pool: 5
development:
<<: *login
database: #{app_name}_development
min_messages: warning
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *login
database: #{app_name}_test
min_messages: warning
production:
<<: *login
database: #{app_name}_production
EOF
end
# Configure our .gitignore file
insert_into_file ".gitignore", "/vendor/bundle\n", after: "/.bundle\n"
insert_into_file ".gitignore", "/db/*.sql\n/db/*.xml\n", after: "/db/*.sqlite3\n"
insert_into_file ".gitignore", "/log/*.log.*\n", after: "/log/*.log\n"
insert_into_file ".gitignore", "/doc/*\n/.sass-cache\n", after: "/tmp\n"
append_to_file ".gitignore", <<-EOF
# Local Ignores
/config/database.yml
/config/secrets.yml
# Sphinx Ignores
/db/sphinx
/log/searchd.*.pid
/config/sphinx.yml
/config/*.sphinx.conf
EOF
# Configure our Gemfile
prepend_to_file "Gemfile", "ENV['RB_USER_INSTALL'] = '1'\n"
append_to_file "Gemfile", <<-EOF
gem "dalli"
gem "rack-cors", require: "rack/cors"
gem "secure_headers"
gem "active_model_serializers"
EOF
gem_group :developer do
gem "capistrano"
gem "puma"
end
gem_group :development do
gem "rails-erd"
gem "pry-rails"
gem "pry-theme"
end
gem_group :test, :development do
gem "factory_girl_rails"
end
gem_group :test do
gem "shoulda", ">= 3.0.1"
gem "factory_girl"
end
# Clean the Gemfile
gsub_file "Gemfile", <<-EOF, ""
# Use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'
# Use unicorn as the app server
# gem 'unicorn'
# Use Capistrano for deployment
# gem 'capistrano', group: :development
# Use debugger
# gem 'debugger', group: [:development, :test]
EOF
gsub_file "Gemfile", /\"/, "'"
run "bundle install --path vendor/bundle"
# Configure CORS in config/application.rb
insert_into_file "config/application.rb", after: " # config.i18n.default_locale = :de\n" do
"
config.middleware.use Rack::Cors do
allow do
origins '*'
resource '/*', headers: :any, methods: [:get, :post]
end
end
"
end
# Configure SecureHeaders
create_file "config/initializers/secure_headers.rb" do
"::SecureHeaders::Configuration.configure do |config|
config.hsts = { max_age: 99, include_subdomains: true}
config.x_frame_options = 'DENY'
config.x_content_type_options = 'nosniff'
config.x_xss_protection = { value: 1, mode: false}
config.csp = {
enforce: Rails.env.production?,
default_src: 'inline eval self',
report_uri: '/uri-directive',
img_src: 'https://ribbon.m247.com data: self',
style_src: 'https://ribbon.m247.com inline self',
script_src: 'https://ribbon.m247.com inline eval self',
connect_src: 'https://ribbon.m247.com self',
frame_src: 'none'
}
end
"
end
insert_into_file "app/controllers/application_controller.rb", after: " protect_from_forgery with: :exception\n" do
" ensure_security_headers\n"
end
# Comment the cookie_store and activate the mem_cache_store
gsub_file "config/initializers/session_store.rb",
/(#{app_const}\.config\.session_store :cookie_store, key: '(?:.*)_session')/, '# \1'
append_to_file "config/initializers/session_store.rb" do
"
#{app_const}.config.session_store :mem_cache_store,
key: '_#{app_name.underscore}_session', namespace: '#{app_name.underscore.gsub('_',':')}:session',
expire_after: 2.days
"
end
# Enable log rotation in all environments
%w(development test).each do |f|
insert_into_file "config/environments/#{f}.rb",
" config.logger = Logger.new(Rails.root.join(\"log\", Rails.env + \".log\"), 3, 5242880)\n",
after: " # Settings specified here will take precedence over those in config/application.rb.\n"
end
# Configure Production environment
gsub_file "config/environments/production.rb",
/# (config\.action_dispatch\.x_sendfile_header = 'X-Accel-Redirect' # for nginx)/, '\1'
gsub_file "config/environments/production.rb",
/# (config.log_tags = \[ :subdomain, :uuid \])/, '\1'
insert_into_file "config/environments/production.rb", after: "# config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new)\n" do
"
# Log to environment log file, keep 7, max size 100mb
config.logger = ActiveSupport::TaggedLogging.new(Logger.new(Rails.root.join(\"log\", Rails.env + \".log\"), 7, 104857600))
"
end
gsub_file "config/environments/production.rb", /# (config\.cache_store = :mem_cache_store)/,
"\\1, { namespace: '#{app_name.underscore.gsub('_',':')}', expires_in: 86400, compress: true }"
git :init
git add: "."
git commit: "-m 'Initial commit of Rails skeleton'"
@Paxa
Copy link

Paxa commented Aug 15, 2014

I think 100.megabytes looks better then 104857600

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment