From 3d1795db96f99b6f826dfb0a8f0ad727ddbd5c36 Mon Sep 17 00:00:00 2001 From: Piotr Chmolowski Date: Thu, 7 Dec 2023 09:27:36 +0100 Subject: [PATCH] Loosen the dependency on ActiveRecord Remove the "activerecord", previously enforced only to have the ActiveRecord::RecordNotFound constant Replace with duck typing. --- .rubocop.yml | 4 ++++ Gemfile.lock | 8 -------- based_uuid.gemspec | 1 - lib/based_uuid/has_based_uuid.rb | 18 ++++++++++++------ test/test_has_based_uuid.rb | 8 +++++++- 5 files changed, 23 insertions(+), 16 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 6930ac3..ad20857 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -23,3 +23,7 @@ Layout/LineLength: Naming/PredicateName: Enabled: false + +# https://github.com/rubocop/rubocop/issues/7298 +Lint/UselessAccessModifier: + Enabled: false diff --git a/Gemfile.lock b/Gemfile.lock index df6be06..3ca9802 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -2,18 +2,11 @@ PATH remote: . specs: based_uuid (0.6.1) - activerecord (>= 7.0) activesupport (>= 7.0) GEM remote: https://rubygems.org/ specs: - activemodel (7.1.2) - activesupport (= 7.1.2) - activerecord (7.1.2) - activemodel (= 7.1.2) - activesupport (= 7.1.2) - timeout (>= 0.4.0) activesupport (7.1.2) base64 bigdecimal @@ -61,7 +54,6 @@ GEM parser (>= 3.2.1.0) ruby-progressbar (1.13.0) ruby2_keywords (0.0.5) - timeout (0.4.1) tzinfo (2.0.6) concurrent-ruby (~> 1.0) unicode-display_width (2.5.0) diff --git a/based_uuid.gemspec b/based_uuid.gemspec index 4b565e6..341e5cb 100644 --- a/based_uuid.gemspec +++ b/based_uuid.gemspec @@ -19,6 +19,5 @@ Gem::Specification.new do |spec| end spec.require_paths = ["lib"] - spec.add_dependency "activerecord", ">= 7.0" spec.add_dependency "activesupport", ">= 7.0" end diff --git a/lib/based_uuid/has_based_uuid.rb b/lib/based_uuid/has_based_uuid.rb index afbe66d..3e0c0f7 100644 --- a/lib/based_uuid/has_based_uuid.rb +++ b/lib/based_uuid/has_based_uuid.rb @@ -2,7 +2,6 @@ require "active_support/concern" require "active_support/core_ext/object/blank" require "active_support/core_ext/class/attribute" -require "active_record" module BasedUUID module HasBasedUUID @@ -30,14 +29,21 @@ module ModelExtensions class_methods do def find_by_based_uuid(token) - prefix, uuid_base32 = BasedUUID.split(token) - raise ArgumentError, "Invalid prefix" if prefix && prefix.to_sym != _based_uuid_prefix - - find_by(_based_uuid_column => Base32UUID.decode(uuid_base32)) + decode_and_find_based_uuid(token:) end def find_by_based_uuid!(token) - find_by_based_uuid(token) or raise ActiveRecord::RecordNotFound + decode_and_find_based_uuid(token:, raise_error: true) + end + + private + + def decode_and_find_based_uuid(token:, raise_error: false) + prefix, uuid_base32 = BasedUUID.split(token) + raise ArgumentError, "Invalid prefix" if prefix && prefix.to_sym != _based_uuid_prefix + + method_name = raise_error ? :find_by! : :find_by + send(method_name, _based_uuid_column => Base32UUID.decode(uuid_base32)) end end diff --git a/test/test_has_based_uuid.rb b/test/test_has_based_uuid.rb index 3c1c449..3db665f 100644 --- a/test/test_has_based_uuid.rb +++ b/test/test_has_based_uuid.rb @@ -4,6 +4,8 @@ class FakeActiveRecordBase < OpenStruct include BasedUUID::HasBasedUUID + class RecordNotFound < StandardError; end + class_attribute :primary_key self.primary_key = "id" @@ -18,6 +20,10 @@ def self.find_by(attrs) fake_datastore[attrs[primary_key]] end + def self.find_by!(attrs) + fake_datastore[attrs[primary_key]] or raise RecordNotFound + end + def [](key) send(key) end @@ -74,7 +80,7 @@ def test_based_uuid_for_custom_uuid_columns def test_find_by_based_uuid assert_nil User.find_by_based_uuid(random_user.based_uuid) - assert_raises(ActiveRecord::RecordNotFound) do + assert_raises(FakeActiveRecordBase::RecordNotFound) do User.find_by_based_uuid!(random_user.based_uuid) end assert_raises(ArgumentError) { User.find_by_based_uuid!("wrong_#{@user.based_uuid(prefix: false)}") }