Skip to content

Commit

Permalink
Remove exists? override to simplify. Closes excid3#75
Browse files Browse the repository at this point in the history
  • Loading branch information
excid3 committed Oct 14, 2024
1 parent e04051a commit 33c7982
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 33 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
### Unreleased

* [Breaking] Remove `exists?` override.

To continue using `exists?`, you can decode the ID first:

```ruby
User.exists? User.decode_prefix_id(params[:id])
```

### 1.8.1

* Ensure that decode returns all parts of composite key
Expand Down
17 changes: 1 addition & 16 deletions lib/prefixed_ids.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,10 @@ module Rails
end

class_methods do
def has_prefix_id(prefix, override_find: true, override_param: true, override_exists: true, fallback: true, **options)
def has_prefix_id(prefix, override_find: true, override_param: true, fallback: true, **options)
include Attribute
include Finder if override_find
include ToParam if override_param
include Exists if override_exists
self._prefix_id = PrefixId.new(self, prefix, **options)
self._prefix_id_fallback = fallback

Expand Down Expand Up @@ -124,18 +123,4 @@ def to_param
_prefix_id.encode(id)
end
end

module Exists
extend ActiveSupport::Concern

class_methods do
def exists?(id)
if _prefix_id.present? && id.is_a?(String)
super(_prefix_id.decode(id))
else
super
end
end
end
end
end
17 changes: 0 additions & 17 deletions test/prefixed_ids_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -191,23 +191,6 @@ class PrefixedIdsTest < ActiveSupport::TestCase
assert_equal account, account.user.accounts.find(account.prefix_id)
end

test "exists? works with prefixed ID" do
account = accounts(:one)
assert Account.exists?(account.prefix_id)
end

test "disabled exists? with prefixed ID" do
post = posts(:one)

refute Post.exists?(post.prefix_id)
end

test "exists? works with conditions instead of ID" do
account = accounts(:one)

assert Account.exists?(id: account.id)
end

test "calling find on an associated model without prefix id succeeds" do
nonprefixed_item = nonprefixed_items(:one)
user = users(:one)
Expand Down

0 comments on commit 33c7982

Please sign in to comment.