From 6e3ace14b0968ee0da05fe3c706afa2ebf166322 Mon Sep 17 00:00:00 2001 From: Nicolas Rodriguez Date: Sun, 28 Jan 2024 02:55:56 +0100 Subject: [PATCH] Switch to zeitwerk to load gem --- draper.gemspec | 1 + lib/draper.rb | 8 +++++--- lib/draper/engine.rb | 12 +++++++++++- lib/draper/test/rspec_integration.rb | 14 ++++---------- lib/draper/view_context.rb | 4 ---- 5 files changed, 21 insertions(+), 18 deletions(-) diff --git a/draper.gemspec b/draper.gemspec index 75ff6f8..865ed49 100644 --- a/draper.gemspec +++ b/draper.gemspec @@ -20,6 +20,7 @@ Gem::Specification.new do |s| s.add_runtime_dependency "rails", ">= 6.1" s.add_runtime_dependency "request_store", ">= 1.0" + s.add_runtime_dependency "zeitwerk" s.add_development_dependency "appraisal" s.add_development_dependency "capybara" diff --git a/lib/draper.rb b/lib/draper.rb index 2e3eb2b..5cab7af 100644 --- a/lib/draper.rb +++ b/lib/draper.rb @@ -3,9 +3,11 @@ require "active_support/concern" require "request_store" -require "draper/view_context" -require "draper/decorator" -require "draper/version" +require 'zeitwerk' +loader = Zeitwerk::Loader.for_gem +loader.ignore("#{__dir__}/draper/rails") +loader.ignore("#{__dir__}/draper/test") +loader.setup module Draper require "draper/engine" if defined?(Rails) diff --git a/lib/draper/engine.rb b/lib/draper/engine.rb index bdd6a98..c30081a 100644 --- a/lib/draper/engine.rb +++ b/lib/draper/engine.rb @@ -17,7 +17,17 @@ class Engine < ::Rails::Engine end config.after_initialize do |_app| - require "draper/test/rspec_integration" if Rails.env.test? && defined?(RSpec) && RSpec.respond_to?(:configure) + if Rails.env.test? && defined?(RSpec) && RSpec.respond_to?(:configure) + require "draper/test/rspec_integration" + + RSpec.configure do |config| + config.include Draper::Test::RspecIntegration, file_path: %r{spec/decorators}, type: :decorator + + %i[decorator controller mailer].each do |type| + config.before(:each, type: type) { Draper::ViewContext.clear! } + end + end + end end rake_tasks do diff --git a/lib/draper/test/rspec_integration.rb b/lib/draper/test/rspec_integration.rb index 0147a69..5fb44e4 100644 --- a/lib/draper/test/rspec_integration.rb +++ b/lib/draper/test/rspec_integration.rb @@ -1,17 +1,11 @@ # frozen_string_literal: true module Draper - module DecoratorExampleGroup - extend ActiveSupport::Concern + module Test + module RspecIntegration + extend ActiveSupport::Concern - included { metadata[:type] = :decorator } - end - - RSpec.configure do |config| - config.include DecoratorExampleGroup, file_path: %r{spec/decorators}, type: :decorator - - %i[decorator controller mailer].each do |type| - config.before(:each, type: type) { Draper::ViewContext.clear! } + included { metadata[:type] = :decorator } end end end diff --git a/lib/draper/view_context.rb b/lib/draper/view_context.rb index a5ae5fb..c0d516c 100644 --- a/lib/draper/view_context.rb +++ b/lib/draper/view_context.rb @@ -1,9 +1,5 @@ # frozen_string_literal: true -require "draper/view_context/build_strategy" -require "draper/view_context/base_helper" -require "draper/view_context/controller_helper" - module Draper module ViewContext module_function