From 2eb359eacee0ba0e4820376c852b605301ea3b77 Mon Sep 17 00:00:00 2001 From: Kim Burgestrand Date: Wed, 9 Oct 2024 11:24:24 +0200 Subject: [PATCH] Require Active Support as is intended > In order to have the smallest default footprint possible, Active Support loads the minimum dependencies by default. It is broken in small pieces so that only the desired extensions can be loaded. https://guides.rubyonrails.org/active_support_core_extensions.html#stand-alone-active-support This was changed in Rails 3, released 2010-08-29. - We're not using anything from `core_ext/module/introspection` - We're not using blank or present from `core_ext/object/blank` - We're not using `autoload` --- CHANGELOG.md | 1 + lib/pundit.rb | 7 ++----- lib/pundit/policy_finder.rb | 3 +++ lib/pundit/rspec.rb | 3 +++ pundit.gemspec | 7 +++---- spec/authorization_spec.rb | 5 +++-- spec/spec_helper.rb | 7 ------- spec/support/models/customer/post.rb | 4 +--- 8 files changed, 16 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d83e138e..31eac5fc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ## Changed - Deprecated `Pundit::SUFFIX`, moved it to `Pundit::PolicyFinder::SUFFIX` (#835) +- Explicitly require less of `active_support` (#837) ## 2.4.0 (2024-08-26) diff --git a/lib/pundit.rb b/lib/pundit.rb index 4f143086..fc9ffb7e 100644 --- a/lib/pundit.rb +++ b/lib/pundit.rb @@ -1,12 +1,9 @@ # frozen_string_literal: true +require "active_support" + require "pundit/version" require "pundit/policy_finder" -require "active_support/concern" -require "active_support/core_ext/string/inflections" -require "active_support/core_ext/object/blank" -require "active_support/core_ext/module/introspection" -require "active_support/dependencies/autoload" require "pundit/authorization" require "pundit/context" require "pundit/cache_store/null_store" diff --git a/lib/pundit/policy_finder.rb b/lib/pundit/policy_finder.rb index b63e7276..e1884dd5 100644 --- a/lib/pundit/policy_finder.rb +++ b/lib/pundit/policy_finder.rb @@ -1,5 +1,8 @@ # frozen_string_literal: true +# String#safe_constantize, String#demodulize, String#underscore, String#camelize +require "active_support/core_ext/string/inflections" + module Pundit # Finds policy and scope classes for given object. # @api public diff --git a/lib/pundit/rspec.rb b/lib/pundit/rspec.rb index fb44ba60..cafc5913 100644 --- a/lib/pundit/rspec.rb +++ b/lib/pundit/rspec.rb @@ -1,5 +1,8 @@ # frozen_string_literal: true +# Array#to_sentence +require "active_support/core_ext/array/conversions" + module Pundit module RSpec module Matchers diff --git a/pundit.gemspec b/pundit.gemspec index 18a97bf7..a5b54b0f 100644 --- a/pundit.gemspec +++ b/pundit.gemspec @@ -22,11 +22,10 @@ Gem::Specification.new do |gem| gem.metadata = { "rubygems_mfa_required" => "true" } gem.add_dependency "activesupport", ">= 3.0.0" - gem.add_development_dependency "actionpack", ">= 3.0.0" - gem.add_development_dependency "activemodel", ">= 3.0.0" + gem.add_development_dependency "actionpack", ">= 3.0.0" # Used to test strong parameters. + gem.add_development_dependency "activemodel", ">= 3.0.0" # Used to test ActiveModel::Naming. gem.add_development_dependency "bundler" - gem.add_development_dependency "pry" - gem.add_development_dependency "railties", ">= 3.0.0" + gem.add_development_dependency "railties", ">= 3.0.0" # Used to test generators. gem.add_development_dependency "rake" gem.add_development_dependency "rspec", ">= 3.0.0" gem.add_development_dependency "rubocop" diff --git a/spec/authorization_spec.rb b/spec/authorization_spec.rb index 8bfa3fcb..ca291f9a 100644 --- a/spec/authorization_spec.rb +++ b/spec/authorization_spec.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require "spec_helper" +require "action_controller/metal/strong_parameters" describe Pundit::Authorization do def to_params(*args, **kwargs, &block) @@ -157,7 +158,7 @@ def to_params(*args, **kwargs, &block) end it "allows policy to be injected" do - new_policy = OpenStruct.new + new_policy = double controller.policies[post] = new_policy expect(controller.policy(post)).to eq new_policy @@ -182,7 +183,7 @@ def to_params(*args, **kwargs, &block) end it "allows policy_scope to be injected" do - new_scope = OpenStruct.new + new_scope = double controller.policy_scopes[Post] = new_scope expect(controller.policy_scope(Post)).to eq new_scope diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 6d63c514..4b932571 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -20,14 +20,7 @@ require "pundit" require "pundit/rspec" - -require "rack" -require "rack/test" -require "pry" -require "active_support" -require "active_support/core_ext" require "active_model/naming" -require "action_controller/metal/strong_parameters" # Load all supporting files: models, policies, etc. require "zeitwerk" diff --git a/spec/support/models/customer/post.rb b/spec/support/models/customer/post.rb index 0b08c654..3562e127 100644 --- a/spec/support/models/customer/post.rb +++ b/spec/support/models/customer/post.rb @@ -2,9 +2,7 @@ module Customer class Post < ::Post - def model_name - OpenStruct.new(param_key: "customer_post") - end + extend ActiveModel::Naming def self.policy_class PostPolicy