Created
November 24, 2024 00:24
-
-
Save anithri/ddb2469efdf37746d190a10dd0e65b0e to your computer and use it in GitHub Desktop.
Rails 8 application template
This file contains hidden or 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
require 'securerandom' | |
def gen_password | |
SecureRandom.random_number(36 ** 12).to_s(36).rjust(16, "0") | |
end | |
PG_PASSWORD = ENV.fetch('POSTGRES_PASSWORD') { gen_password } | |
PG_PORT = ENV.fetch('POSTGRES_PORT') { 15000 + SecureRandom.random_number(1000) } | |
APP_NAME = Rails.application.name | |
gem_group :development, :test do | |
gem 'annotate' | |
gem 'rspec-rails' | |
gem 'factory_bot_rails' | |
gem 'faker' | |
gem 'rubocop-rspec', require: false | |
gem 'shoulda-matchers', '~> 6.3' | |
end | |
gem_group :development do | |
gem 'letter_opener' | |
end | |
gem 'pundit', '~> 2.3' | |
file "./.envrc", <<-CODE | |
export PROJECT_DIR=$(pwd) | |
dotenv ./.env | |
PATH_add "${PROJECT_DIR}/bin" | |
CODE | |
run "chmod 700 ./.envrc" | |
file "./.env", <<-CODE | |
POSTGRES_COMPOSE=${PROJECT_DIR}/db.docker-compose.yml | |
POSTGRES_PASSWORD=#{PG_PASSWORD} | |
POSTGRES_USER=${USER} | |
POSTGRES_VOLUME=${PROJECT_DIR}/tmp/db/data | |
POSTGRES_PORT=#{PG_PORT} | |
POSTGRES_VERSION=14 | |
POSTGRES_HOST=127.0.0.1 | |
# SECRET_KEY_BASE=your_secret_key | |
CODE | |
run "chmod 700 ./.env" | |
run "direnv allow" | |
run "echo './.env' >> .gitignore" | |
file "./db.docker-compose.yml", <<-CODE | |
--- | |
services: | |
ajax_pg_14: | |
image: postgres:${POSTGRES_VERSION} | |
environment: | |
POSTGRES_USER: ${POSTGRES_USER:-$USER} | |
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD} | |
volumes: | |
- ${POSTGRES_VOLUME}:/var/lib/postgresql/data | |
ports: | |
- ${POSTGRES_PORT}:5432 | |
CODE | |
file "bin/db", <<-CODE | |
#!/bin/bash -e | |
fail_unless_env() { | |
if [[ -z ${!1} ]];then | |
echo "ERROR: $1 not present in ENV" | |
exit 1 | |
fi | |
} | |
check_env() { | |
fail_unless_env POSTGRES_COMPOSE | |
fail_unless_env POSTGRES_PASSWORD | |
} | |
usage () { | |
echo "usage: `basename $0` start|stop|status|down" | |
} | |
do_start () { | |
docker compose -f $POSTGRES_COMPOSE up -d | |
} | |
do_stop () { | |
docker compose -f $POSTGRES_COMPOSE stop | |
} | |
do_down () { | |
docker compose -f $POSTGRES_COMPOSE down | |
} | |
do_status () { | |
docker compose -f $POSTGRES_COMPOSE ps | |
} | |
check_env | |
case $1 in | |
start) | |
do_start | |
;; | |
stop) | |
do_stop | |
;; | |
status) | |
do_status | |
;; | |
down) | |
do_down | |
;; | |
*) | |
usage | |
;; | |
esac | |
CODE | |
run "chmod +x bin/db" | |
file "config/database.yml", <<-CODE | |
default: &default | |
adapter: postgresql | |
encoding: unicode | |
# For details on connection pooling, see Rails configuration guide | |
# https://guides.rubyonrails.org/configuring.html#database-pooling | |
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> | |
env_credentials: &env_credentials | |
username: <%= ENV['POSTGRES_USER'] %> | |
password: <%= ENV['POSTGRES_PASSWORD'] %> | |
host: <%= ENV.fetch('POSTGRES_HOST') %> | |
port: <%= ENV.fetch('POSTGRES_PORT') {5432} %> | |
development: | |
<<: *default | |
<<: *env_credentials | |
database: #{APP_NAME}_development | |
test: | |
<<: *default | |
<<: *env_credentials | |
database: #{APP_NAME}_test | |
production: | |
primary: &primary_production | |
<<: *default | |
database: #{APP_NAME}_production | |
username: authority | |
password: <%= ENV["AUTHORITY_DATABASE_PASSWORD"] %> | |
cache: | |
<<: *primary_production | |
database: #{APP_NAME}_production_cache | |
migrations_paths: db/cache_migrate | |
queue: | |
<<: *primary_production | |
database: #{APP_NAME}_production_queue | |
migrations_paths: db/queue_migrate | |
cable: | |
<<: *primary_production | |
database: #{APP_NAME}_production_cable | |
migrations_paths: db/cable_migrate | |
CODE | |
environment 'config.action_mailer.delivery_method = :letter_opener | |
config.action_mailer.perform_deliveries = true', env: 'development' | |
after_bundle do | |
generate "annotate:install" | |
generate "rspec:install" | |
generate "pundit:install" | |
git add: './.envrc' | |
git add: './db.docker-compose.yml' | |
git add: './bin/db' | |
git add: '.rspec' | |
git add: 'app/policies/' | |
git add: 'lib/tasks/auto_annotate_models.rake' | |
git add: 'spec/' | |
git add: '.gitignore' | |
git add: 'Gemfile' | |
git add: 'Gemfile.lock' | |
git add: 'config/database.yml' | |
git add: 'config/environments/development.rb' | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment