prefixed_ids 1.4.0 → 1.5.0

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: b5a5f802c0838ffb2538b43212061d569d0920356aed91e313865799c570d573
4
- data.tar.gz: 6488ec773ddb3f15dfb48a9bc07ce4d39262feabb89eb8d3a5168e5ffdf52017
3
+ metadata.gz: 31ec2b1bba4416445bc96d1009fb7d982a04ad2283e26450126ce526f1fceb44
4
+ data.tar.gz: ad94e3f32b9748ababe2cfe8b6e91e4b3a8ecf6e28e89dab4405bea461db5c47
5
5
  SHA512:
6
- metadata.gz: 8b62b64c158bd670e814fbe92b7306a046b9f4e7933f494e5312ddb44f7a068fe9baaa579a11ad45b635aed0ec1dc83bec5b1699471ad550ed5e846a9125e446
7
- data.tar.gz: a5233cee0e3fb432b1ff4e01bc60f6c33cc670df756bb09e315f26fd5d22c6b7325bc2fde0eb1369be21196d4b23e65850dadf10f0fc2ee927aa636b8067670b
6
+ metadata.gz: 6f281373868530a41f7cadf8fd78179cad3a1be85941a1734f3eef2fb568aef6b80702b9506bb2873989ee9b3fd2137839d4f79d328e4c0f4d92394b370c508f
7
+ data.tar.gz: b3d9c9fe54b7df878d122be69f591eb4ef58969804ac3af27705a5f7dc2b56a37a73ad77345b823a5d58bbd3ddbb2ef746d7802a074fa6fd2cfe1740163b4e09
data/README.md CHANGED
@@ -59,7 +59,7 @@ User.find("user_5vJjbzXq9KrLEMm32iAnOP0xGDYk6dpe")
59
59
  User.find_by_prefix_id("user_5vJjbzXq9KrLEMm32iAnOP0xGDYk6dpe")
60
60
  ```
61
61
 
62
- ⚠️ Note that `find` still finds records by the primary key. Eg. `localhost/users/1` still works.
62
+ ⚠️ Note that `find` still finds records by the primary key. Eg. `localhost/users/1` still works.
63
63
  If you're targeting security issues by masking the ID, make sure to use `find_by_prefix_id` and [add a salt](#salt).
64
64
 
65
65
  We also override `to_param` by default so it'll be used in URLs automatically.
@@ -108,10 +108,12 @@ You can customize the prefix, length, and attribute name for PrefixedIds.
108
108
 
109
109
  ```ruby
110
110
  class Account < ApplicationRecord
111
- has_prefix_id :acct, minimum_length: 32, override_find: false, override_param: false, salt: ""
111
+ has_prefix_id :acct, minimum_length: 32, override_find: false, override_param: false, salt: "", fallback: false
112
112
  end
113
113
  ```
114
114
 
115
+ By default, `find` will accept both Prefix IDs and regular IDs. Setting `fallback: false` will disable finding by regular IDs and will only allow Prefix IDs.
116
+
115
117
  ## Development
116
118
 
117
119
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -19,9 +19,11 @@ module PrefixedIds
19
19
  fallback_value = fallback ? id : nil
20
20
  _, id_without_prefix = PrefixedIds.split_id(id, @delimiter)
21
21
  decoded_hashid = @hashids.decode(id_without_prefix)
22
- return fallback_value unless valid?(decoded_hashid)
23
-
24
- decoded_hashid.last || fallback_value
22
+ if fallback && !valid?(decoded_hashid)
23
+ fallback_value
24
+ else
25
+ decoded_hashid.last
26
+ end
25
27
  end
26
28
 
27
29
  private
@@ -1,3 +1,3 @@
1
1
  module PrefixedIds
2
- VERSION = "1.4.0"
2
+ VERSION = "1.5.0"
3
3
  end
data/lib/prefixed_ids.rb CHANGED
@@ -34,14 +34,16 @@ module PrefixedIds
34
34
 
35
35
  included do
36
36
  class_attribute :_prefix_id
37
+ class_attribute :_prefix_id_fallback
37
38
  end
38
39
 
39
40
  class_methods do
40
- def has_prefix_id(prefix, override_find: true, override_param: true, **options)
41
+ def has_prefix_id(prefix, override_find: true, override_param: true, fallback: true, **options)
41
42
  include Attribute
42
43
  include Finder if override_find
43
44
  include ToParam if override_param
44
45
  self._prefix_id = PrefixId.new(self, prefix, **options)
46
+ self._prefix_id_fallback = fallback
45
47
 
46
48
  # Register with PrefixedIds to support PrefixedIds#find
47
49
  PrefixedIds.models[prefix.to_s] = self
@@ -72,7 +74,7 @@ module PrefixedIds
72
74
  end
73
75
 
74
76
  def prefix_id
75
- self.class._prefix_id.encode(id)
77
+ _prefix_id.encode(id)
76
78
  end
77
79
  end
78
80
 
@@ -81,7 +83,12 @@ module PrefixedIds
81
83
 
82
84
  class_methods do
83
85
  def find(*ids)
84
- super(*ids.map { |id| _prefix_id.decode(id, fallback: true) })
86
+ prefix_ids = *ids.map do |id|
87
+ prefix_id = _prefix_id.decode(id, fallback: _prefix_id_fallback)
88
+ raise Error, "#{id} is not a valid prefix_id" if !_prefix_id_fallback && prefix_id.nil?
89
+ prefix_id
90
+ end
91
+ super(*prefix_ids)
85
92
  end
86
93
 
87
94
  def relation
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.4.0
4
+ version: 1.5.0
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-03-03 00:00:00.000000000 Z
11
+ date: 2023-03-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -108,7 +108,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
108
108
  - !ruby/object:Gem::Version
109
109
  version: '0'
110
110
  requirements: []
111
- rubygems_version: 3.4.7
111
+ rubygems_version: 3.4.9
112
112
  signing_key:
113
113
  specification_version: 4
114
114
  summary: Prefixed IDs generates IDs with friendly prefixes for your models