prefixed_ids 1.3.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: e306f564334470e61f4181b498ace781d12d7d06d1763dfa72a1b8a5f464ecaf
4
- data.tar.gz: 79118c8868ffd9244eccc1929aef505c73d79d85df8de9e1a3a07b07225e4c1a
3
+ metadata.gz: 31ec2b1bba4416445bc96d1009fb7d982a04ad2283e26450126ce526f1fceb44
4
+ data.tar.gz: ad94e3f32b9748ababe2cfe8b6e91e4b3a8ecf6e28e89dab4405bea461db5c47
5
5
  SHA512:
6
- metadata.gz: 66299e3e87f3495ddc7cb973da3cfa60104a6b1d0418f1a9c66e5ae9a2c0311689fa40aa0376e239ac8489a034a2b9fcb503e5aa11def29d818660615fbed958
7
- data.tar.gz: 13f65f23972ca2a38038f00007802ce4d65b163e15b990b15d4b1fdb8f9421d032167722bf181810cfaa281527b321cd4fec0317855f68ca8726aad8461e3244
6
+ metadata.gz: 6f281373868530a41f7cadf8fd78179cad3a1be85941a1734f3eef2fb568aef6b80702b9506bb2873989ee9b3fd2137839d4f79d328e4c0f4d92394b370c508f
7
+ data.tar.gz: b3d9c9fe54b7df878d122be69f591eb4ef58969804ac3af27705a5f7dc2b56a37a73ad77345b823a5d58bbd3ddbb2ef746d7802a074fa6fd2cfe1740163b4e09
data/README.md CHANGED
@@ -13,6 +13,8 @@ user_12345abcd
13
13
  acct_23lksjdg3
14
14
  ```
15
15
 
16
+ This gem works by hashing the record's original `:id` attribute using [`Hashids`](https://hashids.org/ruby/), which transforms numbers like 347 into a string like yr8. It uses the table's name and an optional additional salt to hash values, returning a string like `tablename_hashedvalue`.
17
+
16
18
  Inspired by [Stripe's prefixed IDs](https://stripe.com/docs/api) in their API.
17
19
 
18
20
  ## 🚀 Installation
@@ -50,13 +52,16 @@ User.prefix_id
50
52
 
51
53
  ##### Query by Prefixed ID
52
54
 
53
- To query using the prefixed ID, you can use either `find` or `find_by_prefix_id`:
55
+ To query using the prefixed ID, you can use either `find`, `find_by_prefix_id`, or `find_by_prefix_id!`:
54
56
 
55
57
  ```ruby
56
58
  User.find("user_5vJjbzXq9KrLEMm32iAnOP0xGDYk6dpe")
57
59
  User.find_by_prefix_id("user_5vJjbzXq9KrLEMm32iAnOP0xGDYk6dpe")
58
60
  ```
59
61
 
62
+ ⚠️ Note that `find` still finds records by the primary key. Eg. `localhost/users/1` still works.
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
+
60
65
  We also override `to_param` by default so it'll be used in URLs automatically.
61
66
 
62
67
  To disable find and to_param overrides, simply pass in the options:
@@ -75,7 +80,7 @@ A salt is a secret value that makes it impossible to reverse engineer IDs. We re
75
80
 
76
81
  ```ruby
77
82
  # config/initializers/prefixed_ids.rb
78
- PrefixID.salt = "salt"
83
+ PrefixedIds.salt = "salt"
79
84
  ```
80
85
 
81
86
  ###### Per Model Salt
@@ -103,10 +108,12 @@ You can customize the prefix, length, and attribute name for PrefixedIds.
103
108
 
104
109
  ```ruby
105
110
  class Account < ApplicationRecord
106
- has_prefix_id :acct, minimum_length: 32, override_find: false, to_param: false, salt: ""
111
+ has_prefix_id :acct, minimum_length: 32, override_find: false, override_param: false, salt: "", fallback: false
107
112
  end
108
113
  ```
109
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
+
110
117
  ## Development
111
118
 
112
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.3.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
@@ -61,10 +63,18 @@ module PrefixedIds
61
63
  def find_by_prefix_id!(id)
62
64
  find_by!(id: _prefix_id.decode(id))
63
65
  end
66
+
67
+ def decode_prefix_id(id)
68
+ _prefix_id.decode(id)
69
+ end
70
+
71
+ def decode_prefix_ids(ids)
72
+ ids.map { |id| decode_prefix_id(id) }
73
+ end
64
74
  end
65
75
 
66
76
  def prefix_id
67
- self.class._prefix_id.encode(id)
77
+ _prefix_id.encode(id)
68
78
  end
69
79
  end
70
80
 
@@ -73,7 +83,12 @@ module PrefixedIds
73
83
 
74
84
  class_methods do
75
85
  def find(*ids)
76
- 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)
77
92
  end
78
93
 
79
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.3.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: 2022-10-25 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.3.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