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
- Configure system tests
- 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/Chicago
  • Loading branch information
jarodtaylor committed Sep 15, 2024
1 parent 296b5ef commit 4093101
Show file tree
Hide file tree
Showing 16 changed files with 228 additions and 47 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: 3)
- `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)
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@ end

group :test do
# Use system testing [https://guides.rubyonrails.org/testing.html#system-testing]
gem "capybara"
gem "selenium-webdriver"
gem "capybara", require: false
gem "selenium-webdriver", require: false
end
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
# ror_vite_tailwind

This README would normally document whatever steps are necessary to get the
application up and running.
This is a Rails 7.2 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/dev
```

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
puts ">>>>>> [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><%= content_for(:title) || "Ror Vite Tailwind" %></title>
<title><%= content_for?(:title) ? strip_tags(yield(:title)) : "Ror Vite Tailwind" %></title>
<meta name="apple-mobile-web-app-title" content="Ror Vite Tailwind">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="apple-mobile-web-app-capable" content="yes">
<%= csrf_meta_tags %>
Expand Down
122 changes: 95 additions & 27 deletions bin/setup
Original file line number Diff line number Diff line change
@@ -1,40 +1,108 @@
#!/usr/bin/env ruby
require "fileutils"
# frozen_string_literal: true

APP_ROOT = File.expand_path("..", __dir__)
APP_NAME = "ror-vite-tailwind"
# 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 --check-files" 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/dev", :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:, **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

def overcommit_installable?
File.exist?(".overcommit.yml") && !File.exist?(".git/hooks/overcommit-hook") && run?("overcommit -v")
end

def database_present?
File.exist?("config/database.yml")
end

def yarn_needed?
File.exist?("yarn.lock")
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 tmp_missing?
!Dir.exist?("tmp/pids")
end

def git_safe_needed?
ENV["PATH"].include?(".git/safe/../../bin") && !Dir.exist?(".git/safe")
end

puts "== Installing dependencies =="
system! "gem install bundler --conservative"
system("bundle check") || system!("bundle install")
def with_original_bundler_env(&)
return yield unless defined?(Bundler)

# Install JavaScript dependencies
system("yarn install --check-files")
Bundler.with_original_env(&)
end

# puts "\n== Copying sample files =="
# unless File.exist?("config/database.yml")
# FileUtils.cp "config/database.yml.sample", "config/database.yml"
# end
def env(env_file, from:)
return unless File.exist?(from)

puts "\n== Preparing database =="
system! "bin/rails db:prepare"
unless File.exist?(env_file)
say_status(:copy, "#{from}#{env_file}", :magenta)
require "fileutils"
FileUtils.cp(from, env_file)
end

puts "\n== Removing old logs and tempfiles =="
system! "bin/rails log:clear tmp:clear"
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

puts "\n== Restarting application server =="
system! "bin/rails restart"
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== Configuring puma-dev =="
# system "ln -nfs #{APP_ROOT} ~/.puma-dev/#{APP_NAME}"
# system "curl -Is https://#{APP_NAME}.test/up | head -n 1"
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/Chicago"
# config.eager_load_paths << Rails.root.join("extras")
end
end
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?

# Skip http-to-https redirect for the default health check endpoint.
# config.ssl_options = { redirect: { exclude: ->(request) { request.path == "/up" } } }
Expand Down
7 changes: 7 additions & 0 deletions config/initializers/generators.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

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.

10 changes: 9 additions & 1 deletion test/application_system_test_case.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# frozen_string_literal: true

require "test_helper"

class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
driven_by :selenium, using: :headless_chrome, screen_size: [ 1400, 1400 ]
driven_by :selenium,
using: (ENV["SHOW_BROWSER"] ? :chrome : :headless_chrome),
screen_size: [1400, 1400] do |options|
# Allows running in Docker
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--no-sandbox")
end
end
14 changes: 14 additions & 0 deletions test/support/capybara.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

ActiveSupport.on_load(:action_dispatch_system_test_case) do
require "capybara"
require "capybara/rails"

Capybara.configure do |config|
config.default_max_wait_time = 2
config.save_path = "tmp/screenshots"
config.enable_aria_label = true
config.server = :puma, {Silent: true}
config.test_id = "data-testid"
end
end
2 changes: 2 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ class TestCase
# Add more helper methods to be used by all tests here...
end
end

Dir[File.expand_path("support/**/*.rb", __dir__)].sort.each { |rb| require(rb) }

0 comments on commit 4093101

Please sign in to comment.