Skip to content

Commit

Permalink
Loosen the dependency on ActiveRecord
Browse files Browse the repository at this point in the history
Remove the "activerecord", previously enforced only to have the
ActiveRecord::RecordNotFound constant

Replace with duck typing.
  • Loading branch information
pch committed Dec 7, 2023
1 parent cf407bd commit 3d1795d
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 16 deletions.
4 changes: 4 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@ Layout/LineLength:

Naming/PredicateName:
Enabled: false

# https://github.com/rubocop/rubocop/issues/7298
Lint/UselessAccessModifier:
Enabled: false
8 changes: 0 additions & 8 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion based_uuid.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 12 additions & 6 deletions lib/based_uuid/has_based_uuid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
8 changes: 7 additions & 1 deletion test/test_has_based_uuid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
class FakeActiveRecordBase < OpenStruct
include BasedUUID::HasBasedUUID

class RecordNotFound < StandardError; end

class_attribute :primary_key
self.primary_key = "id"

Expand All @@ -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
Expand Down Expand Up @@ -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)}") }
Expand Down

0 comments on commit 3d1795d

Please sign in to comment.