Skip to content

Commit

Permalink
Rails61part2 (#7807)
Browse files Browse the repository at this point in the history
rails 6.1 defaults and stuff
  • Loading branch information
robguthrie committed Aug 31, 2021
1 parent 51e04be commit 5ed6beb
Show file tree
Hide file tree
Showing 23 changed files with 254 additions and 603 deletions.
1 change: 1 addition & 0 deletions Gemfile
Expand Up @@ -72,6 +72,7 @@ gem 'pghero'
gem 'pg_query', '>= 0.9.0'

group :development, :test do
gem 'listen'
gem 'byebug'
gem 'factory_bot_rails'
gem 'faker'
Expand Down
7 changes: 7 additions & 0 deletions Gemfile.lock
Expand Up @@ -375,6 +375,9 @@ GEM
marcel (~> 1.0.1)
mime-types
terrapin (~> 0.6.0)
listen (3.7.0)
rb-fsevent (~> 0.10, >= 0.10.3)
rb-inotify (~> 0.9, >= 0.9.10)
lograge (0.11.2)
actionpack (>= 4)
activesupport (>= 4)
Expand Down Expand Up @@ -480,6 +483,9 @@ GEM
activerecord (>= 5.2.4)
activesupport (>= 5.2.4)
i18n
rb-fsevent (0.11.0)
rb-inotify (0.10.1)
ffi (~> 1.0)
redcarpet (3.5.1)
redis (4.4.0)
redis-actionpack (5.2.0)
Expand Down Expand Up @@ -667,6 +673,7 @@ DEPENDENCIES
icalendar
image_processing (~> 1.12)
kt-paperclip
listen
lograge
maxminddb
mini_magick
Expand Down
4 changes: 2 additions & 2 deletions app/models/concerns/has_rich_text.rb
Expand Up @@ -2,7 +2,7 @@ module HasRichText
PREVIEW_OPTIONS = {
resize_to_limit: [1280,1280],
saver: {
quality: 80,
quality: 85,
strip: true
}
}
Expand Down Expand Up @@ -65,7 +65,7 @@ def build_attachments
i = file.blob.slice(:id, :filename, :content_type, :byte_size)
i.merge!({ preview_url: Rails.application.routes.url_helpers.rails_representation_path(file.representation(PREVIEW_OPTIONS), only_path: true) }) if file.representable?
i.merge!({ download_url: Rails.application.routes.url_helpers.rails_blob_path(file, only_path: true) })
i.merge!({ icon: attachment_icon(file.blob.content_type || file.blob.filename) })
i.merge!({ icon: attachment_icon(file.content_type || file.filename) })
i.merge!({ signed_id: file.signed_id })
i
end
Expand Down
40 changes: 40 additions & 0 deletions app/services/migrate_events_service.rb
Expand Up @@ -35,4 +35,44 @@ def self.migrate_paperclip
end
end
end

def self.rewrite_inline_images(host = nil)
ActiveStorage::Attachment.where(name: 'image_files').includes(:record).order('id desc').each do |attachment|
record = attachment.record
column_name = names[attachment.record_type]
next unless record[column_name].present?

host ||= Regexp.escape ENV['CANONICAL_HOST']
regex = /https:\/\/#{host}\/rails\/active_storage\/representations\/.*#{Regexp.escape attachment.filename.to_s}/

if record[column_name].match?(regex)
path = Rails.application.routes.url_helpers.rails_representation_path(
attachment.representation(HasRichText::PREVIEW_OPTIONS),
only_path: true
)
puts "updating #{attachment.record_type} #{attachment.record_id}"
record.update_columns(column_name => record[column_name].gsub(regex, path))
end
end
end

def self.rewrite_attachment_links
names.each_pair do |type, col|
type.constantize.where('attachments != ?', '[]').with_attached_files.each do |record|
record.update_columns(attachments: record.build_attachments)
end
end
end

def self.names
names = {
'Discussion' => 'description',
'Comment' => 'body',
'Poll' => 'details',
'Outcome' => 'statement',
'Stance' => 'reason',
'User' => 'short_bio',
'Group' => 'description',
}
end
end
8 changes: 4 additions & 4 deletions config.ru
@@ -1,6 +1,6 @@
# This file is used by Rack-based servers to start the application.
require ::File.expand_path('../config/environment', __FILE__)

# Redirect to non-www address (https://github.com/tylerhunt/rack-canonical-host)
#use Rack::CanonicalHost, ENV['CANONICAL_HOST'] if ENV['CANONICAL_HOST']
run Loomio::Application
require_relative "config/environment"

run Rails.application
Rails.application.load_server
6 changes: 3 additions & 3 deletions config/application.rb
@@ -1,6 +1,6 @@
require File.expand_path('../boot', __FILE__)
require_relative "boot"

require 'rails/all'
require "rails/all"

Bundler.require(*Rails.groups)

Expand All @@ -19,7 +19,7 @@ def lmo_asset_host

module Loomio
class Application < Rails::Application
# config.load_defaults 6.0
config.load_defaults 5.0
# config.autoloader = :classic
config.middleware.use Rack::Attack
# config.active_job.queue_adapter = :sidekiq
Expand Down
4 changes: 2 additions & 2 deletions config/boot.rb
@@ -1,4 +1,4 @@
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)

require 'bundler/setup' # Set up gems listed in the Gemfile.
require 'bootsnap/setup' # Speed up boot time by caching expensive operations.
require "bundler/setup" # Set up gems listed in the Gemfile.
require "bootsnap/setup" # Speed up boot time by caching expensive operations.
8 changes: 4 additions & 4 deletions config/environment.rb
@@ -1,5 +1,5 @@
# Load the rails application
require File.expand_path('../application', __FILE__)
# Load the Rails application.
require_relative "application"

# Initialize the rails application
Loomio::Application.initialize!
# Initialize the Rails application.
Rails.application.initialize!
15 changes: 12 additions & 3 deletions config/environments/development.rb
@@ -1,6 +1,10 @@
Loomio::Application.configure do
# In the development environment your application's code is reloaded on
# every request. This slows down response time but is perfect for development
require "active_support/core_ext/integer/time"

Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb.

# In the development environment your application's code is reloaded any time
# it changes. This slows down response time but is perfect for development
# since you don't have to restart the web server when you make code changes.
config.log_level = ENV.fetch('RAILS_LOG_LEVEL', :debug)
config.cache_classes = false
Expand All @@ -21,6 +25,8 @@

# Do not compress assets
config.assets.compress = false
# Highlight code that triggered database queries in logs.
config.active_record.verbose_query_logs = true

# Expands the lines which load the assets
config.assets.debug = true
Expand All @@ -32,6 +38,9 @@
config.sass.line_comments = false

config.eager_load = false
# Use an evented file watcher to asynchronously detect changes in source code,
# routes, locales, etc. This feature depends on the listen gem.
config.file_watcher = ActiveSupport::EventedFileUpdateChecker

config.action_controller.action_on_unpermitted_parameters = :raise
# config.after_initialize do
Expand Down
7 changes: 4 additions & 3 deletions config/initializers/backtrace_silencers.rb
@@ -1,7 +1,8 @@
# Be sure to restart your server when you modify this file.

# You can add backtrace silencers for libraries that you're using but don't wish to see in your backtraces.
# Rails.backtrace_cleaner.add_silencer { |line| line =~ /my_noisy_library/ }
# Rails.backtrace_cleaner.add_silencer { |line| /my_noisy_library/.match?(line) }

# You can also remove all the silencers if you're trying to debug a problem that might stem from framework code.
# Rails.backtrace_cleaner.remove_silencers!
# You can also remove all the silencers if you're trying to debug a problem that might stem from framework code
# by setting BACKTRACE=1 before calling your invocation, like "BACKTRACE=1 ./bin/rails runner 'MyClass.perform'".
Rails.backtrace_cleaner.remove_silencers! if ENV["BACKTRACE"]
5 changes: 5 additions & 0 deletions config/initializers/content_security_policy.rb
Expand Up @@ -11,6 +11,8 @@
# policy.object_src :none
# policy.script_src :self, :https
# policy.style_src :self, :https
# # If you are using webpack-dev-server then specify webpack-dev-server host
# policy.connect_src :self, :https, "http://localhost:3035", "ws://localhost:3035" if Rails.env.development?

# # Specify URI for violation reports
# # policy.report_uri "/csp-violation-report-endpoint"
Expand All @@ -19,6 +21,9 @@
# If you are using UJS then enable automatic nonce generation
# Rails.application.config.content_security_policy_nonce_generator = -> request { SecureRandom.base64(16) }

# Set the nonce only to specific directives
# Rails.application.config.content_security_policy_nonce_directives = %w(script-src)

# Report CSP violations to a specified URI
# For further information see the following documentation:
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy-Report-Only
Expand Down
6 changes: 6 additions & 0 deletions config/initializers/filter_parameter_logging.rb
@@ -0,0 +1,6 @@
# Be sure to restart your server when you modify this file.

# Configure sensitive parameters which will be filtered from the log file.
Rails.application.config.filter_parameters += [
:passw, :secret, :token, :_key, :crypt, :salt, :certificate, :otp, :ssn
]
67 changes: 67 additions & 0 deletions config/initializers/new_framework_defaults_6_1.rb
@@ -0,0 +1,67 @@
# Be sure to restart your server when you modify this file.
#
# This file contains migration options to ease your Rails 6.1 upgrade.
#
# Once upgraded flip defaults one by one to migrate to the new default.
#
# Read the Guide for Upgrading Ruby on Rails for more info on each option.

# Support for inversing belongs_to -> has_many Active Record associations.
# Rails.application.config.active_record.has_many_inversing = true

# Track Active Storage variants in the database.
Rails.application.config.active_storage.track_variants = true

# Apply random variation to the delay when retrying failed jobs.
# Rails.application.config.active_job.retry_jitter = 0.15

# Stop executing `after_enqueue`/`after_perform` callbacks if
# `before_enqueue`/`before_perform` respectively halts with `throw :abort`.
# Rails.application.config.active_job.skip_after_callbacks_if_terminated = true

# Specify cookies SameSite protection level: either :none, :lax, or :strict.
#
# This change is not backwards compatible with earlier Rails versions.
# It's best enabled when your entire app is migrated and stable on 6.1.
# Rails.application.config.action_dispatch.cookies_same_site_protection = :lax

# Generate CSRF tokens that are encoded in URL-safe Base64.
#
# This change is not backwards compatible with earlier Rails versions.
# It's best enabled when your entire app is migrated and stable on 6.1.
# Rails.application.config.action_controller.urlsafe_csrf_tokens = true

# Specify whether `ActiveSupport::TimeZone.utc_to_local` returns a time with an
# UTC offset or a UTC time.
# ActiveSupport.utc_to_local_returns_utc_offset_times = true

# Change the default HTTP status code to `308` when redirecting non-GET/HEAD
# requests to HTTPS in `ActionDispatch::SSL` middleware.
# Rails.application.config.action_dispatch.ssl_default_redirect_status = 308

# Use new connection handling API. For most applications this won't have any
# effect. For applications using multiple databases, this new API provides
# support for granular connection swapping.
# Rails.application.config.active_record.legacy_connection_handling = false

# Make `form_with` generate non-remote forms by default.
# Rails.application.config.action_view.form_with_generates_remote_forms = false

# Set the default queue name for the analysis job to the queue adapter default.
# Rails.application.config.active_storage.queues.analysis = nil

# Set the default queue name for the purge job to the queue adapter default.
# Rails.application.config.active_storage.queues.purge = nil

# Set the default queue name for the incineration job to the queue adapter default.
# Rails.application.config.action_mailbox.queues.incineration = nil

# Set the default queue name for the routing job to the queue adapter default.
# Rails.application.config.action_mailbox.queues.routing = nil

# Set the default queue name for the mail deliver job to the queue adapter default.
# Rails.application.config.action_mailer.deliver_later_queue_name = nil

# Generate a `Link` header that gives a hint to modern browsers about
# preloading assets when using `javascript_include_tag` and `stylesheet_link_tag`.
# Rails.application.config.action_view.preload_links_header = true
11 changes: 11 additions & 0 deletions config/initializers/permissions_policy.rb
@@ -0,0 +1,11 @@
# Define an application-wide HTTP permissions policy. For further
# information see https://developers.google.com/web/updates/2018/06/feature-policy
#
# Rails.application.config.permissions_policy do |f|
# f.camera :none
# f.gyroscope :none
# f.microphone :none
# f.usb :none
# f.fullscreen :self
# f.payment :self, "https://secure.example.com"
# end
2 changes: 1 addition & 1 deletion config/initializers/wrap_parameters.rb
Expand Up @@ -10,5 +10,5 @@

# To enable root element in JSON for ActiveRecord objects.
# ActiveSupport.on_load(:active_record) do
# self.include_root_in_json = true
# self.include_root_in_json = true
# end
59 changes: 38 additions & 21 deletions config/puma.rb
@@ -1,26 +1,43 @@
workers Integer(ENV['PUMA_WORKERS'] || 0)
threads Integer(ENV['MIN_THREADS'] || 1), Integer(ENV['MAX_THREADS'] || 1)
# Puma can serve each request in a thread from an internal thread pool.
# The `threads` method setting takes two numbers: a minimum and maximum.
# Any libraries that use thread pools should be configured to match
# the maximum value specified for Puma. Default is set to 5 threads for minimum
# and maximum; this matches the default thread size of Active Record.
#
max_threads_count = ENV['RAILS_MAX_THREADS'] || ENV['MAX_THREADS'] || 5
min_threads_count = ENV['RAILS_MIN_THREADS'] || ENV['MIN_THREADS'] || max_threads_count
threads min_threads_count, max_threads_count

preload_app!
# Specifies the `worker_timeout` threshold that Puma will use to wait before
# terminating a worker in development environments.
#
worker_timeout 3600 if ENV.fetch("RAILS_ENV", "development") == "development"

# Specifies the `port` that Puma will listen on to receive requests; default is 3000.
#
port ENV.fetch("PORT") { 3000 }

rackup DefaultRackup
port ENV['PORT'] || 3000
environment ENV['RAILS_ENV'] || 'development'
# Specifies the `environment` that Puma will run in.
#
environment ENV.fetch("RAILS_ENV") { "development" }

if ENV['RAILS_ENV'] == 'development'
puts "Running in development mode, upping worker_timeout to 7200..."
worker_timeout 7200
end
# Specifies the `pidfile` that Puma will use.
pidfile ENV.fetch("PIDFILE") { "tmp/pids/server.pid" }

if ENV['LOOMIO_SSL_KEY']
ssl_bind '0.0.0.0', '9292', {
key: ENV['LOOMIO_SSL_KEY'],
cert: ENV['LOOMIO_SSL_CERT']
}
end
# Specifies the number of `workers` to boot in clustered mode.
# Workers are forked web server processes. If using threads and workers together
# the concurrency of the application would be max `threads` * `workers`.
# Workers do not work on JRuby or Windows (both of which do not support
# processes).
#
workers ENV['PUMA_WORKERS'] || ENV['WEB_CONCURRENCY'] || 0

# Use the `preload_app!` method when specifying a `workers` number.
# This directive tells Puma to first boot the application and load code
# before forking the application. This takes advantage of Copy On Write
# process behavior so workers use less memory.
#
preload_app!

on_worker_boot do
ActiveSupport.on_load(:active_record) do
ActiveRecord::Base.establish_connection
end
end
# Allow puma to be restarted by `rails restart` command.
plugin :tmp_restart
2 changes: 1 addition & 1 deletion config/routes.rb
Expand Up @@ -10,7 +10,7 @@ def dev_routes_for(namespace)

require 'sidekiq/web'

Loomio::Application.routes.draw do
Rails.application.routes.draw do
authenticate :user, lambda { |u| u.is_admin? } do
mount Sidekiq::Web => '/admin/sidekiq'
mount Blazer::Engine, at: "/admin/blazer"
Expand Down
12 changes: 6 additions & 6 deletions config/spring.rb
@@ -1,6 +1,6 @@
%w[
.ruby-version
.rbenv-vars
tmp/restart.txt
tmp/caching-dev.txt
].each { |path| Spring.watch(path) }
Spring.watch(
".ruby-version",
".rbenv-vars",
"tmp/restart.txt",
"tmp/caching-dev.txt"
)
7 changes: 7 additions & 0 deletions db/migrate/20210831085201_rewrite_active_storage_links.rb
@@ -0,0 +1,7 @@
class RewriteActiveStorageLinks < ActiveRecord::Migration[6.1]
def change
return if ENV['CANONICAL_HOST'] == 'www.loomio.org'
MigrateEventsService.rewrite_inline_images
MigrateEventsService.rewrite_attachment_links
end
end

0 comments on commit 5ed6beb

Please sign in to comment.