Skip to content

Instantly share code, notes, and snippets.

@joemsak

joemsak/rails.rb

Last active Apr 20, 2021
Embed
What would you like to do?
Dockerized Rails 6.1 Template
gem 'sidekiq'
gem 'redis'
gem 'email_validator', require: 'email_validator/strict'
gem 'bcrypt'
gem 'friendly_id'
gem "tailwindcss-rails"
gem_group :development, :test do
gem 'pry-rails'
gem 'dotenv-rails'
end
gem_group :development do
gem 'pessimize'
end
gem_group :test do
gem 'rexml'
gem 'capybara'
gem 'selenium-webdriver'
gem 'webdrivers'
gem 'factory_bot_rails'
gem 'rspec-rails'
gem 'rails-controller-testing'
end
file "docker-compose.yml", <<~CODE
version: "3.9"
x-app: &app
build:
context: .
environment: &env
NODE_ENV: development
RAILS_ENV: ${RAILS_ENV:-development}
PGPASSWORD: password
image: schedule_board-dev:0.0.a
tmpfs:
- /tmp
x-backend: &backend
<<: *app
stdin_open: true
tty: true
build: .
volumes:
- ./:/app:cached
- ./log:/root/log:cached
- ./.psqlrc:/root/.psqlrc:ro
- bundle:/usr/local/bundle
- node_modules:/app/node_modules
- packs:/app/public/packs
- packs-test:/app/public/packs-test
- rails_cache:/app/tmp/cache
- ssh-data:/ssh:ro
environment:
<<: *env
BOOTSNAP_CACHE_DIR: /usr/local/bundle/_bootsnap
EDITOR: vim
PSQL_HISTFILE: /root/log/.psql_history
REDIS_HOST: redis
REDIS_URL: redis://redis:6379/
SSH_AUTH_SOCK: /ssh/auth/sock
WEBPACK_DEV_SERVER_MANIFEST_HOST: webpack
WEBPACK_DEV_SERVER_HOST: webpack
depends_on:
- db
- redis
services:
web:
<<: *backend
volumes:
- .:/app
- bundle:/usr/local/bundle
- node_modules:/app/node_modules
- rails_cache:/app/tmp/cache
- ssh-data:/ssh:ro
ports:
- "3000:3000"
depends_on:
- db
sidekiq:
<<: *backend
command: bundle exec sidekiq -C config/sidekiq.yml
webpack:
<<: *app
command: ./bin/webpack-dev-server --inline true
environment:
WEBPACKER_DEV_SERVER_HOST: 0.0.0.0
WEBPACKER_DEV_SERVER_PUBLIC: localhost:3035
ports:
- 3035:3035
volumes:
- .:/app
- bundle:/usr/local/bundle
- node_modules:/app/node_modules
- packs:/app/public/packs
- packs-test:/app/public/packs-test
db:
image: postgres
volumes:
- postgres:/var/lib/postgresql/data
- postgres_history:/var/log/psql_history
ports:
- 5432
environment:
<<: *env
POSTGRES_PASSWORD: password
POSTGRES_HOST_AUTH_METHOD: "trust"
redis:
image: redis:latest
volumes:
- redis:/data
ports:
- 6379
volumes:
postgres:
postgres_history:
bundle:
redis:
rails_cache:
node_modules:
packs:
packs-test:
ssh-data:
external:
name: ssh_data
CODE
file "Dockerfile", <<~CODE
FROM ruby:3.0.1
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -; \
echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list; \
curl -sL https://deb.nodesource.com/setup_14.x | bash -;
RUN apt-get update -qq && apt-get install -y \
vim \
yarn \
nodejs \
postgresql-client \
chromium;
WORKDIR /app
COPY Gemfile /app/Gemfile
COPY Gemfile.lock /app/Gemfile.lock
RUN gem install bundler -v 2.2.16
RUN bundle install
COPY . /app
RUN chmod -R 755 $RAILS_ROOT/bin
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000
CMD ["rails", "server", "-b", "0.0.0.0"]
CODE
file "dip.yml", <<~CODE
version: '4.1'
environment:
COMPOSE_EXT: development
compose:
files:
- docker-compose.yml
project_name: schedule_board
interaction:
bash:
description: Open the Bash shell in app's container
service: web
command: bash
compose:
run_options: [no-deps]
bundle:
description: Run Bundler commands
service: web
command: bundle
yarn:
description: Run Yarn commands
service: web
command: yarn
npx:
description: Run npx commands
service: web
command: npx
rspec:
description: Run Rspec commands
service: web
environment:
RAILS_ENV: test
command: bundle exec rspec
rails:
description: Run Rails commands
service: web
command: bundle exec rails
subcommands:
s:
command: bundle exec puma -C config/puma.rb
description: Run Rails server at http://localhost:3000
service: web
compose:
run_options: [service-ports, use-aliases]
psql:
description: Run Postgres psql console
service: db
default_args: app_development
command: psql -h db -U postgres
provision:
- dip compose down --volumes
- dip compose up -d db
- dip bash -c ./bin/setup
CODE
file "entrypoint.sh", <<~CODE
#!/bin/bash
set -e
FILE=/app/tmp/pids/server.pid
if test -f "$FILE"; then
rm -f $FILE
fi
exec "$@"
CODE
file "config/database.yml", <<~CODE
default: &default
adapter: postgresql
encoding: unicode
host: <%= ENV["DATABASE_HOST"] || 'db' %>
username: postgres
password: <%= ENV["PGPASSWORD"] %>
pool: 5
development:
<<: *default
database: app_development
test:
<<: *default
database: app_test
production:
url: <%= ENV['DATABASE_URL'] %>
CODE
file "config/sidekiq.yml", <<~CODE
---
:concurrency: 5
:pidfile: tmp/pids/sidekiq.pid
:queues:
- default
CODE
file "config/webpacker.yml", <<~CODE
# Note: You must restart bin/webpack-dev-server for changes to take effect
default: &default
source_path: app/javascript
source_entry_path: packs
public_root_path: public
public_output_path: packs
cache_path: tmp/cache/webpacker
webpack_compile_output: true
# Additional paths webpack should lookup modules
# ['app/assets', 'engine/foo/app/assets']
additional_paths: []
# Reload manifest.json on all requests so we reload latest compiled packs
cache_manifest: false
# Extract and emit a css file
extract_css: false
static_assets_extensions:
- .jpg
- .jpeg
- .png
- .gif
- .tiff
- .ico
- .svg
- .eot
- .otf
- .ttf
- .woff
- .woff2
extensions:
- .jsx
- .mjs
- .js
- .sass
- .scss
- .css
- .module.sass
- .module.scss
- .module.css
- .png
- .svg
- .gif
- .jpeg
- .jpg
development:
<<: *default
compile: true
# Reference: https://webpack.js.org/configuration/dev-server/
dev_server:
https: false
host: webpack
port: 3035
public: webpack:3035
hmr: false
# Inline should be set to true if using HMR
inline: true
overlay: true
compress: true
disable_host_check: true
use_local_ip: false
quiet: false
pretty: false
headers:
'Access-Control-Allow-Origin': '*'
watch_options:
ignored: '**/node_modules/**'
test:
<<: *default
compile: true
# Compile test packs to a separate directory
public_output_path: packs-test
production:
<<: *default
# Production depends on precompilation of packs prior to booting for performance.
compile: false
# Extract and emit a css file
extract_css: true
# Cache manifest.json for performance
cache_manifest: true
CODE
file ".github/workflows/rspec.yml", <<~CODE
env:
RUBY_VERSION: 3.0
POSTGRES_USER: postgres
PGPASSWORD: postgres
POSTGRES_DB: postgres
DATABASE_HOST: localhost
name: RSpec
on: [push,pull_request]
jobs:
rspec-test:
name: RSpec
runs-on: ubuntu-18.04
services:
postgres:
image: postgres:latest
ports:
- 5432:5432
env:
POSTGRES_USER: ${{ env.POSTGRES_USER }}
POSTGRES_PASSWORD: ${{ env.PGPASSWORD }}
steps:
- uses: actions/checkout@v1
- uses: actions/setup-ruby@v1
with:
ruby-version: ${{ env.RUBY_VERSION }}
- name: Install postgres client
run: sudo apt-get install libpq-dev
- name: Install dependencies
run: |
gem install bundler
bundle install
yarn install
- name: Create database
run: |
bundle exec rails db:create RAILS_ENV=test
bundle exec rails db:migrate RAILS_ENV=test
- name: Compile assets, webpack
run: |
bundle exec rails assets:precompile RAILS_ENV=test
bundle exec rails webpacker:compile RAILS_ENV=test
- name: Run tests
run: bundle exec rspec
CODE
file "spec/support/factory_bot_helpers.rb", <<~CODE
RSpec.configure do |config|
config.include FactoryBot::Syntax::Methods
end
CODE
file "spec/support/time_helpers.rb", <<~CODE
RSpec.configure do |config|
config.include ActiveSupport::Testing::TimeHelpers
end
CODE
file "spec/support/authentication_helpers.rb", <<~CODE
module AuthenticationHelpers
def sign_in(scope)
@signed_in_scope ||= create(scope)
allow_any_instance_of(ApplicationController).to receive(:current_user)
.and_return(@signed_in_scope)
end
def sign_out
@signed_in_scope = nil
allow_any_instance_of(ApplicationController).to receive(:current_user)
.and_return(nil)
end
end
RSpec.configure do |config|
config.include AuthenticationHelpers
end
CODE
file "spec/rails_helper.rb", <<~CODE
require 'spec_helper'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../config/environment', __dir__)
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'rspec/rails'
Dir[Rails.root.join('spec', 'support', '**', '*.rb')].sort.each { |f| require f }
begin
ActiveRecord::Migration.maintain_test_schema!
rescue ActiveRecord::PendingMigrationError => e
puts e.to_s.strip
exit 1
end
RSpec.configure do |config|
config.fixture_path = "#{::Rails.root}/spec/factories"
config.use_transactional_fixtures = true
config.infer_spec_type_from_file_location!
config.filter_rails_from_backtrace!
# config.filter_gems_from_backtrace("gem name")
end
CODE
initializer "email_validator.rb", <<~CODE
if defined?(EmailValidator)
EmailValidator.default_options[:mode] = :strict
end
CODE
environment <<~CODE
config.active_job.queue_adapter = :sidekiq
config.i18n.load_path += Dir[
Rails.root.join('config', 'locales', '**', '*.{rb,yml}')
]
config.generators do |g|
g.system_tests = nil
g.test_framework :rspec
g.helper_specs false
g.controller_specs false
g.view_specs false
g.routing_specs false
g.fixture_replacement :factory_bot, dir: "spec/factories"
g.stylesheets false
g.helper false
g.orm :active_record, primary_key_type: :uuid
end
CODE
after_bundle do
run "pessimize -c patch"
generate "rspec:install"
generate "friendly_id"
rails_command "tailwindcss:install"
run "dip provision"
git :init
git add: "."
git commit: %Q{ -m 'Initial commit' }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment