prefixed_ids 1.6.1 → 1.7.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: afa295f257c683531956a2cb9d576c15a17daa215917e053552a2debe8c5645c
4
- data.tar.gz: 9e232d13a6675324217539ea4b2320f1a406ade6762aacef552f66d33d780f42
3
+ metadata.gz: 6a72c0361e99045f5e380aafa8e671be149ce14bdfb1bc83fb8520f72f4d63ac
4
+ data.tar.gz: fe98952521f2b62e4e1bbce4d66fe8a1c6d2074ce61e07cdbfaec2abfd07ac81
5
5
  SHA512:
6
- metadata.gz: 1a88c09cea33f417b70bb5022ddc6a64dcc0125b4ec13f657db0b99aab899c5d510cfe61afc5a72df559dd31152ae59771315686d3695189108ca11bb723c216
7
- data.tar.gz: 759d7c033de37807e08fef5f0c97731dfaddb8f8e0a718cfa8d78dea7fd8239955110d985065042e1a26d477bd18fb45bd9278e1f3e43e075db59c17860cc3fc
6
+ metadata.gz: 9386a5f418e0c547540e4589fde5633efe4aa61518f472d33f2e812de2140efe4d4cff0474153722e0dae4e6b5c6923a382a9883787ccc9fa5383573c672fd69
7
+ data.tar.gz: 011ab4e58529325474cbbb6b2f09d0c17daa6118b4ecb2866dad4b10997e1b64234f33e9d306bbec97e150c209617d00ef5a041b9c46f96440a35327082ab0e0
data/README.md CHANGED
@@ -104,6 +104,26 @@ PrefixedIds.find("acct_2iAnOP0xGDYk6dpe")
104
104
  #=> #<Account>
105
105
  ```
106
106
 
107
+ ### Exists
108
+
109
+ You can check if a record exists by its prefixed ID:
110
+
111
+ ```ruby
112
+ User.exists?("user_5vJjbzXq9KrLEMm3")
113
+ #=> true
114
+ ```
115
+
116
+ If given anything other than a prefixed ID, or `override_exists` is set to false, it will fall back to its original `exists?` method.
117
+
118
+ ```ruby
119
+ class User < ApplicationRecord
120
+ has_prefix_id :user, override_exists: false
121
+ end
122
+
123
+ User.exists?("user_5vJjbzXq9KrLEMm3")
124
+ #=> false
125
+ ```
126
+
107
127
  ### Customizing Prefix IDs
108
128
 
109
129
  You can customize the prefix, length, and attribute name for PrefixedIds.
@@ -11,6 +11,8 @@ module PrefixedIds
11
11
  end
12
12
 
13
13
  def encode(id)
14
+ return if id.nil?
15
+
14
16
  @prefix + @delimiter + @hashids.encode(TOKEN, id)
15
17
  end
16
18
 
@@ -1,3 +1,3 @@
1
1
  module PrefixedIds
2
- VERSION = "1.6.1"
2
+ VERSION = "1.7.1"
3
3
  end
data/lib/prefixed_ids.rb CHANGED
@@ -38,10 +38,11 @@ module PrefixedIds
38
38
  end
39
39
 
40
40
  class_methods do
41
- def has_prefix_id(prefix, override_find: true, override_param: true, fallback: true, **options)
41
+ def has_prefix_id(prefix, override_find: true, override_param: true, override_exists: true, fallback: true, **options)
42
42
  include Attribute
43
43
  include Finder if override_find
44
44
  include ToParam if override_param
45
+ include Exists if override_exists
45
46
  self._prefix_id = PrefixId.new(self, prefix, **options)
46
47
  self._prefix_id_fallback = fallback
47
48
 
@@ -123,4 +124,18 @@ module PrefixedIds
123
124
  _prefix_id.encode(id)
124
125
  end
125
126
  end
127
+
128
+ module Exists
129
+ extend ActiveSupport::Concern
130
+
131
+ class_methods do
132
+ def exists?(id)
133
+ if _prefix_id.present? && id.is_a?(String)
134
+ super(_prefix_id.decode(id))
135
+ else
136
+ super
137
+ end
138
+ end
139
+ end
140
+ end
126
141
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prefixed_ids
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.1
4
+ version: 1.7.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Oliver
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-11-17 00:00:00.000000000 Z
11
+ date: 2024-07-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -44,34 +44,6 @@ dependencies:
44
44
  - - "<"
45
45
  - !ruby/object:Gem::Version
46
46
  version: 2.0.0
47
- - !ruby/object:Gem::Dependency
48
- name: standard
49
- requirement: !ruby/object:Gem::Requirement
50
- requirements:
51
- - - ">="
52
- - !ruby/object:Gem::Version
53
- version: '0'
54
- type: :development
55
- prerelease: false
56
- version_requirements: !ruby/object:Gem::Requirement
57
- requirements:
58
- - - ">="
59
- - !ruby/object:Gem::Version
60
- version: '0'
61
- - !ruby/object:Gem::Dependency
62
- name: appraisal
63
- requirement: !ruby/object:Gem::Requirement
64
- requirements:
65
- - - ">="
66
- - !ruby/object:Gem::Version
67
- version: '0'
68
- type: :development
69
- prerelease: false
70
- version_requirements: !ruby/object:Gem::Requirement
71
- requirements:
72
- - - ">="
73
- - !ruby/object:Gem::Version
74
- version: '0'
75
47
  description: Prefixed IDs generates IDs with friendly prefixes for your models
76
48
  email:
77
49
  - excid3@gmail.com
@@ -108,7 +80,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
108
80
  - !ruby/object:Gem::Version
109
81
  version: '0'
110
82
  requirements: []
111
- rubygems_version: 3.4.22
83
+ rubygems_version: 3.5.13
112
84
  signing_key:
113
85
  specification_version: 4
114
86
  summary: Prefixed IDs generates IDs with friendly prefixes for your models