prefixed_ids 1.6.0 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 16d022537d510bcdcea78b0923d63c446e1b5fa85a5de1498f5206d6c8e1a935
4
- data.tar.gz: 9c4d0267908842cc93d498320628916a8ba966a72634730e6eeade8ab70ffa3d
3
+ metadata.gz: 89b486c2c314140be222d50337f74c65b7e3168397bd55c76a7248c70a81fa08
4
+ data.tar.gz: 568c49fbbf0ca7ce7a4137953cb12213ba898232a4e95451175065f311587b01
5
5
  SHA512:
6
- metadata.gz: 1c6ea5a2cb678238a2ee0102459d40c4d1c89755d69eddbca34e8c54b68ce455f08dd35c07b2c3fe381ddb4528d3913a0b50190bd45457baa18d102ee2c72a69
7
- data.tar.gz: 4743147c32367f459b0eced1fd7959ce36cfbc55061ae85e50a858ed0a46e0269bc25a0763dbed68d928af4ad84ccd8e3656c3c3759a0609b1627be5326a08b6
6
+ metadata.gz: 2b7cdc5be6e1f6923847ce68fb54f6cc095b7da0cd59ff909c7527fcdfb19b87c57ea8317d5809b63578ce72c3878c4df23db243c5364429e3aa3ca34ea05791
7
+ data.tar.gz: '0872d0c35e310df40ab3aed3c3cb07d3f29a3599f165085081786a9829c74832e529a50c7fe37ea3051fc805c6974ea0dfb7a0beb31e66793f7b9f2d0885c842'
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.
@@ -1,3 +1,3 @@
1
1
  module PrefixedIds
2
- VERSION = "1.6.0"
2
+ VERSION = "1.7.0"
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
 
@@ -91,14 +92,16 @@ module PrefixedIds
91
92
 
92
93
  class_methods do
93
94
  def find(*ids)
94
- prefix_ids = *ids.map do |id|
95
- # Skip if model doesn't use prefixed ids
96
- next id unless _prefix_id.present?
95
+ # Skip if model doesn't use prefixed ids
96
+ return super if _prefix_id.blank?
97
97
 
98
+ prefix_ids = ids.flatten.map do |id|
98
99
  prefix_id = _prefix_id.decode(id, fallback: _prefix_id_fallback)
99
100
  raise Error, "#{id} is not a valid prefix_id" if !_prefix_id_fallback && prefix_id.nil?
100
101
  prefix_id
101
102
  end
103
+ prefix_ids = [prefix_ids] if ids.first.is_a?(Array)
104
+
102
105
  super(*prefix_ids)
103
106
  end
104
107
 
@@ -121,4 +124,18 @@ module PrefixedIds
121
124
  _prefix_id.encode(id)
122
125
  end
123
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
124
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.0
4
+ version: 1.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Oliver
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-05-24 00:00:00.000000000 Z
11
+ date: 2024-04-25 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
@@ -93,7 +65,7 @@ metadata:
93
65
  homepage_uri: https://github.com/excid3/prefixed_ids
94
66
  source_code_uri: https://github.com/excid3/prefixed_ids
95
67
  changelog_uri: https://github.com/excid3/prefixed_ids
96
- post_install_message:
68
+ post_install_message:
97
69
  rdoc_options: []
98
70
  require_paths:
99
71
  - lib
@@ -108,8 +80,8 @@ 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.13
112
- signing_key:
83
+ rubygems_version: 3.5.9
84
+ signing_key:
113
85
  specification_version: 4
114
86
  summary: Prefixed IDs generates IDs with friendly prefixes for your models
115
87
  test_files: []