Skip to content

Commit

Permalink
Merge pull request #2225 from tvdeyen/rails-7
Browse files Browse the repository at this point in the history
Rails 7 Support
  • Loading branch information
tvdeyen authored Apr 11, 2022
2 parents 469301b + 19309ce commit 26eacf8
Show file tree
Hide file tree
Showing 25 changed files with 210 additions and 278 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,21 @@ jobs:
rails:
- "6.0"
- "6.1"
- "7.0"
ruby:
- "2.6"
- "2.7"
- "3.0"
database:
- mysql
- postgresql
exclude:
- rails: "7.0"
ruby: "2.6"
database: mysql
- rails: "7.0"
ruby: "2.6"
database: postgresql
env:
DB: ${{ matrix.database }}
DB_USER: alchemy_user
Expand Down
4 changes: 3 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ source "https://rubygems.org"

gemspec

rails_version = ENV.fetch("RAILS_VERSION", 6.1).to_f
rails_version = ENV.fetch("RAILS_VERSION", 7.0).to_f
# Necessary until a new 6.1.5 version has been released
# https://github.com/rails/rails/pull/44691
if rails_version.to_s.match?(/6.1/)
gem "rails", git: "https://github.com/rails/rails", branch: "6-1-stable"
elsif rails_version.to_s.match?(/7.0/)
gem "rails", git: "https://github.com/rails/rails", branch: "7-0-stable"
else
gem "rails", "~> #{rails_version}.0"
end
Expand Down
2 changes: 1 addition & 1 deletion alchemy_cms.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Gem::Specification.new do |gem|
activesupport
railties
].each do |rails_gem|
gem.add_runtime_dependency rails_gem, [">= 6.0", "< 6.2"]
gem.add_runtime_dependency rails_gem, [">= 6.0", "< 7.1"]
end

gem.add_runtime_dependency "active_model_serializers", ["~> 0.10.0"]
Expand Down
8 changes: 2 additions & 6 deletions app/controllers/alchemy/admin/elements_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,18 +106,14 @@ def fold
def element_includes
[
{
contents: {
essence: :ingredient_association,
},
contents: :essence,
ingredients: :related_object,
},
:tags,
{
all_nested_elements: [
{
contents: {
essence: :ingredient_association,
},
contents: :essence,
ingredients: :related_object,
},
:tags,
Expand Down
9 changes: 6 additions & 3 deletions app/controllers/alchemy/admin/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,12 @@ def unlock
@pages_locked_by_user = Page.from_current_site.locked_by(current_alchemy_user)
respond_to do |format|
format.js
format.html {
redirect_to params[:redirect_to].blank? ? admin_pages_path : params[:redirect_to]
}
format.html do
redirect_to(
params[:redirect_to].presence || admin_pages_path,
allow_other_host: true,
)
end
end
end

Expand Down
6 changes: 1 addition & 5 deletions app/controllers/alchemy/api/contents_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,7 @@ def show
private

def content_includes
[
{
essence: :ingredient_association,
},
]
%i[essence]
end
end
end
8 changes: 2 additions & 6 deletions app/controllers/alchemy/api/elements_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,14 @@ def element_includes
{
nested_elements: [
{
contents: {
essence: :ingredient_association,
},
contents: :essence,
ingredients: :related_object,
},
:tags,
],
},
{
contents: {
essence: :ingredient_association,
},
contents: :essence,
ingredients: :related_object,
},
:tags,
Expand Down
8 changes: 2 additions & 6 deletions app/controllers/alchemy/api/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,17 +112,13 @@ def page_includes
{
nested_elements: [
{
contents: {
essence: :ingredient_association,
},
contents: :essence,
},
:tags,
],
},
{
contents: {
essence: :ingredient_association,
},
contents: :essence,
},
:tags,
],
Expand Down
2 changes: 1 addition & 1 deletion lib/alchemy/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Engine < Rails::Engine
end
end

initializer "alchemy.userstamp" do
config.after_initialize do
if Alchemy.user_class
ActiveSupport.on_load(:active_record) do
Alchemy.user_class.model_stamper
Expand Down
26 changes: 0 additions & 26 deletions lib/alchemy/essence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,6 @@
require "active_record"

module Alchemy #:nodoc:
# A bogus association that skips eager loading for essences not having an ingredient association
class IngredientAssociation < ActiveRecord::Associations::BelongsToAssociation
# Skip eager loading if called by Rails' preloader
def klass
if caller.any? { |line| line =~ /preloader\.rb/ }
nil
else
super
end
end
end

module Essence #:nodoc:
def self.included(base)
base.extend(ClassMethods)
Expand Down Expand Up @@ -43,8 +31,6 @@ def acts_as_essence(options = {})
ingredient_column: "body",
}.update(options)

@_classes_with_ingredient_association ||= []

class_eval <<-RUBY, __FILE__, __LINE__ + 1
attr_writer :validation_errors
include Alchemy::Essence::InstanceMethods
Expand Down Expand Up @@ -87,18 +73,6 @@ def preview_text_column
alias_method :#{configuration[:ingredient_column]}, :ingredient_association
alias_method :#{configuration[:ingredient_column]}=, :ingredient_association=
RUBY

@_classes_with_ingredient_association << self
end
end

# Overwrite ActiveRecords method to return a bogus association class that skips eager loading
# for essence classes that do not have an ingredient association
def _reflect_on_association(name)
if name == :ingredient_association && !in?(@_classes_with_ingredient_association)
OpenStruct.new(association_class: Alchemy::IngredientAssociation)
else
super
end
end

Expand Down
12 changes: 0 additions & 12 deletions lib/alchemy/test_support/essence_shared_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,6 @@
let(:content) { Alchemy::Content.new(name: "foo") }
let(:content_definition) { { "name" => "foo" } }

describe "eager loading" do
before do
2.times { described_class.create! }
end

it "does not throw error if eager loaded" do
expect {
described_class.all.includes(:ingredient_association).to_a
}.to_not raise_error
end
end

it "touches the element after save" do
element = FactoryBot.create(:alchemy_element)
content = FactoryBot.create(:alchemy_content, element: element, essence: essence, essence_type: essence.class.name)
Expand Down
2 changes: 1 addition & 1 deletion lib/alchemy/upgrader/tasks/ingredients_migrator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def create_ingredients
# eager load all elements that have ingredients defined but no ingredient records yet.
all_elements = Alchemy::Element
.named(elements_with_ingredients.map { |d| d[:name] })
.includes(contents: { essence: :ingredient_association })
.includes(contents: :essence)
.left_outer_joins(:ingredients).where(alchemy_ingredients: { id: nil })
.to_a
elements_with_ingredients.map do |element_definition|
Expand Down
1 change: 0 additions & 1 deletion spec/dummy/bin/rails
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env ruby
# frozen_string_literal: true
APP_PATH = File.expand_path("../config/application", __dir__)
require_relative "../config/boot"
require "rails/commands"
1 change: 0 additions & 1 deletion spec/dummy/bin/rake
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env ruby
# frozen_string_literal: true
require_relative "../config/boot"
require "rake"
Rake.application.run
12 changes: 4 additions & 8 deletions spec/dummy/bin/setup
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env ruby
# frozen_string_literal: true
require "fileutils"

# path to your application root.
Expand All @@ -10,20 +9,17 @@ def system!(*args)
end

FileUtils.chdir APP_ROOT do
# This script is a way to setup or update your development environment automatically.
# This script is idempotent, so that you can run it at anytime and get an expectable outcome.
# 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.

puts "== Installing dependencies =="
system! "gem install bundler --conservative"
system("bundle check") || system!("bundle install")

# Install JavaScript dependencies if using Yarn
# system('bin/yarn')

# puts "\n== Copying sample files =="
# unless File.exist?('config/database.yml')
# FileUtils.cp 'config/database.yml.sample', 'config/database.yml'
# unless File.exist?("config/database.yml")
# FileUtils.cp "config/database.yml.sample", "config/database.yml"
# end

puts "\n== Preparing database =="
Expand Down
2 changes: 1 addition & 1 deletion spec/dummy/config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module Dummy
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
if config.respond_to?(:load_defaults)
config.load_defaults ENV["RAILS_VERSION"] || 6.1
config.load_defaults ENV["RAILS_VERSION"] || 7.0
end

# Settings in config/environments/* take precedence over those specified here.
Expand Down
7 changes: 5 additions & 2 deletions spec/dummy/config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@
# Show full error reports.
config.consider_all_requests_local = true

# Enable server timing
config.server_timing = true

# Enable/disable caching. By default caching is disabled.
# Run rails dev:cache to toggle caching.
if Rails.root.join("tmp", "caching-dev.txt").exist?
if Rails.root.join("tmp/caching-dev.txt").exist?
config.action_controller.perform_caching = true
config.action_controller.enable_fragment_cache_logging = true

config.cache_store = :memory_store
config.public_file_server.headers = {
"Cache-Control" => "public, max-age=#{2.days.to_i}",
"Cache-Control" => "public, max-age=#{2.days.to_i}"
}
else
config.action_controller.perform_caching = false
Expand Down
47 changes: 10 additions & 37 deletions spec/dummy/config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
config.eager_load = true

# Full error reports are disabled and caching is turned on.
config.consider_all_requests_local = false
config.consider_all_requests_local = false
config.action_controller.perform_caching = true

# Ensures that a master key has been made available in either ENV["RAILS_MASTER_KEY"]
Expand All @@ -31,11 +31,11 @@
config.assets.compile = false

# Enable serving of images, stylesheets, and JavaScripts from an asset server.
# config.asset_host = 'http://assets.example.com'
# config.asset_host = "http://assets.example.com"

# Specifies the header that your server uses for sending files.
# config.action_dispatch.x_sendfile_header = 'X-Sendfile' # for Apache
# config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for NGINX
# config.action_dispatch.x_sendfile_header = "X-Sendfile" # for Apache
# config.action_dispatch.x_sendfile_header = "X-Accel-Redirect" # for NGINX

# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
# config.force_ssl = true
Expand All @@ -45,7 +45,7 @@
config.log_level = :info

# Prepend all log lines with the following tags.
config.log_tags = [:request_id]
config.log_tags = [ :request_id ]

# Use a different cache store in production.
# config.cache_store = :mem_cache_store
Expand All @@ -64,49 +64,22 @@
# the I18n.default_locale when a translation cannot be found).
config.i18n.fallbacks = true

# Send deprecation notices to registered listeners.
config.active_support.deprecation = :notify

# Log disallowed deprecations.
config.active_support.disallowed_deprecation = :log

# Tell Active Support which deprecation messages to disallow.
config.active_support.disallowed_deprecation_warnings = []
# Don't log any deprecations.
config.active_support.report_deprecations = false

# Use default logging formatter so that PID and timestamp are not suppressed.
config.log_formatter = ::Logger::Formatter.new

# Use a different logger for distributed setups.
# require "syslog/logger"
# config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new 'app-name')
# config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new "app-name")

if ENV["RAILS_LOG_TO_STDOUT"].present?
logger = ActiveSupport::Logger.new(STDOUT)
logger = ActiveSupport::Logger.new(STDOUT)
logger.formatter = config.log_formatter
config.logger = ActiveSupport::TaggedLogging.new(logger)
config.logger = ActiveSupport::TaggedLogging.new(logger)
end

# Do not dump schema after migrations.
config.active_record.dump_schema_after_migration = false

# Inserts middleware to perform automatic connection switching.
# The `database_selector` hash is used to pass options to the DatabaseSelector
# middleware. The `delay` is used to determine how long to wait after a write
# to send a subsequent read to the primary.
#
# The `database_resolver` class is used by the middleware to determine which
# database is appropriate to use based on the time delay.
#
# The `database_resolver_context` class is used by the middleware to set
# timestamps for the last write to the primary. The resolver uses the context
# class timestamps to determine how long to wait before reading from the
# replica.
#
# By default Rails will store a last write timestamp in the session. The
# DatabaseSelector middleware is designed as such you can define your own
# strategy for connection switching and pass that into the middleware through
# these configuration options.
# config.active_record.database_selector = { delay: 2.seconds }
# config.active_record.database_resolver = ActiveRecord::Middleware::DatabaseSelector::Resolver
# config.active_record.database_resolver_context = ActiveRecord::Middleware::DatabaseSelector::Resolver::Session
end
Loading

0 comments on commit 26eacf8

Please sign in to comment.