From 9413d23a025448a05103b49e422d849f5be633ac Mon Sep 17 00:00:00 2001 From: Georg Gadinger Date: Thu, 29 Dec 2022 06:49:15 +0100 Subject: [PATCH] let Zeitwerk autoload the `lib/` directory MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit this also allows the UseCase classes to be hot reloaded in dev 🎉 - remove use_case requires (except for the exporter as Zeitwerk doesn't know about the subclasses) - move version.rb to lib/retrospring so that Zeitwerk knows where to find Retrospring::Version --- app/controllers/ajax/moderation_controller.rb | 4 ++-- app/controllers/ajax/question_controller.rb | 3 --- app/controllers/ajax/relationship_controller.rb | 2 -- config/application.rb | 8 +++++--- lib/{ => retrospring}/version.rb | 0 lib/tasks/version.rake | 4 ++-- lib/use_case/data_export/answers.rb | 2 -- lib/use_case/data_export/appendables.rb | 2 -- lib/use_case/data_export/base.rb | 2 -- lib/use_case/data_export/comments.rb | 2 -- lib/use_case/data_export/mute_rules.rb | 2 -- lib/use_case/data_export/questions.rb | 2 -- lib/use_case/data_export/relationships.rb | 2 -- lib/use_case/data_export/theme.rb | 2 -- lib/use_case/data_export/user.rb | 2 -- lib/use_case/question/create.rb | 1 - lib/use_case/question/create_followers.rb | 2 -- lib/use_case/question/destroy.rb | 1 - lib/use_case/relationship/create.rb | 1 - lib/use_case/relationship/destroy.rb | 1 - lib/use_case/user/ban.rb | 2 -- lib/use_case/user/unban.rb | 4 +--- spec/lib/{ => retrospring}/version_spec.rb | 2 -- spec/lib/use_case/data_export/answers_spec.rb | 2 -- spec/lib/use_case/data_export/appendables_spec.rb | 2 -- spec/lib/use_case/data_export/comments_spec.rb | 2 -- spec/lib/use_case/data_export/mute_rules_spec.rb | 2 -- spec/lib/use_case/data_export/questions_spec.rb | 2 -- spec/lib/use_case/data_export/relationships_spec.rb | 2 -- spec/lib/use_case/data_export/theme_spec.rb | 2 -- spec/lib/use_case/data_export/user_spec.rb | 2 -- spec/lib/use_case/question/create_followers_spec.rb | 1 - spec/lib/use_case/question/create_spec.rb | 1 - spec/lib/use_case/question/destroy_spec.rb | 1 - spec/lib/use_case/relationship/create_spec.rb | 1 - spec/lib/use_case/relationship/destroy_spec.rb | 1 - spec/support/example_exporter.rb | 2 -- 37 files changed, 10 insertions(+), 66 deletions(-) rename lib/{ => retrospring}/version.rb (100%) rename spec/lib/{ => retrospring}/version_spec.rb (98%) diff --git a/app/controllers/ajax/moderation_controller.rb b/app/controllers/ajax/moderation_controller.rb index c6f2b976..51dbdc93 100644 --- a/app/controllers/ajax/moderation_controller.rb +++ b/app/controllers/ajax/moderation_controller.rb @@ -1,5 +1,5 @@ -require 'use_case/user/ban' -require 'use_case/user/unban' +# frozen_string_literal: true + require 'errors' class Ajax::ModerationController < AjaxController diff --git a/app/controllers/ajax/question_controller.rb b/app/controllers/ajax/question_controller.rb index 78b88635..43b2c0cf 100644 --- a/app/controllers/ajax/question_controller.rb +++ b/app/controllers/ajax/question_controller.rb @@ -1,9 +1,6 @@ # frozen_string_literal: true require "errors" -require "use_case/question/create" -require "use_case/question/create_followers" -require "use_case/question/destroy" class Ajax::QuestionController < AjaxController def destroy diff --git a/app/controllers/ajax/relationship_controller.rb b/app/controllers/ajax/relationship_controller.rb index b91f72c9..a56ae976 100644 --- a/app/controllers/ajax/relationship_controller.rb +++ b/app/controllers/ajax/relationship_controller.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require "use_case/relationship/create" -require "use_case/relationship/destroy" require "errors" class Ajax::RelationshipController < AjaxController diff --git a/config/application.rb b/config/application.rb index d14b74c7..efb46b32 100644 --- a/config/application.rb +++ b/config/application.rb @@ -8,8 +8,6 @@ start = Time.now Bundler.require(*Rails.groups) puts 'processing time of bundler require: ' + "#{(Time.now - start).round(3).to_s.ljust(5, '0')}s".light_green -require_relative "../lib/version" - module Justask class Application < Rails::Application # Settings in config/environments/* take precedence over those specified here. @@ -17,7 +15,11 @@ module Justask # -- all .rb files in that directory are automatically loaded. config.load_defaults 6.0 - config.autoload_paths += %W["#{config.root}/app/validators"] + # add `lib/` to the autoload paths so zeitwerk can find e.g. our `UseCase`s + # without an explicit `require`, and also take care of hot reloading the code + # (really useful in development!) + config.autoload_paths << config.root.join("lib") + config.eager_load_paths << config.root.join("lib") # Use Sidekiq for background jobs config.active_job.queue_adapter = :sidekiq diff --git a/lib/version.rb b/lib/retrospring/version.rb similarity index 100% rename from lib/version.rb rename to lib/retrospring/version.rb diff --git a/lib/tasks/version.rake b/lib/tasks/version.rake index d3e9009a..7f46da01 100644 --- a/lib/tasks/version.rake +++ b/lib/tasks/version.rake @@ -9,7 +9,7 @@ namespace :version do now = Time.now.utc today_ymd = %i[year month day].map { now.public_send(_1) } - version_path = Rails.root.join("lib/version.rb") + version_path = Rails.root.join("lib/retrospring/version.rb") version_contents = File.read(version_path) patch_contents = lambda do |key, val| @@ -35,7 +35,7 @@ namespace :version do desc "Commit and tag a new release" task commit: :environment do - version_path = Rails.root.join("lib/version.rb") + version_path = Rails.root.join("lib/retrospring/version.rb") puts "Committing version" sh %(git commit -m 'Bump version to #{Retrospring::Version}' -- #{version_path.to_s.inspect}) diff --git a/lib/use_case/data_export/answers.rb b/lib/use_case/data_export/answers.rb index 1443ec6d..e9431e2e 100644 --- a/lib/use_case/data_export/answers.rb +++ b/lib/use_case/data_export/answers.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require "use_case/data_export/base" - module UseCase module DataExport class Answers < UseCase::DataExport::Base diff --git a/lib/use_case/data_export/appendables.rb b/lib/use_case/data_export/appendables.rb index 0525431b..19221aee 100644 --- a/lib/use_case/data_export/appendables.rb +++ b/lib/use_case/data_export/appendables.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require "use_case/data_export/base" - module UseCase module DataExport class Appendables < UseCase::DataExport::Base diff --git a/lib/use_case/data_export/base.rb b/lib/use_case/data_export/base.rb index dc2ded83..4fd9afe5 100644 --- a/lib/use_case/data_export/base.rb +++ b/lib/use_case/data_export/base.rb @@ -2,8 +2,6 @@ require "json" -require "use_case/base" - module UseCase module DataExport class Base < UseCase::Base diff --git a/lib/use_case/data_export/comments.rb b/lib/use_case/data_export/comments.rb index 6a6e820d..708efffa 100644 --- a/lib/use_case/data_export/comments.rb +++ b/lib/use_case/data_export/comments.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require "use_case/data_export/base" - module UseCase module DataExport class Comments < UseCase::DataExport::Base diff --git a/lib/use_case/data_export/mute_rules.rb b/lib/use_case/data_export/mute_rules.rb index 6b8755ba..bc795a0c 100644 --- a/lib/use_case/data_export/mute_rules.rb +++ b/lib/use_case/data_export/mute_rules.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require "use_case/data_export/base" - module UseCase module DataExport class MuteRules < UseCase::DataExport::Base diff --git a/lib/use_case/data_export/questions.rb b/lib/use_case/data_export/questions.rb index 7536e7b6..5996f587 100644 --- a/lib/use_case/data_export/questions.rb +++ b/lib/use_case/data_export/questions.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require "use_case/data_export/base" - module UseCase module DataExport class Questions < UseCase::DataExport::Base diff --git a/lib/use_case/data_export/relationships.rb b/lib/use_case/data_export/relationships.rb index 57fb8cb9..6000cdb9 100644 --- a/lib/use_case/data_export/relationships.rb +++ b/lib/use_case/data_export/relationships.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require "use_case/data_export/base" - module UseCase module DataExport class Relationships < UseCase::DataExport::Base diff --git a/lib/use_case/data_export/theme.rb b/lib/use_case/data_export/theme.rb index 135fc89e..cfd2ec46 100644 --- a/lib/use_case/data_export/theme.rb +++ b/lib/use_case/data_export/theme.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require "use_case/data_export/base" - module UseCase module DataExport class Theme < UseCase::DataExport::Base diff --git a/lib/use_case/data_export/user.rb b/lib/use_case/data_export/user.rb index 6dcbb36f..4f7f8445 100644 --- a/lib/use_case/data_export/user.rb +++ b/lib/use_case/data_export/user.rb @@ -2,8 +2,6 @@ require "httparty" -require "use_case/data_export/base" - module UseCase module DataExport class User < UseCase::DataExport::Base diff --git a/lib/use_case/question/create.rb b/lib/use_case/question/create.rb index 0b17d530..08ea63a0 100644 --- a/lib/use_case/question/create.rb +++ b/lib/use_case/question/create.rb @@ -1,6 +1,5 @@ # frozen_string_literal: true -require "use_case/base" require "errors" module UseCase diff --git a/lib/use_case/question/create_followers.rb b/lib/use_case/question/create_followers.rb index cce75f76..b74517e5 100644 --- a/lib/use_case/question/create_followers.rb +++ b/lib/use_case/question/create_followers.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require "use_case/base" - module UseCase module Question class CreateFollowers < UseCase::Base diff --git a/lib/use_case/question/destroy.rb b/lib/use_case/question/destroy.rb index e41043a1..f348aca5 100644 --- a/lib/use_case/question/destroy.rb +++ b/lib/use_case/question/destroy.rb @@ -1,6 +1,5 @@ # frozen_string_literal: true -require "use_case/base" require "errors" module UseCase diff --git a/lib/use_case/relationship/create.rb b/lib/use_case/relationship/create.rb index 7cca7eea..039a500f 100644 --- a/lib/use_case/relationship/create.rb +++ b/lib/use_case/relationship/create.rb @@ -1,6 +1,5 @@ # frozen_string_literal: true -require "use_case/base" require "errors" module UseCase diff --git a/lib/use_case/relationship/destroy.rb b/lib/use_case/relationship/destroy.rb index c83a44fb..ac0aafd1 100644 --- a/lib/use_case/relationship/destroy.rb +++ b/lib/use_case/relationship/destroy.rb @@ -1,6 +1,5 @@ # frozen_string_literal: true -require "use_case/base" require "errors" module UseCase diff --git a/lib/use_case/user/ban.rb b/lib/use_case/user/ban.rb index 12d0d8d1..63ef69cf 100644 --- a/lib/use_case/user/ban.rb +++ b/lib/use_case/user/ban.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require "use_case/base" - module UseCase module User class Ban < UseCase::Base diff --git a/lib/use_case/user/unban.rb b/lib/use_case/user/unban.rb index 55e15991..f0b8c355 100644 --- a/lib/use_case/user/unban.rb +++ b/lib/use_case/user/unban.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require 'use_case/base' - module UseCase module User class Unban < UseCase::Base @@ -24,4 +22,4 @@ module UseCase end end end -end \ No newline at end of file +end diff --git a/spec/lib/version_spec.rb b/spec/lib/retrospring/version_spec.rb similarity index 98% rename from spec/lib/version_spec.rb rename to spec/lib/retrospring/version_spec.rb index e53e917f..04eff35d 100644 --- a/spec/lib/version_spec.rb +++ b/spec/lib/retrospring/version_spec.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require "version" - RSpec.describe Retrospring::Version do before(:each) do allow(Retrospring::Version).to receive(:year) { 1984 } diff --git a/spec/lib/use_case/data_export/answers_spec.rb b/spec/lib/use_case/data_export/answers_spec.rb index e61d74d0..3fa24f8e 100644 --- a/spec/lib/use_case/data_export/answers_spec.rb +++ b/spec/lib/use_case/data_export/answers_spec.rb @@ -2,8 +2,6 @@ require "rails_helper" -require "use_case/data_export/answers" - describe UseCase::DataExport::Answers, :data_export do include ActiveSupport::Testing::TimeHelpers diff --git a/spec/lib/use_case/data_export/appendables_spec.rb b/spec/lib/use_case/data_export/appendables_spec.rb index 60523f45..e57b3dbf 100644 --- a/spec/lib/use_case/data_export/appendables_spec.rb +++ b/spec/lib/use_case/data_export/appendables_spec.rb @@ -2,8 +2,6 @@ require "rails_helper" -require "use_case/data_export/appendables" - describe UseCase::DataExport::Appendables, :data_export do include ActiveSupport::Testing::TimeHelpers diff --git a/spec/lib/use_case/data_export/comments_spec.rb b/spec/lib/use_case/data_export/comments_spec.rb index 162d1798..35f57470 100644 --- a/spec/lib/use_case/data_export/comments_spec.rb +++ b/spec/lib/use_case/data_export/comments_spec.rb @@ -2,8 +2,6 @@ require "rails_helper" -require "use_case/data_export/comments" - describe UseCase::DataExport::Comments, :data_export do include ActiveSupport::Testing::TimeHelpers diff --git a/spec/lib/use_case/data_export/mute_rules_spec.rb b/spec/lib/use_case/data_export/mute_rules_spec.rb index 9e610b07..9e67b3fa 100644 --- a/spec/lib/use_case/data_export/mute_rules_spec.rb +++ b/spec/lib/use_case/data_export/mute_rules_spec.rb @@ -2,8 +2,6 @@ require "rails_helper" -require "use_case/data_export/mute_rules" - describe UseCase::DataExport::MuteRules, :data_export do include ActiveSupport::Testing::TimeHelpers diff --git a/spec/lib/use_case/data_export/questions_spec.rb b/spec/lib/use_case/data_export/questions_spec.rb index dfb7b78f..671cfe91 100644 --- a/spec/lib/use_case/data_export/questions_spec.rb +++ b/spec/lib/use_case/data_export/questions_spec.rb @@ -2,8 +2,6 @@ require "rails_helper" -require "use_case/data_export/questions" - describe UseCase::DataExport::Questions, :data_export do include ActiveSupport::Testing::TimeHelpers diff --git a/spec/lib/use_case/data_export/relationships_spec.rb b/spec/lib/use_case/data_export/relationships_spec.rb index 7af992f9..458f7777 100644 --- a/spec/lib/use_case/data_export/relationships_spec.rb +++ b/spec/lib/use_case/data_export/relationships_spec.rb @@ -2,8 +2,6 @@ require "rails_helper" -require "use_case/data_export/relationships" - describe UseCase::DataExport::Relationships, :data_export do include ActiveSupport::Testing::TimeHelpers diff --git a/spec/lib/use_case/data_export/theme_spec.rb b/spec/lib/use_case/data_export/theme_spec.rb index 8a073d89..af997cc8 100644 --- a/spec/lib/use_case/data_export/theme_spec.rb +++ b/spec/lib/use_case/data_export/theme_spec.rb @@ -2,8 +2,6 @@ require "rails_helper" -require "use_case/data_export/theme" - describe UseCase::DataExport::Theme, :data_export do include ActiveSupport::Testing::TimeHelpers diff --git a/spec/lib/use_case/data_export/user_spec.rb b/spec/lib/use_case/data_export/user_spec.rb index d769605b..a8c692ea 100644 --- a/spec/lib/use_case/data_export/user_spec.rb +++ b/spec/lib/use_case/data_export/user_spec.rb @@ -2,8 +2,6 @@ require "rails_helper" -require "use_case/data_export/user" - describe UseCase::DataExport::User, :data_export do let(:user_params) do { diff --git a/spec/lib/use_case/question/create_followers_spec.rb b/spec/lib/use_case/question/create_followers_spec.rb index 2b310427..ea958d07 100644 --- a/spec/lib/use_case/question/create_followers_spec.rb +++ b/spec/lib/use_case/question/create_followers_spec.rb @@ -2,7 +2,6 @@ require "rails_helper" require "errors" -require "use_case/question/create_followers" describe UseCase::Question::CreateFollowers do subject do diff --git a/spec/lib/use_case/question/create_spec.rb b/spec/lib/use_case/question/create_spec.rb index 28a97ffe..7126d8eb 100644 --- a/spec/lib/use_case/question/create_spec.rb +++ b/spec/lib/use_case/question/create_spec.rb @@ -2,7 +2,6 @@ require "rails_helper" require "errors" -require "use_case/question/create" describe UseCase::Question::Create do subject do diff --git a/spec/lib/use_case/question/destroy_spec.rb b/spec/lib/use_case/question/destroy_spec.rb index a2e3475c..6d709d7f 100644 --- a/spec/lib/use_case/question/destroy_spec.rb +++ b/spec/lib/use_case/question/destroy_spec.rb @@ -2,7 +2,6 @@ require "rails_helper" require "errors" -require "use_case/question/destroy" describe UseCase::Question::Destroy do subject do diff --git a/spec/lib/use_case/relationship/create_spec.rb b/spec/lib/use_case/relationship/create_spec.rb index a0656cd0..9a3b3a28 100644 --- a/spec/lib/use_case/relationship/create_spec.rb +++ b/spec/lib/use_case/relationship/create_spec.rb @@ -2,7 +2,6 @@ require "rails_helper" -require "use_case/relationship/create" require "errors" describe UseCase::Relationship::Create do diff --git a/spec/lib/use_case/relationship/destroy_spec.rb b/spec/lib/use_case/relationship/destroy_spec.rb index 1080b76a..b59fb8ae 100644 --- a/spec/lib/use_case/relationship/destroy_spec.rb +++ b/spec/lib/use_case/relationship/destroy_spec.rb @@ -2,7 +2,6 @@ require "rails_helper" -require "use_case/relationship/destroy" require "errors" describe UseCase::Relationship::Destroy do diff --git a/spec/support/example_exporter.rb b/spec/support/example_exporter.rb index 66508eab..925a1ae7 100644 --- a/spec/support/example_exporter.rb +++ b/spec/support/example_exporter.rb @@ -2,8 +2,6 @@ raise ArgumentError.new("This file should only be required in the 'test' environment! Current environment: #{Rails.env}") unless Rails.env.test? -require "use_case/data_export/base" - # an example exporter to be used for the tests of `Exporter` # # this only returning basic files, nothing user-specific. each exporter should be tested individually.