From 6ec054d75f715f40b654338446020df9ed80f05f Mon Sep 17 00:00:00 2001 From: Philip Arndt Date: Fri, 11 Mar 2022 13:24:37 +1300 Subject: [PATCH] Comply with Zeitwerk autoloader (#3465) ### Context For full Zeitwerk support, we require all gems that currently depend on something in Refinery by requiring the gem's name to update the require to a path inside the gem. For example, if a gem had: require 'refinerycms-pages' This has to change to: require 'refinery/pages' Not a huge code change, but needs to be done everywhere. ### Changes Applies the above suggestion to code that lives inside this repository. ### Considerations One issue is that this kind of violates the namespace expectation of a gem called `refinerycms-core`, which should have `lib/refinerycms/core.rb`. Perhaps this is worth a lot of consideration as an alternative. Resolves #3464 Co-authored-by: Brice Sanchez --- Gemfile | 9 ++++++--- Rakefile | 2 +- bin/rails | 3 ++- .../stylesheets/refinery/sections/_layout.scss | 6 +++--- core/config/initializers/zeitwerk.rb | 12 ++++++++++++ .../dummy/templates/rails/application.rb.erb | 3 +-- .../generators/refinery/engine/templates/Rakefile | 2 +- .../engine/templates/lib/refinery/plural_name.rb.erb | 2 +- core/lib/refinery/cli.rb | 2 +- core/lib/refinery/{ => core}/users_manager.rb | 0 core/lib/refinerycms/core.rb | 1 + core/lib/tasks/refinery.rake | 6 +++--- core/spec/lib/refinery/cli_spec.rb | 2 +- .../lib/refinery/{ => core}/users_manager_spec.rb | 2 +- core/spec/support/refinery.rb | 2 +- ...ge collection to an extension using PageImages.md | 2 +- dragonfly/Rakefile | 2 +- dragonfly/lib/refinerycms/dragonfly.rb | 1 + images/lib/refinery/images.rb | 4 ++-- images/lib/refinerycms-images.rb | 1 - images/lib/refinerycms/images.rb | 1 + pages/lib/refinery/pages.rb | 4 ++-- pages/lib/refinerycms/pages.rb | 1 + resources/lib/refinery/resources.rb | 4 ++-- resources/lib/refinerycms/resources.rb | 1 + spec/spec_helper.rb | 2 ++ testing/lib/refinery/tasks/testing.rake | 4 ++-- testing/lib/refinery/testing.rb | 2 +- testing/lib/refinerycms/testing.rb | 1 + 29 files changed, 53 insertions(+), 31 deletions(-) create mode 100644 core/config/initializers/zeitwerk.rb rename core/lib/refinery/{ => core}/users_manager.rb (100%) create mode 100644 core/lib/refinerycms/core.rb rename core/spec/lib/refinery/{ => core}/users_manager_spec.rb (75%) create mode 100644 dragonfly/lib/refinerycms/dragonfly.rb create mode 100644 images/lib/refinerycms/images.rb create mode 100644 pages/lib/refinerycms/pages.rb create mode 100644 resources/lib/refinerycms/resources.rb create mode 100644 testing/lib/refinerycms/testing.rb diff --git a/Gemfile b/Gemfile index 4ae7be828d..4f90b258f2 100644 --- a/Gemfile +++ b/Gemfile @@ -9,10 +9,12 @@ path "./" do gem "refinerycms-resources" end -gem 'refinerycms-i18n', github: 'refinery/refinerycms-i18n', branch: 'master' +gem 'refinerycms-i18n', github: 'refinery/refinerycms-i18n', branch: 'zeitwerk' # Add support for refinerycms-acts-as-indexed -gem 'refinerycms-acts-as-indexed', ['~> 3.0', '>= 3.0.0'] +gem 'refinerycms-acts-as-indexed', ['~> 4.0', '>= 4.0.0'], + git: 'https://github.com/refinery/refinerycms-acts-as-indexed', + branch: 'zeitwerk' # Add the default visual editor, for now. gem 'refinerycms-wymeditor', ['~> 3.0', '>= 3.0.0'] @@ -49,7 +51,8 @@ group :test do gem 'launchy' gem 'coveralls', require: false gem 'rspec-retry' - gem 'puma' + gem 'falcon' + gem 'falcon-capybara' # TODO: Use beta source for Rails 6 support gem 'rspec-rails', '~> 4.0.0.beta3' diff --git a/Rakefile b/Rakefile index 5faf5d3ec1..75e56a31f1 100644 --- a/Rakefile +++ b/Rakefile @@ -15,7 +15,7 @@ Dir[File.expand_path('../tasks/**/*', __FILE__)].each do |task| load task end -require "refinerycms-testing" +require "refinery/testing" Refinery::Testing::Railtie.load_dummy_tasks File.dirname(__FILE__) desc "Build gem files for all projects" diff --git a/bin/rails b/bin/rails index 7fcd79c93b..aa42ad50fa 100755 --- a/bin/rails +++ b/bin/rails @@ -7,5 +7,6 @@ begin rescue LoadError => load_error warn "No dummy Rails application found! \n" \ "To create one in spec/dummy, please run: \n\n" \ - " rake refinery:testing:dummy_app" + " rake refinery:testing:dummy_app\n\n" \ + "#{load_error}" end diff --git a/core/app/assets/stylesheets/refinery/sections/_layout.scss b/core/app/assets/stylesheets/refinery/sections/_layout.scss index 70ef789ddd..cfd137e04a 100644 --- a/core/app/assets/stylesheets/refinery/sections/_layout.scss +++ b/core/app/assets/stylesheets/refinery/sections/_layout.scss @@ -266,7 +266,7 @@ noscript .flash.flash_alert a:hover { float: left; margin-right: 1em; margin-bottom: 1em; - + img { display: block; } @@ -1073,9 +1073,9 @@ a img { /* page parts */ -ul#page_parts, +ul#page_parts, ul#page_parts_controls { - margin: 0; + margin: 12px 0 0 0; padding: 0; } diff --git a/core/config/initializers/zeitwerk.rb b/core/config/initializers/zeitwerk.rb new file mode 100644 index 0000000000..60dee200c3 --- /dev/null +++ b/core/config/initializers/zeitwerk.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +class RefineryInflector < Zeitwerk::Inflector + def camelize(basename, abspath) + return 'Refinery' if basename.start_with?('refinerycms-') + super + end +end + +Rails.autoloaders.each do |autoloader| + autoloader.inflector = RefineryInflector.new +end diff --git a/core/lib/generators/refinery/dummy/templates/rails/application.rb.erb b/core/lib/generators/refinery/dummy/templates/rails/application.rb.erb index 4282a5024e..d65a67e71a 100644 --- a/core/lib/generators/refinery/dummy/templates/rails/application.rb.erb +++ b/core/lib/generators/refinery/dummy/templates/rails/application.rb.erb @@ -1,7 +1,7 @@ require File.expand_path('../boot', __FILE__) require 'rails/all' -require 'refinerycms-core' +require 'refinerycms/core' # Require the gems listed in Gemfile, including any gems # you've limited to :test, :development, or :production. @@ -11,7 +11,6 @@ module Dummy class Application < Rails::Application # Initialize configuration defaults for originally generated Rails version. config.load_defaults 6.0 - config.autoloader = :classic # Settings in config/environments/* take precedence over those specified here. # Application configuration can go into files in config/initializers diff --git a/core/lib/generators/refinery/engine/templates/Rakefile b/core/lib/generators/refinery/engine/templates/Rakefile index b390735b63..834f8539f5 100644 --- a/core/lib/generators/refinery/engine/templates/Rakefile +++ b/core/lib/generators/refinery/engine/templates/Rakefile @@ -12,7 +12,7 @@ if File.exists? APP_RAKEFILE load 'rails/tasks/engine.rake' end -require "refinerycms-testing" +require "refinery/testing" Refinery::Testing::Railtie.load_dummy_tasks ENGINE_PATH load File.expand_path('../tasks/testing.rake', __FILE__) diff --git a/core/lib/generators/refinery/engine/templates/lib/refinery/plural_name.rb.erb b/core/lib/generators/refinery/engine/templates/lib/refinery/plural_name.rb.erb index 0339f060ac..95583b637d 100644 --- a/core/lib/generators/refinery/engine/templates/lib/refinery/plural_name.rb.erb +++ b/core/lib/generators/refinery/engine/templates/lib/refinery/plural_name.rb.erb @@ -1,4 +1,4 @@ -require 'refinerycms-core' +require 'refinery/core' module Refinery autoload :<%= extension_plural_class_name %>Generator, 'generators/refinery/<%= extension_plural_name %>_generator' diff --git a/core/lib/refinery/cli.rb b/core/lib/refinery/cli.rb index c4366b5ae0..68b6914fae 100644 --- a/core/lib/refinery/cli.rb +++ b/core/lib/refinery/cli.rb @@ -1,7 +1,7 @@ require 'thor' module Refinery - class CLI < Thor + class Cli < Thor include Thor::Actions no_tasks do diff --git a/core/lib/refinery/users_manager.rb b/core/lib/refinery/core/users_manager.rb similarity index 100% rename from core/lib/refinery/users_manager.rb rename to core/lib/refinery/core/users_manager.rb diff --git a/core/lib/refinerycms/core.rb b/core/lib/refinerycms/core.rb new file mode 100644 index 0000000000..d7e9a78333 --- /dev/null +++ b/core/lib/refinerycms/core.rb @@ -0,0 +1 @@ +require_relative '../refinery/core' diff --git a/core/lib/tasks/refinery.rake b/core/lib/tasks/refinery.rake index 6bf992bf6c..6db8c9b5d1 100644 --- a/core/lib/tasks/refinery.rake +++ b/core/lib/tasks/refinery.rake @@ -1,18 +1,18 @@ namespace :refinery do desc "Override files for use in an application" task :override => :environment do - Refinery::CLI.new.override(ENV) + Refinery::Cli.new.override(ENV) end desc "Override files for use in an application" namespace :override do task :list => :environment do - Refinery::CLI.new.override_list(ENV) + Refinery::Cli.new.override_list(ENV) end end desc "Un-crudify a method on a controller that uses crudify" task :uncrudify => :environment do - Refinery::CLI.new.uncrudify(ENV['controller'], ENV['action']) + Refinery::Cli.new.uncrudify(ENV['controller'], ENV['action']) end end diff --git a/core/spec/lib/refinery/cli_spec.rb b/core/spec/lib/refinery/cli_spec.rb index 622f95c34e..f68a5b0711 100644 --- a/core/spec/lib/refinery/cli_spec.rb +++ b/core/spec/lib/refinery/cli_spec.rb @@ -2,7 +2,7 @@ require 'refinery/cli' require "rake" -describe "CLI" do +describe "Cli" do let(:rake) { Rake::Application.new } before do diff --git a/core/spec/lib/refinery/users_manager_spec.rb b/core/spec/lib/refinery/core/users_manager_spec.rb similarity index 75% rename from core/spec/lib/refinery/users_manager_spec.rb rename to core/spec/lib/refinery/core/users_manager_spec.rb index 1cfbb4500b..18c1ab3e41 100644 --- a/core/spec/lib/refinery/users_manager_spec.rb +++ b/core/spec/lib/refinery/core/users_manager_spec.rb @@ -1,5 +1,5 @@ require 'spec_helper_no_rails' -require 'refinery/users_manager' +require 'refinery/core/users_manager' module Refinery module Core diff --git a/core/spec/support/refinery.rb b/core/spec/support/refinery.rb index 3506fa6497..d6bc9e5a6f 100644 --- a/core/spec/support/refinery.rb +++ b/core/spec/support/refinery.rb @@ -1,4 +1,4 @@ -require 'refinerycms-testing' +require 'refinery/testing' RSpec.configure do |config| config.extend Refinery::Testing::ControllerMacros::Authentication, :type => :controller diff --git a/doc/guides/4 - Refinery Extensions/9 - Adding an image collection to an extension using PageImages.md b/doc/guides/4 - Refinery Extensions/9 - Adding an image collection to an extension using PageImages.md index 4ec3a4bdc1..f8dab837c8 100644 --- a/doc/guides/4 - Refinery Extensions/9 - Adding an image collection to an extension using PageImages.md +++ b/doc/guides/4 - Refinery Extensions/9 - Adding an image collection to an extension using PageImages.md @@ -75,7 +75,7 @@ end Edit the file `vendor/extensions/shows/lib/refinery/shows.rb`: ```ruby -require 'refinerycms-core' +require 'refinery/core' module Refinery autoload :ShowsGenerator, 'generators/refinery/shows_generator' diff --git a/dragonfly/Rakefile b/dragonfly/Rakefile index ae2c44e57c..acc1dac130 100644 --- a/dragonfly/Rakefile +++ b/dragonfly/Rakefile @@ -1,7 +1,7 @@ namespace :refinery do namespace :testing do task :setup_extension do - require 'refinerycms-dragonfly' + require 'refinery/dragonfly' Refinery::DragonflyGenerator.start %w[--quiet] end end diff --git a/dragonfly/lib/refinerycms/dragonfly.rb b/dragonfly/lib/refinerycms/dragonfly.rb new file mode 100644 index 0000000000..3f7f8bd7a0 --- /dev/null +++ b/dragonfly/lib/refinerycms/dragonfly.rb @@ -0,0 +1 @@ +require_relative '../refinery/dragonfly' diff --git a/images/lib/refinery/images.rb b/images/lib/refinery/images.rb index 8f89d89cb1..6303b17179 100644 --- a/images/lib/refinery/images.rb +++ b/images/lib/refinery/images.rb @@ -1,5 +1,5 @@ -require 'refinerycms-core' -require 'refinerycms-dragonfly' +require 'refinerycms/core' +require 'refinerycms/dragonfly' module Refinery autoload :ImagesGenerator, 'generators/refinery/images/images_generator' diff --git a/images/lib/refinerycms-images.rb b/images/lib/refinerycms-images.rb index ab6f69c86c..5bee13a4d9 100644 --- a/images/lib/refinerycms-images.rb +++ b/images/lib/refinerycms-images.rb @@ -1,2 +1 @@ require 'refinery/images' - diff --git a/images/lib/refinerycms/images.rb b/images/lib/refinerycms/images.rb new file mode 100644 index 0000000000..ced88fab80 --- /dev/null +++ b/images/lib/refinerycms/images.rb @@ -0,0 +1 @@ +require_relative '../refinery/images' diff --git a/pages/lib/refinery/pages.rb b/pages/lib/refinery/pages.rb index 4a1f24f853..30c7661619 100644 --- a/pages/lib/refinery/pages.rb +++ b/pages/lib/refinery/pages.rb @@ -1,4 +1,4 @@ -require 'refinerycms-core' +require 'refinerycms/core' require 'mobility' module Refinery @@ -51,4 +51,4 @@ module Admin require 'friendly_id' require 'seo_meta' require 'babosa' -require 'speakingurl-rails' \ No newline at end of file +require 'speakingurl-rails' diff --git a/pages/lib/refinerycms/pages.rb b/pages/lib/refinerycms/pages.rb new file mode 100644 index 0000000000..c0b34899ea --- /dev/null +++ b/pages/lib/refinerycms/pages.rb @@ -0,0 +1 @@ +require_relative '../refinery/pages' diff --git a/resources/lib/refinery/resources.rb b/resources/lib/refinery/resources.rb index 200c1f8399..18dcb79d88 100644 --- a/resources/lib/refinery/resources.rb +++ b/resources/lib/refinery/resources.rb @@ -1,5 +1,5 @@ -require 'refinerycms-core' -require 'refinerycms-dragonfly' +require 'refinerycms/core' +require 'refinerycms/dragonfly' module Refinery autoload :ResourcesGenerator, 'generators/refinery/resources/resources_generator' diff --git a/resources/lib/refinerycms/resources.rb b/resources/lib/refinerycms/resources.rb new file mode 100644 index 0000000000..5b3b68ab88 --- /dev/null +++ b/resources/lib/refinerycms/resources.rb @@ -0,0 +1 @@ +require_relative '../refinery/resources' diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 5a3b22875c..ffb8cbb115 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -17,6 +17,8 @@ require 'rspec/rails' require 'capybara/rspec' require 'webdrivers/chromedriver' +require 'falcon/capybara' +Capybara.server = :falcon if ENV['RETRY_COUNT'] require 'rspec/retry' diff --git a/testing/lib/refinery/tasks/testing.rake b/testing/lib/refinery/tasks/testing.rake index 1e018c8b26..a6bac01264 100644 --- a/testing/lib/refinery/tasks/testing.rake +++ b/testing/lib/refinery/tasks/testing.rake @@ -20,7 +20,7 @@ namespace :refinery do desc "Sets up just the dummy application for testing, no migrations or extensions" task :setup_dummy_app do - require 'refinerycms-core' + require 'refinerycms/core' Refinery::DummyGenerator.start %W[--quiet --database=#{ENV['DB'].presence || 'sqlite3'}] @@ -36,7 +36,7 @@ namespace :refinery do # namespace :refinery do # namespace :testing do # task :setup_extension do - # require 'refinerycms-my-extension' + # require 'refinery/my-extension' # Refinery::MyEngineGenerator.start %w[--quiet] # end # end diff --git a/testing/lib/refinery/testing.rb b/testing/lib/refinery/testing.rb index f73bde1dd7..08da08f5b9 100644 --- a/testing/lib/refinery/testing.rb +++ b/testing/lib/refinery/testing.rb @@ -1,4 +1,4 @@ -require 'refinerycms-core' +require 'refinerycms/core' require 'rspec-rails' require 'factory_bot' require 'rails-controller-testing' diff --git a/testing/lib/refinerycms/testing.rb b/testing/lib/refinerycms/testing.rb new file mode 100644 index 0000000000..534fdcb4e4 --- /dev/null +++ b/testing/lib/refinerycms/testing.rb @@ -0,0 +1 @@ +require_relative '../refinery/testing'