Skip to content

Commit

Permalink
Enhance base Rails template with better docs, etc
Browse files Browse the repository at this point in the history
- Add improved bin/setup script
- Specify 2-space indent and other general editor settings
- Generate documentation
- Create a Procfile
- Set up default rake task
- DRY up Gemfile and .ruby-version file
- Improve title and meta information for HTML layout
- Disable legacy javascript and stylesheet generators
- Allow force_ssl to be controlled via env var
- Create initial schema.rb
- Set default time zone: America/Los_Angeles
  • Loading branch information
yatish27 committed Apr 19, 2024
1 parent ade5518 commit 2494f5d
Show file tree
Hide file tree
Showing 13 changed files with 200 additions and 42 deletions.
14 changes: 14 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# https://editorconfig.org

root = true

[*]
charset = utf-8
indent_style = space
indent_size = 2
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[Makefile]
indent_style = tab
6 changes: 6 additions & 0 deletions .prettierrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/** @type {import("prettier").Config} */

module.exports = {
tabWidth: 2,
useTabs: false,
};
11 changes: 11 additions & 0 deletions DEPLOYMENT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Deployment

## Environment variables

These environment variables affect how the app functions when deployed in production.

- `RAILS_DISABLE_SSL` - Disable HSTS and secure cookies
- `RAILS_ENV` **REQUIRED** - "production"
- `RAILS_MAX_THREADS` - Number of threads per Puma process (default: 5)
- `SECRET_KEY_BASE` **REQUIRED** - Unique, secret key used to encrypt and sign cookies and other sensitive data
- `WEB_CONCURRENCY` - Number of Puma processes (default: number of CPUs)
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
source "https://rubygems.org"

ruby "3.3.0"
ruby file: ".ruby-version"

# Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main"
gem "rails", "~> 7.1.3", ">= 7.1.3.2"
Expand Down
2 changes: 2 additions & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
web: bundle exec puma -C config/puma.rb
release: bundle exec rake db:migrate
50 changes: 37 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,48 @@
# README
# shore

This README would normally document whatever steps are necessary to get the
application up and running.
This is a Rails 7.1 app.

Things you may want to cover:
## Prerequisites

* Ruby version
This project requires:

* System dependencies
- Ruby (see [.ruby-version](./.ruby-version)), preferably managed using [rbenv](https://github.com/rbenv/rbenv)
- PostgreSQL must be installed and accepting connections

* Configuration
On macOS, these [Homebrew](http://brew.sh) packages are recommended:

* Database creation
```
brew install rbenv
brew install postgresql@16
```

* Database initialization
## Getting started

* How to run the test suite
### bin/setup

* Services (job queues, cache servers, search engines, etc.)
Run this script to install necessary dependencies and prepare the Rails app to be started for the first time.

* Deployment instructions
```
bin/setup
```

* ...
> [!TIP]
> The `bin/setup` script is idempotent and is designed to be run often. You should run it every time you pull code that introduces new dependencies or makes other significant changes to the project.
### Run the app!

Start the Rails server with this command:

```
bin/rails s
```

The app will be located at <http://localhost:3000/>.

## Development

Use this command to run the full suite of automated tests:

```
bin/rake
```
7 changes: 7 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,10 @@
require_relative "config/application"

Rails.application.load_tasks

Rake::Task[:default].prerequisites.clear if Rake::Task.task_defined?(:default)

desc "Run all checks"
task default: %w[test:all] do
Thor::Base.shell.new.say_status :OK, "All checks passed!"
end
5 changes: 3 additions & 2 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<!DOCTYPE html>
<html>
<html lang="en">
<head>
<title>Shore</title>
<title><%= content_for?(:title) ? strip_tags(yield(:title)) : "Shore" %></title>
<meta name="apple-mobile-web-app-title" content="Shore">
<meta name="viewport" content="width=device-width,initial-scale=1">
<%= csrf_meta_tags %>
<%= csp_meta_tag %>
Expand Down
119 changes: 95 additions & 24 deletions bin/setup
Original file line number Diff line number Diff line change
@@ -1,36 +1,107 @@
#!/usr/bin/env ruby
require "fileutils"

# path to your application root.
APP_ROOT = File.expand_path("..", __dir__)
# This script is a way to set up or update your development environment automatically.
# This script is idempotent, so that you can run it at any time and get an expectable outcome.
# Add necessary setup steps to this method.
def setup!
env ".env", from: ".env.sample"
run "bundle install" if bundle_needed?
run "overcommit --install" if overcommit_installable?
run "bin/rails db:prepare" if database_present?
run "yarn install" if yarn_needed?
run "bin/rails tmp:create" if tmp_missing?
run "bin/rails restart"

def system!(*args)
system(*args, exception: true)
if git_safe_needed?
say_status :notice,
"Remember to run #{colorize("mkdir -p .git/safe", :yellow)} to trust the binstubs in this project",
:magenta
end

say_status :Ready!,
"Use #{colorize("bin/rails s", :yellow)} to start the app, " \
"or #{colorize("bin/rake", :yellow)} to run tests"
end

def run(command, echo: true, silent: false, exception: true)
say_status(:run, command, :blue) if echo
with_original_bundler_env do
options = silent ? {out: File::NULL, err: File::NULL} : {}
system(command, exception: exception, **options)
end
end

def run?(command)
run command, silent: true, echo: false, exception: false
end

def bundle_needed?
!run("bundle check", silent: true, exception: false)
end

FileUtils.chdir APP_ROOT do
# This script is a way to set up or update your development environment automatically.
# This script is idempotent, so that you can run it at any time and get an expectable outcome.
# Add necessary setup steps to this file.
def overcommit_installable?
File.exist?(".overcommit.yml") && !File.exist?(".git/hooks/overcommit-hook") && run?("overcommit -v")
end

puts "== Installing dependencies =="
system! "gem install bundler --conservative"
system("bundle check") || system!("bundle install")
def database_present?
File.exist?("config/database.yml")
end

# Install JavaScript dependencies
system("yarn check --check-files") || system!("yarn install")
def yarn_needed?
File.exist?("yarn.lock") && !run("yarn check --check-files", silent: true, exception: false)
end

# puts "\n== Copying sample files =="
# unless File.exist?("config/database.yml")
# FileUtils.cp "config/database.yml.sample", "config/database.yml"
# end
def tmp_missing?
!Dir.exist?("tmp/pids")
end

puts "\n== Preparing database =="
system! "bin/rails db:prepare"
def git_safe_needed?
ENV["PATH"].include?(".git/safe/../../bin") && !Dir.exist?(".git/safe")
end

def with_original_bundler_env(&block)
return yield unless defined?(Bundler)

puts "\n== Removing old logs and tempfiles =="
system! "bin/rails log:clear tmp:clear"
Bundler.with_original_env(&block)
end

def env(env_file, from:)
return unless File.exist?(from)

unless File.exist?(env_file)
say_status(:copy, "#{from}#{env_file}", :magenta)
require "fileutils"
FileUtils.cp(from, env_file)
end

keys = ->(f) { File.readlines(f).filter_map { |l| l[/^([^#\s][^=\s]*)/, 1] } }

missing = keys[from] - keys[env_file]
return if missing.empty?

say_status(:WARNING, "Your #{env_file} file is missing #{missing.join(", ")}. Refer to #{from} for details.", :red)
end

def say_status(label, message, color = :green)
label = label.to_s.rjust(12)
puts [colorize(label, color), message.gsub(/^/, " " * 13).strip].join(" ")
end

def colorize(str, color)
return str unless color_supported?

code = {red: 31, green: 32, yellow: 33, blue: 34, magenta: 35}.fetch(color)
"\e[0;#{code};49m#{str}\e[0m"
end

def color_supported?
if ENV["TERM"] == "dumb" || !ENV["NO_COLOR"].to_s.empty?
false
else
[$stdout, $stderr].all? { |io| io.respond_to?(:tty?) && io.tty? }
end
end

puts "\n== Restarting application server =="
system! "bin/rails restart"
Dir.chdir(File.expand_path("..", __dir__)) do
setup!
end
2 changes: 1 addition & 1 deletion config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Application < Rails::Application
# These settings can be overridden in specific environments using the files
# in config/environments, which are processed later.
#
# config.time_zone = "Central Time (US & Canada)"
config.time_zone = "America/Los_Angeles"
# config.eager_load_paths << Rails.root.join("extras")

# Don't generate system test files.
Expand Down
2 changes: 1 addition & 1 deletion config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
# config.assume_ssl = true

# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
config.force_ssl = true
config.force_ssl = ENV["RAILS_DISABLE_SSL"].blank?

# Log to STDOUT by default
config.logger = ActiveSupport::Logger.new(STDOUT)
Expand Down
5 changes: 5 additions & 0 deletions config/initializers/generators.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Rails.application.config.generators do |g|
# Disable generators we don't need.
g.javascripts false
g.stylesheets false
end
17 changes: 17 additions & 0 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2494f5d

Please sign in to comment.