Created
July 20, 2013 18:07
-
-
Save geoffgarside/6045926 to your computer and use it in GitHub Desktop.
Rails 4 Ember Application Template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Rails 4 Ember Application Template | |
# vim: set sts=2 sw=2 et: | |
# 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 to sample | |
inside "config" do | |
run "cp database.yml database.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/secret.key | |
# Sphinx Ignores | |
/db/sphinx | |
/log/searchd.*.pid | |
/config/sphinx.yml | |
/config/*.sphinx.conf | |
# Generated files | |
/ERD.dot | |
/ERD.pdf | |
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" | |
gem "ember-rails" | |
EOF | |
gem_group :developer do | |
gem "capistrano" | |
gem "puma" | |
end | |
gem_group :development do | |
gem "rails-erd" | |
gem "pry-rails" | |
gem "better_errors", platforms: [:ruby_19, :ruby_20, :rbx] | |
gem "binding_of_caller", platforms: [:ruby_19, :ruby_20, :rbx] | |
gem "meta_request" | |
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, "" | |
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks | |
gem 'turbolinks' | |
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder | |
gem 'jbuilder', '~> 1.2' | |
EOF | |
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 | |
# Replace config/initializers/secret_token.rb | |
gsub_file "config/initializers/secret_token.rb", | |
/(#{app_const}\.config\.secret_key_base = (?:.*))/, '# \1' | |
append_to_file "config/initializers/secret_token.rb" do | |
" | |
# Keep the secret key in config/secret.key | |
secret_file = File.expand_path('config/secret.key', Rails.root) | |
unless File.exists?(secret_file) | |
puts 'Missing config/secret.key file. Please run:' | |
puts ' $ rake secret > config/secret.key' | |
exit 1 | |
end | |
#{app_const}.config.secret_key_base = File.read(secret_file).chomp | |
" | |
end | |
run "rake secret > config/secret.key" | |
# 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 }" | |
# This needs to be done outside the bootstrap generator | |
# to avoid an initial "unknown variant" error. | |
environment <<-RUBY.strip_heredoc, env: :development | |
config.ember.variant = :development | |
RUBY | |
environment <<-RUBY.strip_heredoc, env: :test | |
config.ember.variant = :development | |
RUBY | |
environment <<-RUBY.strip_heredoc, env: :production | |
config.ember.variant = :production | |
RUBY | |
gsub_file "app/assets/javascripts/application.js", "//= require turbolinks", "" | |
# Configure the app to serve Ember.js and app assets from an AssetsController | |
generate "ember:bootstrap" | |
generate "ember:install" | |
generate :controller, "Assets", "index" | |
run "rm app/views/assets/index.html.erb" | |
file 'app/views/assets/index.html.erb', <<-CODE | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>#{@app_name.titleize}</title> | |
<%= stylesheet_link_tag "application", media: "all" %> | |
<%= csrf_meta_tags %> | |
</head> | |
<body> | |
<%= javascript_include_tag "application" %> | |
</body> | |
</html> | |
CODE | |
remove_file 'app/assets/javascripts/templates/application.handlebars' | |
file 'app/assets/javascripts/templates/application.handlebars', <<-CODE | |
<div style="width: 600px; border: 6px solid #eee; margin: 0 auto; padding: 20px; text-align: center; font-family: sans-serif;"> | |
<img src="http://emberjs.com/images/about/ember-productivity-sm.png" style="display: block; margin: 0 auto;"> | |
<h1>Welcome to Ember.js!</h1> | |
<p>You're running an Ember.js app on top of Ruby on Rails. To get started, replace this content | |
(inside <code>app/assets/javascripts/templates/application.handlebars</code>) with your application's | |
HTML.</p> | |
</div> | |
CODE | |
run "rm -rf app/views/layouts" | |
route "root to: 'assets#index'" | |
# Generate a default serializer that is compatible with ember-data | |
generate :serializer, "application", "--parent", "ActiveModel::Serializer" | |
inject_into_class "app/serializers/application_serializer.rb", 'ApplicationSerializer' do | |
" embed :ids, include: true\n" | |
end | |
git :init | |
git add: "." | |
git commit: "-m 'Initial commit of Rails skeleton'" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment