Skip to content

Commit

Permalink
Comply with Zeitwerk autoloader (#3465)
Browse files Browse the repository at this point in the history
### 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 <[email protected]>
  • Loading branch information
parndt and bricesanchez authored Mar 11, 2022
1 parent 40bb4c2 commit 6ec054d
Show file tree
Hide file tree
Showing 29 changed files with 53 additions and 31 deletions.
9 changes: 6 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down Expand Up @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 2 additions & 1 deletion bin/rails
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 3 additions & 3 deletions core/app/assets/stylesheets/refinery/sections/_layout.scss
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ noscript .flash.flash_alert a:hover {
float: left;
margin-right: 1em;
margin-bottom: 1em;

img {
display: block;
}
Expand Down Expand Up @@ -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;
}

Expand Down
12 changes: 12 additions & 0 deletions core/config/initializers/zeitwerk.rb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion core/lib/generators/refinery/engine/templates/Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'refinerycms-core'
require 'refinery/core'

module Refinery
autoload :<%= extension_plural_class_name %>Generator, 'generators/refinery/<%= extension_plural_name %>_generator'
Expand Down
2 changes: 1 addition & 1 deletion core/lib/refinery/cli.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'thor'

module Refinery
class CLI < Thor
class Cli < Thor
include Thor::Actions

no_tasks do
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions core/lib/refinerycms/core.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require_relative '../refinery/core'
6 changes: 3 additions & 3 deletions core/lib/tasks/refinery.rake
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion core/spec/lib/refinery/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
require 'refinery/cli'
require "rake"

describe "CLI" do
describe "Cli" do
let(:rake) { Rake::Application.new }

before do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require 'spec_helper_no_rails'
require 'refinery/users_manager'
require 'refinery/core/users_manager'

module Refinery
module Core
Expand Down
2 changes: 1 addition & 1 deletion core/spec/support/refinery.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'refinerycms-testing'
require 'refinery/testing'

RSpec.configure do |config|
config.extend Refinery::Testing::ControllerMacros::Authentication, :type => :controller
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion dragonfly/Rakefile
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 1 addition & 0 deletions dragonfly/lib/refinerycms/dragonfly.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require_relative '../refinery/dragonfly'
4 changes: 2 additions & 2 deletions images/lib/refinery/images.rb
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
1 change: 0 additions & 1 deletion images/lib/refinerycms-images.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
require 'refinery/images'

1 change: 1 addition & 0 deletions images/lib/refinerycms/images.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require_relative '../refinery/images'
4 changes: 2 additions & 2 deletions pages/lib/refinery/pages.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'refinerycms-core'
require 'refinerycms/core'
require 'mobility'

module Refinery
Expand Down Expand Up @@ -51,4 +51,4 @@ module Admin
require 'friendly_id'
require 'seo_meta'
require 'babosa'
require 'speakingurl-rails'
require 'speakingurl-rails'
1 change: 1 addition & 0 deletions pages/lib/refinerycms/pages.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require_relative '../refinery/pages'
4 changes: 2 additions & 2 deletions resources/lib/refinery/resources.rb
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
1 change: 1 addition & 0 deletions resources/lib/refinerycms/resources.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require_relative '../refinery/resources'
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
4 changes: 2 additions & 2 deletions testing/lib/refinery/tasks/testing.rake
Original file line number Diff line number Diff line change
Expand Up @@ -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'}]

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion testing/lib/refinery/testing.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'refinerycms-core'
require 'refinerycms/core'
require 'rspec-rails'
require 'factory_bot'
require 'rails-controller-testing'
Expand Down
1 change: 1 addition & 0 deletions testing/lib/refinerycms/testing.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require_relative '../refinery/testing'

0 comments on commit 6ec054d

Please sign in to comment.