Skip to content

feat: use Rails 8.0 #601

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 17 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ jobs:
strategy:
# don't stop all variants if one of them fails (we usually want to know
# how many fail)
fail-fast: false
fail-fast: true
matrix:
js_package_manager:
- name: npm
Expand Down Expand Up @@ -203,7 +203,8 @@ jobs:
# "react", "sidekiq" etc.
APP_NAME: ${{ matrix.variant.name }}-demo
CONFIG_PATH: ${{ matrix.variant.config_path }}
SKIPS: '--skip-javascript --skip-docker ${{ matrix.variant.skips }}'
# prettier-ignore
SKIPS: '--skip-javascript --skip-docker --skip-kamal --skip-solid ${{ matrix.variant.skips }}'
PGUSER: postgres
PGPASSWORD: postgres
PGHOST: localhost
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,15 +193,15 @@ $ wget https://raw.githubusercontent.com/ackama/rails-template/main/ackama_rails

# Create a new app using the template. Template options will be taken from
# ./ackama_rails_template.config.yml
$ rails new my_app --no-rc --database=postgresql --skip-javascript --skip-docker --template=https://raw.githubusercontent.com/ackama/rails-template/main/template.rb
$ rails new my_app --no-rc --database=postgresql --skip-javascript --skip-docker --skip-kamal --skip-solid --template=https://raw.githubusercontent.com/ackama/rails-template/main/template.rb

# Example 2
# #########

# Create a custom config YAML file, saving as ./my_custom_config.yml

# Template options will be taken from ../my_custom_config.yml (relative to the new app directory)
$ CONFIG_PATH=../my_custom_config.yml rails new my_app --no-rc --database=postgresql --skip-javascript --skip-docker --template=https://raw.githubusercontent.com/ackama/rails-template/main/template.rb
$ CONFIG_PATH=../my_custom_config.yml rails new my_app --no-rc --database=postgresql --skip-javascript --skip-docker --skip-kamal --skip-solid --template=https://raw.githubusercontent.com/ackama/rails-template/main/template.rb
```

Here are some additional options you can add to this command. We don't
Expand Down Expand Up @@ -230,6 +230,8 @@ To make this the default Rails application template on your system, create a
-d postgresql
--skip-javascript
--skip-docker
--skip-kamal
--skip-solid
-m https://raw.githubusercontent.com/ackama/rails-template/main/template.rb
```

Expand Down
2 changes: 1 addition & 1 deletion ci/bin/build-and-test
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ config_path = if ENV.fetch("CONFIG_PATH", "") == ""
end

config_env_var = %Q(CONFIG_PATH="#{config_path}") if config_path
skip_flags = ENV.fetch("SKIPS", "--skip-javascript --skip-docker")
skip_flags = ENV.fetch("SKIPS", "--skip-javascript --skip-docker --skip-kamal --skip-solid")

puts "=" * 80
puts <<~EO_CONFIG_SUMMARY
Expand Down
4 changes: 2 additions & 2 deletions target_versions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# This is stored in a separate file so we can share it between the template and
# our CI configuration.
target_rails_major_minor: '7.2' # specify as major.minor
target_rails_major_minor: '8.0' # specify as major.minor

# Set this to the minimum version of Ruby that the chosen Rails version supports.
minimum_ruby_major_minor: '3.1'
minimum_ruby_major_minor: '3.2'
38 changes: 20 additions & 18 deletions template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -498,36 +498,38 @@ def create_initial_migration
run_with_clean_bundler_env "bin/rake db:migrate"
end

def gsub_file!(path, flag, ...)
# Ensures that the contents of the file at <code>path</code> is changed after
# executing the given block, raising an error if it has not changed
#
# @param [String] path
def ensure_file_changes!(path, &)
content = File.binread(path)

gsub_file(path, flag, ...)

raise StandardError, "the contents of #{path} did not change!" if content == File.binread(path)
end
yield

def append_to_file!(path, ...)
content = File.binread(path)
return unless content == File.binread(path)

append_to_file(path, ...)
puts "--- #{path} ---"
puts content
puts "--- #{"-" * path.length} ---"

raise StandardError, "the contents of #{path} did not change!" if content == File.binread(path)
raise StandardError, "the contents of #{path} did not change!"
end

def prepend_to_file!(path, ...)
content = File.binread(path)
def gsub_file!(path, flag, ...)
ensure_file_changes!(path) { gsub_file(path, flag, ...) }
end

prepend_to_file(path, ...)
def append_to_file!(path, ...)
ensure_file_changes!(path) { append_to_file(path, ...) }
end

raise StandardError, "the contents of #{path} did not change!" if content == File.binread(path)
def prepend_to_file!(path, ...)
ensure_file_changes!(path) { prepend_to_file(path, ...) }
end

def insert_into_file!(path, ...)
content = File.binread(path)

insert_into_file(path, ...)

raise StandardError, "the contents of #{path} did not change!" if content == File.binread(path)
ensure_file_changes!(path) { insert_into_file(path, ...) }
end

apply_template!
1 change: 1 addition & 0 deletions variants/backend-base/app/assets/config/manifest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// This file must exist for assets pipeline compatibility.
8 changes: 8 additions & 0 deletions variants/backend-base/app/template.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Configure app/assets
copy_file "variants/backend-base/app/assets/config/manifest.js", "app/assets/config/manifest.js"

# Configure app/controllers
copy_file "variants/backend-base/app/controllers/home_controller.rb", "app/controllers/home_controller.rb"
copy_file "variants/backend-base/app/controllers/active_storage/base_controller.rb", "app/controllers/active_storage/base_controller.rb"
Expand Down Expand Up @@ -29,3 +32,8 @@
# allow_browser versions: :modern
AFTER
)

remove_dir ".github/"
remove_dir "app/views/pwa"
remove_file "bin/dev"
remove_file "bin/thrust"
70 changes: 23 additions & 47 deletions variants/backend-base/config/environments/production.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
insert_into_file! "config/environments/production.rb",
after: /# config\.assets\.css_compressor = :sass\n/ do
<<-RUBY
# TODO: check if this option is still active, and if we want to keep ensuring it is disabled
# insert_into_file! "config/environments/production.rb",
# after: /# config\.assets\.css_compressor = :sass\n/ do
# <<-RUBY
#
# # Disable minification since it adds a *huge* amount of time to precompile.
# # Anyway, gzip alone gets us about 70% of the benefits of minify+gzip.
# config.assets.css_compressor = false
# RUBY
# end

# Disable minification since it adds a *huge* amount of time to precompile.
# Anyway, gzip alone gets us about 70% of the benefits of minify+gzip.
config.assets.css_compressor = false
RUBY
end
gsub_file! "config/environments/production.rb",
"config.assume_ssl = true",
<<~RUBY
#
# You should never use this because it just blindly sets headers without any actual
# checks; instead, whatever is handling the SSL-termination should be setting the
# appropriate headers to indicate that the request was actually SSL-terminated.
config.assume_ssl = false
RUBY

gsub_file! "config/environments/production.rb",
"config.force_ssl = true",
<<~RUBY
##
# `force_ssl` defaults to on. Set `force_ssl` to false if (and only if) RAILS_FORCE_SSL=false, otherwise set it to true.
#
# On by default, though can be disabled by setting RAILS_FORCE_SSL=false (and only "false")
config.force_ssl = ENV.fetch("RAILS_FORCE_SSL", "true").downcase != "false"
RUBY

# TODO: it looks like these options are now spread across the config
gsub_file! "config/environments/production.rb",
"# config.action_mailer.raise_delivery_errors = false",
<<-RUBY
Expand Down Expand Up @@ -44,43 +55,8 @@
'ENV.fetch("RAILS_LOG_LEVEL", ENV.fetch("LOG_LEVEL", "info"))'

gsub_file! "config/environments/production.rb",
"ActiveSupport::Logger.new(STDOUT)",
"ActiveSupport::Logger.new($stdout)"

insert_into_file! "config/environments/production.rb",
after: /.*config\.public_file_server\.enabled.*\n/ do
<<~'RUBY'

# Ensure that Rails sets appropriate caching headers on static assets if
# Rails is serving static assets in production e.g. on Heroku
#
# Overview of Cache-control values:
#
# max-age=<seconds>
# The maximum amount of time a resource is considered fresh.
#
# s-maxage=<seconds>
# Overrides max-age or the Expires header, but only for shared
# caches (e.g., proxies). Ignored by private caches.
#
# More info: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control
#
# Our Cache-Control header:
#
# * It tells all caches (both proxies like Cloudflare and the users web
# browser) that the asset can be cached.
# * It tells shared caches (e.g. Cloudflare) that they can cache it for 365 days
# * It tells browsers that they should cache for 365 days
#
# Cloudflare will respect s-maxage if it is set so change that value if you
# want Cloudflare to cache differently than then browser.
#
config.public_file_server.headers = {
"Cache-Control" => "public, s-maxage=#{365.days.seconds}, max-age=#{365.days.seconds}"
}

RUBY
end
"ActiveSupport::TaggedLogging.logger(STDOUT)",
"ActiveSupport::TaggedLogging.logger($stdout)"

insert_into_file! "config/environments/production.rb",
after: /.*config.cache_store = :mem_cache_store\n/ do
Expand Down
2 changes: 1 addition & 1 deletion variants/backend-base/config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
end

gsub_file! "config/environments/test.rb",
'config.action_mailer.default_url_options = { host: "www.example.com" }',
'config.action_mailer.default_url_options = { host: "example.com" }',
'config.action_mailer.default_url_options = { host: "localhost:3000" }'

gsub_file! "config/environments/test.rb",
Expand Down
29 changes: 29 additions & 0 deletions variants/backend-base/config/routes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
Rails.application.routes.draw do
# Workaround a "bug" in lighthouse CLI
#
# Lighthouse CLI (versions 5.4 - 5.6 tested) issues a `GET /asset-manifest.json`
# request during its run - the URL seems to be hard-coded. This file does not
# exist so, during tests, your test will fail because rails will die with a 404.
#
# Lighthouse run from Chrome Dev-tools does not have the same behaviour.
#
# This hack works around this. This behaviour might be fixed by the time you
# read this. You can check by commenting out this block and running the
# accessibility and performance tests. You are encouraged to remove this hack
# as soon as it is no longer needed.
if defined?(Shakapacker) && Rails.env.test?
# manifest paths depend on your shakapacker config so we inspect it
manifest_path = Shakapacker::Configuration
.new(root_path: Rails.root, config_path: Rails.root.join("config/shakapacker.yml"), env: Rails.env)
.public_manifest_path
.relative_path_from(Rails.public_path)
.to_s
get "/asset-manifest.json", to: redirect(manifest_path)
end

root "home#index"

mount OkComputer::Engine, at: "/healthchecks"

# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
end
32 changes: 1 addition & 31 deletions variants/backend-base/config/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,37 +25,7 @@
apply "variants/backend-base/config/environments/test.rb"
template "variants/backend-base/config/environments/staging.rb.tt", "config/environments/staging.rb"

route <<~HEALTH_CHECK_ROUTES
mount OkComputer::Engine, at: "/healthchecks"
HEALTH_CHECK_ROUTES

route <<-EO_ROUTES
##
# Workaround a "bug" in lighthouse CLI
#
# Lighthouse CLI (versions 5.4 - 5.6 tested) issues a `GET /asset-manifest.json`
# request during its run - the URL seems to be hard-coded. This file does not
# exist so, during tests, your test will fail because rails will die with a 404.
#
# Lighthouse run from Chrome Dev-tools does not have the same behaviour.
#
# This hack works around this. This behaviour might be fixed by the time you
# read this. You can check by commenting out this block and running the
# accessibility and performance tests. You are encouraged to remove this hack
# as soon as it is no longer needed.
#
if defined?(Shakapacker) && Rails.env.test?
# manifest paths depend on your shakapacker config so we inspect it
manifest_path = Shakapacker::Configuration
.new(root_path: Rails.root, config_path: Rails.root.join("config/shakapacker.yml"), env: Rails.env)
.public_manifest_path
.relative_path_from(Rails.public_path)
.to_s
get "/asset-manifest.json", to: redirect(manifest_path)
end

root "home#index"
EO_ROUTES
copy_file "variants/backend-base/config/routes.rb", "config/routes.rb", force: true

if File.exist? "config/storage.yml"
gsub_file! "config/storage.yml", /# service: S3/ do
Expand Down
Loading