sqinky 0.1.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 +7 -0
- data/CHANGELOG.md +7 -0
- data/CODE_OF_CONDUCT.md +10 -0
- data/LICENSE.md +21 -0
- data/README.md +228 -0
- data/Rakefile +10 -0
- data/lib/sqinky/identifier_encoding.rb +270 -0
- data/lib/sqinky/version.rb +5 -0
- data/lib/sqinky.rb +7 -0
- metadata +212 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 40416492e70faec554b8a67ff4c00c8d0ee9e4a0b2b76a885adffa0dbce64294
|
|
4
|
+
data.tar.gz: 1d538c90645378e38d3007b0d3473f07b264433cf95eabb826c392cc4099ef05
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: c55b2e64caf756a43f6336fe99135d5eeb4e7a10e664c87bbfb17143ab91ab1e75b8b2a1350061588172678997167da6219ae5c0e87ae1b5faf87679735f30c7
|
|
7
|
+
data.tar.gz: 7f83412fe303c907f842b1c510675b2d9efc00e2721fa3a2d85b9ac679d6d0488c30a95c52b42b1fb4f0a9882e7f170ae2320ae217596e39b64d9884e5ec6c94
|
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# Code of Conduct
|
|
2
|
+
|
|
3
|
+
"sqinky" follows [The Ruby Community Conduct Guideline](https://www.ruby-lang.org/en/conduct) in all "collaborative space", which is defined as community communications channels (such as mailing lists, submitted patches, commit comments, etc.):
|
|
4
|
+
|
|
5
|
+
* Participants will be tolerant of opposing views.
|
|
6
|
+
* Participants must ensure that their language and actions are free of personal attacks and disparaging personal remarks.
|
|
7
|
+
* When interpreting the words and actions of others, participants should always assume good intentions.
|
|
8
|
+
* Behaviour which can be reasonably considered harassment will not be tolerated.
|
|
9
|
+
|
|
10
|
+
If you have any concerns about behaviour within this project, please contact us at ["david.uhlig@gmail.com"](mailto:"david.uhlig@gmail.com").
|
data/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 David Uhlig
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
[Sqids]: https://sqids.org/
|
|
2
|
+
|
|
3
|
+
# 🫟 Sqinky
|
|
4
|
+
|
|
5
|
+
## 🦑 [Sqids] for your Active Record models.
|
|
6
|
+
|
|
7
|
+
[](https://github.com/david-uhlig/sqinky/blob/main/LICENSE.md) [](https://badge.fury.io/rb/sqinky) [](https://github.com/david-uhlig/sqinky/actions/workflows/main.yml)
|
|
8
|
+
|
|
9
|
+
> **What is Sqids?**
|
|
10
|
+
>
|
|
11
|
+
> Sqids (pronounced "squids") is an open-source library that lets you generate short unique identifiers from numbers. These IDs are URL-safe, can encode several numbers, and do not contain common profanity words.
|
|
12
|
+
>
|
|
13
|
+
> This is what they look like: `https://example.com/CN4Xst`
|
|
14
|
+
>
|
|
15
|
+
> Source: https://sqids.org/
|
|
16
|
+
|
|
17
|
+
Sqinky brings Sqids-based identifier encoding[^1] and decoding directly to your Active Record models.
|
|
18
|
+
|
|
19
|
+
The library adds a thin access layer on top of [Sqids](https://sqids.org/) to work effortlessly with short, unique, and URL-safe identifiers. It supports encodings composed of multiple attributes and multiple encodings per model. Sqinky does not store encodings in the database. Instead, it decodes them on the fly back to the initial arguments and uses standard Active Record methods to retrieve the records.
|
|
20
|
+
|
|
21
|
+
#### Key features
|
|
22
|
+
- **Dynamic Encodings**: Computed from record attributes on the fly. No extra data is stored in the database.
|
|
23
|
+
- **Auto-generated Helpers**: Provides `find_by_*`, `find_by_*!`, `destroy_by_*`, and `delete_by_*` methods.
|
|
24
|
+
- **Multiple Attributes**: Encode and decode composite identifiers (e.g., `[account_id, id]`).
|
|
25
|
+
- **Customizable**: Pass any Sqids option (`alphabet`, `min_length`, and `blocklist`) per encoder.
|
|
26
|
+
|
|
27
|
+
## Installation
|
|
28
|
+
|
|
29
|
+
Run the following command to add Sqinky to your Gemfile:
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
bundle add sqinky
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Usage
|
|
36
|
+
|
|
37
|
+
To use it include `Sqinky::IdentifierEncoding` in your Active Record model and
|
|
38
|
+
* add the `encodes_identifier` macro for a single attribute (defaults to `:id`), or
|
|
39
|
+
* add the `encodes_identifiers` macro for multiple attributes.
|
|
40
|
+
* See [Parameter Overview](#parameter-overview) for all configuration options.
|
|
41
|
+
|
|
42
|
+
This generates the encoding methods `<attribute>_encoding` and `<attribute>_encoding!` (by default; configurable), and the dynamic Active Record methods `find_by_*`, `find_by_*!`, `delete_by_*`, and `destroy_by_*`. See [Generated Method Overview](#generated-methods-overview) for a detailed overview.
|
|
43
|
+
|
|
44
|
+
#### Basic usage
|
|
45
|
+
```ruby
|
|
46
|
+
class Comment < ApplicationRecord
|
|
47
|
+
include Sqinky::IdentifierEncoding
|
|
48
|
+
|
|
49
|
+
encodes_identifier :id
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# Retrieving the encoding
|
|
53
|
+
comment = Comment.create!(text: "Sqinky is the linky that breaks when you drinky.")
|
|
54
|
+
comment.id # => 1
|
|
55
|
+
comment.id_encoding # => "Uk"
|
|
56
|
+
|
|
57
|
+
# Finding the comment back
|
|
58
|
+
Comment.find_by_id_encoding("Uk") # => #<Comment id: 1, ...>
|
|
59
|
+
Comment.find_by_id_encoding!("Uk") # Raises ActiveRecord::RecordNotFound if not found
|
|
60
|
+
Comment.destroy_by_id_encoding("Uk")
|
|
61
|
+
Comment.delete_by_id_encoding("Uk")
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
> [!WARNING]
|
|
65
|
+
> For consistent behavior only use attributes with `Integer` values `>= 0`.
|
|
66
|
+
|
|
67
|
+
> [!NOTE]
|
|
68
|
+
> `id_encoding` in all these methods is dynamically generated from the `id` attribute.
|
|
69
|
+
> * If you encode a different attribute, e.g. `user_id` the method names change to `user_id_encoding`.
|
|
70
|
+
> * Composite encodings are connected with `_and_` in the specified order: `id_and_user_id_encoding`.
|
|
71
|
+
> * You can choose your own name with the `as:` parameter: `as: :token # => instance.token`.
|
|
72
|
+
|
|
73
|
+
#### Most common workflow
|
|
74
|
+
1. Receive encoding from a **persisted** record: `user.id_encoding # => "Uk"`.
|
|
75
|
+
2. Pass the encoding around, either via URL `/users/Uk/show` or store it in a session.
|
|
76
|
+
3. Retrieve the record through the encoding `User.find_by_id_encoding("Uk") # => #<User id: 1, ...>`
|
|
77
|
+
|
|
78
|
+
### Multiple Attributes & Custom Names
|
|
79
|
+
|
|
80
|
+
Sqinky can compose multiple attributes into a single encoding, here: `:id` and `:account_id`. Normally, this would dynamically generate `id_and_account_id_encoding` methods, but here we rename them to `token` with the `as:` parameter.
|
|
81
|
+
|
|
82
|
+
```ruby
|
|
83
|
+
class Invitation < ApplicationRecord
|
|
84
|
+
include Sqinky::IdentifierEncoding
|
|
85
|
+
|
|
86
|
+
# Encode both `id` and `account_id` into a single `token`
|
|
87
|
+
encodes_identifiers :id, :account_id, as: :token
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
invitation = Invitation.create!(account_id: 42)
|
|
91
|
+
invitation.token # => "ySrS"
|
|
92
|
+
|
|
93
|
+
# Internally calls find_by(id: 1, account_id: 42)
|
|
94
|
+
Invitation.find_by_token("ySrS")
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
> [!NOTE]
|
|
98
|
+
> Mind the pluralized `encodes_identifiers` when encoding multiple attributes.
|
|
99
|
+
|
|
100
|
+
### Decoding Helper
|
|
101
|
+
|
|
102
|
+
If you need to access the decoded hash, you can pass your preferred method name to the `decodes_as:` parameter, e.g. `decodes_as: :id_decoding`. This will generate a dynamic class method, that takes a single argument: the encoding.
|
|
103
|
+
|
|
104
|
+
```ruby
|
|
105
|
+
class Order < ApplicationRecord
|
|
106
|
+
include Sqinky::IdentifierEncoding
|
|
107
|
+
|
|
108
|
+
encodes_identifier :id, as: :public_id, decodes_as: :decode_public_id
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
Order.decode_public_id("86Rf07") # => { id: 1 }
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
### Custom Sqids Options
|
|
115
|
+
|
|
116
|
+
Configure Sqids by passing in the [Sqids options](https://github.com/sqids/sqids-ruby?tab=readme-ov-file#%E2%80%8D-examples): `alphabet`, `min_length`, and `blocklist`. Each coder can have its own configuration.
|
|
117
|
+
|
|
118
|
+
```ruby
|
|
119
|
+
class Comment < ApplicationRecord
|
|
120
|
+
include Sqinky::IdentifierEncoding
|
|
121
|
+
|
|
122
|
+
encodes_identifier :id, alphabet: "abcdef0123456789", min_length: 10
|
|
123
|
+
end
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
### Multiple Encodings
|
|
127
|
+
|
|
128
|
+
A model can have multiple encodings, even for the same attribute, as long as they have distinct `as:` method names.
|
|
129
|
+
|
|
130
|
+
```ruby
|
|
131
|
+
class Post < ApplicationRecord
|
|
132
|
+
include Sqinky::IdentifierEncoding
|
|
133
|
+
|
|
134
|
+
encodes_identifier
|
|
135
|
+
encodes_identifiers :id, :tenant_id
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
post = Post.create!(title: "How Sqinky became so inkie.")
|
|
139
|
+
post.id # => 212
|
|
140
|
+
post.tenant_id # => 42
|
|
141
|
+
post.id_encoding # => "37E"
|
|
142
|
+
post.id_and_tenant_id_encoding # "jGTwn"
|
|
143
|
+
|
|
144
|
+
Post.find_by_id_encoding("37E") # => #<Post id: 212, ...>
|
|
145
|
+
Post.find_by_id_and_tenant_id_encoding("jGTwn") # => #<Post id: 212, ...>
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
### Inheritance
|
|
149
|
+
|
|
150
|
+
Since Sqinky generates regular methods, it can be included anywhere in the class tree. Child classes can use the parents' methods or overwrite them with their own. It isn't required for the including class to have the configured attributes.
|
|
151
|
+
|
|
152
|
+
```ruby
|
|
153
|
+
class ApplicationRecord < ActiveRecord::Base
|
|
154
|
+
include Sqinky::IdentifierEncoding
|
|
155
|
+
|
|
156
|
+
encodes_identifier
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
class Label < ApplicationRecord
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
label = Label.create!(text: "Pinky")
|
|
163
|
+
label.id = 1
|
|
164
|
+
label.id_encoding # => "Uk"
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
### Parameter Overview
|
|
168
|
+
|
|
169
|
+
#### `encodes_identifier`
|
|
170
|
+
|
|
171
|
+
| Parameter | Default | Description |
|
|
172
|
+
|-------------------|---------|---------------------------------------------------------------------------------------------|
|
|
173
|
+
| `attribute` | `:id` | The attribute to encode. Should only have `Integer` `>= 0` values. |
|
|
174
|
+
| `as:` | `nil` | If `nil` inferred as `<attribute>_encoding`. |
|
|
175
|
+
| `decodes_as:` | `nil` | Name of the decoding class method. If `nil` no such method is generated. |
|
|
176
|
+
| `**sqids_options` | `{}` | Sqids options passed through to `Sqids.new`, e.g. `min_length`, `alphabet`, and `blocklist` |
|
|
177
|
+
|
|
178
|
+
#### `encodes_identifiers`
|
|
179
|
+
|
|
180
|
+
| Parameter | Default | Description |
|
|
181
|
+
|-------------------|---------|---------------------------------------------------------------------------------------------|
|
|
182
|
+
| `*attributes` | | The attribute(s) to encode. Should only have `Integer` `>= 0` values. |
|
|
183
|
+
| `as:` | `nil` | If `nil` inferred as `<attribute[_and_<attribute>]>_encoding`. |
|
|
184
|
+
| `decodes_as:` | `nil` | Name of the decoding class method. If `nil` no such method is generated. |
|
|
185
|
+
| `**sqids_options` | `{}` | Sqids options passed through to `Sqids.new`, e.g. `min_length`, `alphabet`, and `blocklist` |
|
|
186
|
+
|
|
187
|
+
### Generated Methods Overview
|
|
188
|
+
|
|
189
|
+
| Method | Description |
|
|
190
|
+
|-----------------------------------|----------------------------------------------------------------------------------------------------|
|
|
191
|
+
| `instance.<as>` | Returns the Sqids encoding for the configured attributes. Returns `nil` if any attribute is `nil`. |
|
|
192
|
+
| `instance.<as>!` | Same as above, but raises `ArgumentError` if any attribute is not an `Integer`. |
|
|
193
|
+
| `Class.<decodes_as>(encoding)` | Returns the decoded hash, e.g. `{ id: 42 }`. |
|
|
194
|
+
| `Class.find_by_<as>(encoding)` | Decodes `encoding` and passes the decoded hash to `find_by(...)` |
|
|
195
|
+
| `Class.find_by_<as>(encoding)!` | Decodes `encoding` and passes the decoded hash to `find_by(...)!` |
|
|
196
|
+
| `Class.destroy_by_<as>(encoding)` | Decodes `encoding` and passes the decoded hash to `destroy_by(...)` |
|
|
197
|
+
| `Class.delete_by_<as>(encoding)` | Decodes `encoding` and passes the decoded hash to `delete_by(...)` |
|
|
198
|
+
|
|
199
|
+
## Development
|
|
200
|
+
|
|
201
|
+
### Setup
|
|
202
|
+
|
|
203
|
+
After checking out the repo, run the setup script to install dependencies:
|
|
204
|
+
|
|
205
|
+
```bash
|
|
206
|
+
bin/setup
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
This project uses [mise](https://mise.jdx.dev/) for managing Ruby versions and tasks. If you have mise installed, you can use it to run common tasks.
|
|
210
|
+
|
|
211
|
+
### Scripts & Tasks
|
|
212
|
+
|
|
213
|
+
- `bin/console`: Open an interactive prompt to experiment with the code.
|
|
214
|
+
- `rake spec`: Run the test suite.
|
|
215
|
+
- `rake standard`: Run the StandardRB linter.
|
|
216
|
+
- `bundle exec appraisal install`:
|
|
217
|
+
- `bundle exec appraisal rake spec`: Run tests against all supported Rails versions.
|
|
218
|
+
- `mise run ci`: Run the local CI pipeline (linting and multi-Rails tests).
|
|
219
|
+
|
|
220
|
+
## Contributing
|
|
221
|
+
|
|
222
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/david-uhlig/sqinky. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/david-uhlig/sqinky/blob/main/CODE_OF_CONDUCT.md).
|
|
223
|
+
|
|
224
|
+
## License
|
|
225
|
+
|
|
226
|
+
The gem is available as open source under the terms of the [MIT License](LICENSE.md).
|
|
227
|
+
|
|
228
|
+
[^1]: In the context of this library the term `encoding` refers to the output of `Sqids.new.encode([<number>])`. In Sqids terminology this is a *short unique identifier from numbers*.
|
data/Rakefile
ADDED
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "active_support/concern"
|
|
4
|
+
require "sqids"
|
|
5
|
+
|
|
6
|
+
module Sqinky
|
|
7
|
+
# Add Sqids-based identifier encoding/decoding helpers to Active Record models.
|
|
8
|
+
#
|
|
9
|
+
# {Sqids}[https://sqids.org/] is an open-source library that lets you generate short unique identifiers from numbers.
|
|
10
|
+
# These IDs are URL-safe, can encode several numbers, and do not contain common profanity words.
|
|
11
|
+
#
|
|
12
|
+
# *Note*: Sqids encodings are computed dynamically from record attributes and do not need to be stored.
|
|
13
|
+
#
|
|
14
|
+
# Sqinky adds a thin access layer on top of Sqids to work effortlessly with Sqids in Active Record models.
|
|
15
|
+
# It generates an encoding method, e.g. +id_encoding+ and database methods for finding and deleting matching records,
|
|
16
|
+
# e.g. +find_by_id_encoding+, +find_by_id_encoding!+, +destroy_by_id_encoding+, and +delete_by_id_encoding+. Sqinky
|
|
17
|
+
# does not persist the encoding to the database. It supports encodings composed of multiple attributes, and multiple
|
|
18
|
+
# encodings per model.
|
|
19
|
+
module IdentifierEncoding
|
|
20
|
+
extend ActiveSupport::Concern
|
|
21
|
+
|
|
22
|
+
class_methods do
|
|
23
|
+
# Generates methods for creating and consuming a single identifier attribute encoding, typically for the primary
|
|
24
|
+
# key +id+.
|
|
25
|
+
#
|
|
26
|
+
# #### Note
|
|
27
|
+
# * If the +as+ argument is missing, it is generated as +<attribute>_encoding+.
|
|
28
|
+
# * The referenced +attribute+ must be present when the generated +#{as}+ method is called.
|
|
29
|
+
#
|
|
30
|
+
# #### Generates
|
|
31
|
+
# * +#<as>+ - Generates the Sqids encoding from the attribute value.
|
|
32
|
+
# * +#<as>!+ - Generates the Sqids encoding from the attribute value. Raises +ArgumentError+ the attribute value is noninteger.
|
|
33
|
+
# * +#<decodes_as>(encoding)+ - Decodes a Sqids encoding back to the attribute-value hash. (Optional)
|
|
34
|
+
# * +.find_by_<as>(encoding)+ - Finds record by +encoding+ or returns nil.
|
|
35
|
+
# * +.find_by_<as>!(encoding)+ - Finds record by +encoding+ or raises +ActiveRecord::RecordNotFound+ error.
|
|
36
|
+
# * +.destroy_by_<as>(encoding)+ - Destroys record by +encoding+.
|
|
37
|
+
# * +.delete_by_<as>(encoding)+ - Deletes record by +encoding+.
|
|
38
|
+
#
|
|
39
|
+
# @param attribute [Symbol] Attribute to encode. Must take positive +Integer+ values.
|
|
40
|
+
# @param as [Symbol, nil] Optional name of the instance method that returns the encoding. Also part of the database methods, e.g. +find_by_<as>+. If missing, it is generated from the attribute name, e.g. +id_encoding+.
|
|
41
|
+
# @param decodes_as [Symbol, nil] Optional class method name that, when given an encoding, returns a hash of decoded attribute values. If missing, no such method is generated.
|
|
42
|
+
# @param sqids_options [Hash] Options forwarded to +Sqids.new+, e.g. +alphabet+, +min_length+, and +blocklist+.
|
|
43
|
+
#
|
|
44
|
+
# @return [Void]
|
|
45
|
+
#
|
|
46
|
+
# @see .encodes_identifiers
|
|
47
|
+
def encodes_identifier(attribute = :id, as: nil, decodes_as: nil, **sqids_options)
|
|
48
|
+
encodes_identifiers(attribute, as: as, decodes_as: decodes_as, **sqids_options)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# Generates methods for creating and consuming a single or multiple identifier attribute encoding.
|
|
52
|
+
#
|
|
53
|
+
# #### Note
|
|
54
|
+
# * If the +as+ argument is missing, it is generated as +<attribute>_encoding+. If multiple attributes are present they are joined by +_and_+, e.g. +<attribute>_and_<attribute>_encoding+.
|
|
55
|
+
# * The referenced +attribute+ must be present when the generated +#{as}+ method is called.
|
|
56
|
+
#
|
|
57
|
+
# #### Generates
|
|
58
|
+
# * +#<as>+ - Generates the Sqids encoding from the attribute values.
|
|
59
|
+
# * +#<as>!+ - Generates the Sqids encoding from the attribute values. Raises +ArgumentError+ if any attribute value is noninteger.
|
|
60
|
+
# * +#<decodes_as>(encoding)+ - Decodes a Sqids encoding back to the attributes-values hash. (Optional)
|
|
61
|
+
# * +.find_by_<as>(encoding)+ - Finds record by +encoding+ or returns nil.
|
|
62
|
+
# * +.find_by_<as>!(encoding)+ - Finds record by +encoding+ or raises +ActiveRecord::RecordNotFound+ error.
|
|
63
|
+
# * +.destroy_by_<as>(encoding)+ - Destroys record by +encoding+.
|
|
64
|
+
# * +.delete_by_<as>(encoding)+ - Deletes record by +encoding+.
|
|
65
|
+
#
|
|
66
|
+
# ### Usage
|
|
67
|
+
#
|
|
68
|
+
# @example Basic usage for an `id` primary key
|
|
69
|
+
# class Post < ApplicationRecord
|
|
70
|
+
# include Sqinky::IdentifierEncoding
|
|
71
|
+
#
|
|
72
|
+
# # Encodes the `id` attribute into `id_encoding`
|
|
73
|
+
# encodes_identifier
|
|
74
|
+
# end
|
|
75
|
+
#
|
|
76
|
+
# post = Post.create!(title: "Hello")
|
|
77
|
+
# post.id # => 1
|
|
78
|
+
# post.id_encoding # => "Uk"
|
|
79
|
+
# Post.find_by_id_encoding("Uk") # => #<Post id: 1, ...>
|
|
80
|
+
# Post.find_by_id_encoding!("Uk") # => Same as above, raises if not found.
|
|
81
|
+
# Post.destroy_by_id_encoding("Uk")
|
|
82
|
+
# Post.delete_by_id_encoding("Uk")
|
|
83
|
+
#
|
|
84
|
+
# @example Encoding multiple attributes
|
|
85
|
+
# class Invitation < ApplicationRecord
|
|
86
|
+
# include Sqinky::IdentifierEncoding
|
|
87
|
+
#
|
|
88
|
+
# # Encode `account_id` and `id`
|
|
89
|
+
# encodes_identifiers :id, :account_id
|
|
90
|
+
# end
|
|
91
|
+
#
|
|
92
|
+
# invitation = Invitation.create!(account_id: 42)
|
|
93
|
+
# invitation.id # => 1
|
|
94
|
+
# invitation.id_and_account_id_encoding # => "ySrS"
|
|
95
|
+
# Invitation.find_by_id_and_account_id_encoding("ySrS")
|
|
96
|
+
# # => Internally calls find_by(id: 1, account_id: 42)
|
|
97
|
+
# # => #<Invitation id: 1, account_id: 42, ...>
|
|
98
|
+
# Invitation.find_by_id_and_account_id_encoding!("ySrS")
|
|
99
|
+
# Invitation.destroy_by_id_and_account_id_encoding("ySrS")
|
|
100
|
+
# Invitation.delete_by_id_and_account_id_encoding("ySrS")
|
|
101
|
+
#
|
|
102
|
+
# @example Renaming the helper methods
|
|
103
|
+
# class Invitation < ApplicationRecord
|
|
104
|
+
# include Sqinky::IdentifierEncoding
|
|
105
|
+
#
|
|
106
|
+
# # Encode `account_id` and `id` into `token`
|
|
107
|
+
# encodes_identifiers :id, :account_id, as: :token
|
|
108
|
+
# end
|
|
109
|
+
#
|
|
110
|
+
# invitation = Invitation.create!(account_id: 42)
|
|
111
|
+
# invitation.id # => 1
|
|
112
|
+
# invitation.token # => "ySrS"
|
|
113
|
+
# Invitation.find_by_token("ySrS")
|
|
114
|
+
# # => Internally calls find_by(id: 1, account_id: 42)
|
|
115
|
+
# # => #<Invitation id: 1, account_id: 42, ...>
|
|
116
|
+
# Invitation.find_by_token!("ySrS")
|
|
117
|
+
# Invitation.destroy_by_token("ySrS")
|
|
118
|
+
# Invitation.delete_by_token("ySrS")
|
|
119
|
+
#
|
|
120
|
+
# @example Generating a decoding helper
|
|
121
|
+
# class Order < ApplicationRecord
|
|
122
|
+
# include Sqinky::IdentifierEncoding
|
|
123
|
+
#
|
|
124
|
+
# # Add a decoding helper that returns the decoded attributes hash
|
|
125
|
+
# encodes_identifiers :shop_id, :id,
|
|
126
|
+
# as: :public_id,
|
|
127
|
+
# decodes_as: :decode_public_id
|
|
128
|
+
# end
|
|
129
|
+
#
|
|
130
|
+
# order = Order.create!(shop_id: 10)
|
|
131
|
+
# encoded = order.public_id # => e.g. "86Rf07"
|
|
132
|
+
# Order.decode_public_id(encoded)
|
|
133
|
+
# # => { shop_id: 10, id: 1 }
|
|
134
|
+
#
|
|
135
|
+
# @example Passing Sqids options through
|
|
136
|
+
# class Comment < ApplicationRecord
|
|
137
|
+
# include Sqinky::IdentifierEncoding
|
|
138
|
+
#
|
|
139
|
+
# # Use a custom alphabet and minimum length for generated IDs
|
|
140
|
+
# encodes_identifier :id,
|
|
141
|
+
# alphabet: "abc",
|
|
142
|
+
# min_length: 10
|
|
143
|
+
# end
|
|
144
|
+
#
|
|
145
|
+
# comment = Comment.create!
|
|
146
|
+
# comment.id_encoding # => Always at least 10 characters, consists only of "abc" characters.
|
|
147
|
+
#
|
|
148
|
+
# @example Multiple encoders in a single model
|
|
149
|
+
# class Membership < ApplicationRecord
|
|
150
|
+
# include Sqinky::IdentifierEncoding
|
|
151
|
+
#
|
|
152
|
+
# # Encodes +id+ into +id_encoding+.
|
|
153
|
+
# encode_identifier
|
|
154
|
+
# # Encodes +id+ (again) into +code+ with the +abc+ alphabet.
|
|
155
|
+
# encode_identifier as: :code, alphabet: "abc"
|
|
156
|
+
# # Encodes both +user_id+ and +group_id+ into a single token. Make sure to
|
|
157
|
+
# # use a different +as+ (and +decodes_as+) value for each encoder, otherwise they will overwrite
|
|
158
|
+
# # each other.
|
|
159
|
+
# encodes_identifiers :user_id, :group_id, as: :membership_token
|
|
160
|
+
# end
|
|
161
|
+
#
|
|
162
|
+
# membership = Membership.create!(user_id: 44, group_id: 12)
|
|
163
|
+
# token = membership.id_encoding
|
|
164
|
+
# # => "Uk"
|
|
165
|
+
# Membership.find_by_id_encoding(token)
|
|
166
|
+
# # => Internally calls `find_by(id: 1)`
|
|
167
|
+
#
|
|
168
|
+
# code = membership.code
|
|
169
|
+
# # => "aa"
|
|
170
|
+
# Membership.find_by_code(code)
|
|
171
|
+
# # => Internally calls `find_by(id: 1)`
|
|
172
|
+
#
|
|
173
|
+
# membership_token = membership.membership_token
|
|
174
|
+
# # => "7edZ"
|
|
175
|
+
# Membership.find_by_membership_token(membership_token)
|
|
176
|
+
# # => Internally calls `find_by(user_id: 44, group_id: 12)
|
|
177
|
+
#
|
|
178
|
+
# @return [Void]
|
|
179
|
+
#
|
|
180
|
+
# @param attributes [Array<Symbol>] List of attributes to encode. At least one attribute must be provided.
|
|
181
|
+
# @param as [Symbol, nil] Name of the instance method that returns the encoding. Also part of the database methods, e.g. +find_by_<as>+. If missing, it is generated from the attribute names, e.g. +id_encoding+ or +id_and_tenant_id_encoding+.
|
|
182
|
+
# @param decodes_as [Symbol, nil] Optional class method name that, when given an encoding, returns a hash of decoded attribute values. If missing, no such method is generated.
|
|
183
|
+
# @param sqids_options [Hash] Options forwarded to +Sqids.new+, e.g. +alphabet+, +min_length+, and +blocklist+.
|
|
184
|
+
#
|
|
185
|
+
# @raise [ArgumentError] if no attributes are given.
|
|
186
|
+
#
|
|
187
|
+
# @return [void]
|
|
188
|
+
def encodes_identifiers(*attributes, as: nil, decodes_as: nil, **sqids_options)
|
|
189
|
+
if attributes.compact_blank!.empty?
|
|
190
|
+
raise ArgumentError, <<~MSG
|
|
191
|
+
Must specify at least one attribute. Hint: Use `encodes_identifier` instead to encode the primary key
|
|
192
|
+
without having to specify the `:id` attribute.
|
|
193
|
+
MSG
|
|
194
|
+
end
|
|
195
|
+
coder = Sqids.new(**sqids_options)
|
|
196
|
+
encoding_method_name = as.presence || attributes.join("_and_").concat("_encoding")
|
|
197
|
+
database_methods = %w[find_by find_by! destroy_by delete_by].map do |base_method|
|
|
198
|
+
# ["find_by!", "find_by_id_encoding!"]
|
|
199
|
+
[base_method, base_method.gsub(/(\w+?)(!?)\b/, "\\1_#{encoding_method_name}\\2")]
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
# @!method <encoding_method_name>
|
|
203
|
+
# Returns the Sqids-encoded identifier for the configured attributes.
|
|
204
|
+
#
|
|
205
|
+
# Will return an irreversible encoding if any attribute is noninteger. Use the bang method to ensure a
|
|
206
|
+
# reversible encoding.
|
|
207
|
+
#
|
|
208
|
+
# @raises [ArgumentError] If any of the attributes is a number below 0 or above +Sqids.max_value+.
|
|
209
|
+
# @return [String, nil] Encoded identifier or nil if any of the attributes is +blank?+.
|
|
210
|
+
define_method(encoding_method_name) do
|
|
211
|
+
values = attributes.map { send(_1) }
|
|
212
|
+
values.any?(&:blank?) ? nil : coder.encode(values)
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
# @!method <encoding_method_name>
|
|
216
|
+
# Returns the Sqids-encoded identifier for the configured attributes.
|
|
217
|
+
#
|
|
218
|
+
# Ensures a reversible encoding.
|
|
219
|
+
#
|
|
220
|
+
# @raises [ArgumentError] If any of the attributes is not a positive integer between 0 and +Sqids.max_value+
|
|
221
|
+
# @return [String] Encoded identifier.
|
|
222
|
+
define_method("#{encoding_method_name}!") do
|
|
223
|
+
values = attributes.map { send(_1) }
|
|
224
|
+
unless values.all? { _1.is_a?(Integer) }
|
|
225
|
+
raise ArgumentError, <<~MSG
|
|
226
|
+
Encoding supports integers between 0 and #{Sqids.max_value}.
|
|
227
|
+
|
|
228
|
+
Received: #{attributes.zip(values).to_h}
|
|
229
|
+
MSG
|
|
230
|
+
end
|
|
231
|
+
coder.encode(values)
|
|
232
|
+
end
|
|
233
|
+
|
|
234
|
+
database_methods.each do |base_method, dynamic_method|
|
|
235
|
+
# @!method find_by_<dynamic_method>(encoding)
|
|
236
|
+
# Find a record by decoding the given encoding into the configured
|
|
237
|
+
# attributes, then delegating to the corresponding Active Record
|
|
238
|
+
# query method (e.g. `find_by`, `find_by!`, `destroy_by`, `delete_by`).
|
|
239
|
+
#
|
|
240
|
+
# @param encoding [String] Sqids-encoded identifier
|
|
241
|
+
# @return [Object, nil] model instance or result of the delegated
|
|
242
|
+
# query method
|
|
243
|
+
#
|
|
244
|
+
# The actual method names are generated dynamically, e.g.:
|
|
245
|
+
# `find_by_id_encoding`, `find_by_id_encoding!`,
|
|
246
|
+
# `destroy_by_id_encoding`, `delete_by_id_encoding`.
|
|
247
|
+
define_singleton_method(dynamic_method) do |encoding|
|
|
248
|
+
values = coder.decode(encoding)
|
|
249
|
+
args = attributes.zip(values).to_h
|
|
250
|
+
send(base_method, args)
|
|
251
|
+
end
|
|
252
|
+
end
|
|
253
|
+
|
|
254
|
+
unless decodes_as.blank?
|
|
255
|
+
decoding_method_name = decodes_as.to_s
|
|
256
|
+
# @!method <decodes_as>(encoding)
|
|
257
|
+
# Decode the given encoding into a hash mapping each configured
|
|
258
|
+
# attribute to its decoded numeric value.
|
|
259
|
+
#
|
|
260
|
+
# @param encoding [String] Sqids-encoded identifier
|
|
261
|
+
# @return [Hash{Symbol=>Integer}] decoded attribute values
|
|
262
|
+
define_singleton_method(decoding_method_name) do |encoding|
|
|
263
|
+
values = coder.decode(encoding)
|
|
264
|
+
attributes.zip(values).to_h
|
|
265
|
+
end
|
|
266
|
+
end
|
|
267
|
+
end
|
|
268
|
+
end
|
|
269
|
+
end
|
|
270
|
+
end
|
data/lib/sqinky.rb
ADDED
metadata
ADDED
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: sqinky
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- David Uhlig
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2026-03-06 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: activerecord
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - ">="
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: 7.0.0
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - ">="
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: 7.0.0
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: activesupport
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - ">="
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: 7.0.0
|
|
34
|
+
type: :runtime
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - ">="
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: 7.0.0
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: sqids
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - ">="
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: 0.2.0
|
|
48
|
+
type: :runtime
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - ">="
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: 0.2.0
|
|
55
|
+
- !ruby/object:Gem::Dependency
|
|
56
|
+
name: appraisal
|
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - ">="
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: '0'
|
|
62
|
+
type: :development
|
|
63
|
+
prerelease: false
|
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - ">="
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: '0'
|
|
69
|
+
- !ruby/object:Gem::Dependency
|
|
70
|
+
name: irb
|
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
|
72
|
+
requirements:
|
|
73
|
+
- - ">="
|
|
74
|
+
- !ruby/object:Gem::Version
|
|
75
|
+
version: '0'
|
|
76
|
+
type: :development
|
|
77
|
+
prerelease: false
|
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
79
|
+
requirements:
|
|
80
|
+
- - ">="
|
|
81
|
+
- !ruby/object:Gem::Version
|
|
82
|
+
version: '0'
|
|
83
|
+
- !ruby/object:Gem::Dependency
|
|
84
|
+
name: pg
|
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
|
86
|
+
requirements:
|
|
87
|
+
- - ">="
|
|
88
|
+
- !ruby/object:Gem::Version
|
|
89
|
+
version: '0'
|
|
90
|
+
type: :development
|
|
91
|
+
prerelease: false
|
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
93
|
+
requirements:
|
|
94
|
+
- - ">="
|
|
95
|
+
- !ruby/object:Gem::Version
|
|
96
|
+
version: '0'
|
|
97
|
+
- !ruby/object:Gem::Dependency
|
|
98
|
+
name: rake
|
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
|
100
|
+
requirements:
|
|
101
|
+
- - "~>"
|
|
102
|
+
- !ruby/object:Gem::Version
|
|
103
|
+
version: '13.0'
|
|
104
|
+
type: :development
|
|
105
|
+
prerelease: false
|
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
107
|
+
requirements:
|
|
108
|
+
- - "~>"
|
|
109
|
+
- !ruby/object:Gem::Version
|
|
110
|
+
version: '13.0'
|
|
111
|
+
- !ruby/object:Gem::Dependency
|
|
112
|
+
name: rspec
|
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
|
114
|
+
requirements:
|
|
115
|
+
- - "~>"
|
|
116
|
+
- !ruby/object:Gem::Version
|
|
117
|
+
version: '3.2'
|
|
118
|
+
type: :development
|
|
119
|
+
prerelease: false
|
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
121
|
+
requirements:
|
|
122
|
+
- - "~>"
|
|
123
|
+
- !ruby/object:Gem::Version
|
|
124
|
+
version: '3.2'
|
|
125
|
+
- !ruby/object:Gem::Dependency
|
|
126
|
+
name: standard
|
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
|
128
|
+
requirements:
|
|
129
|
+
- - "~>"
|
|
130
|
+
- !ruby/object:Gem::Version
|
|
131
|
+
version: '1.3'
|
|
132
|
+
type: :development
|
|
133
|
+
prerelease: false
|
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
135
|
+
requirements:
|
|
136
|
+
- - "~>"
|
|
137
|
+
- !ruby/object:Gem::Version
|
|
138
|
+
version: '1.3'
|
|
139
|
+
- !ruby/object:Gem::Dependency
|
|
140
|
+
name: sqlite3
|
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
|
142
|
+
requirements:
|
|
143
|
+
- - ">="
|
|
144
|
+
- !ruby/object:Gem::Version
|
|
145
|
+
version: '0'
|
|
146
|
+
type: :development
|
|
147
|
+
prerelease: false
|
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
149
|
+
requirements:
|
|
150
|
+
- - ">="
|
|
151
|
+
- !ruby/object:Gem::Version
|
|
152
|
+
version: '0'
|
|
153
|
+
- !ruby/object:Gem::Dependency
|
|
154
|
+
name: benchmark-ips
|
|
155
|
+
requirement: !ruby/object:Gem::Requirement
|
|
156
|
+
requirements:
|
|
157
|
+
- - ">="
|
|
158
|
+
- !ruby/object:Gem::Version
|
|
159
|
+
version: '0'
|
|
160
|
+
type: :development
|
|
161
|
+
prerelease: false
|
|
162
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
163
|
+
requirements:
|
|
164
|
+
- - ">="
|
|
165
|
+
- !ruby/object:Gem::Version
|
|
166
|
+
version: '0'
|
|
167
|
+
description: 'Sqinky adds a thin access layer on top of Sqids to work effortlessly
|
|
168
|
+
with Sqids in Active Record models. It supports encodings composed of multiple attributes,
|
|
169
|
+
and multiple encodings per model.
|
|
170
|
+
|
|
171
|
+
'
|
|
172
|
+
email:
|
|
173
|
+
- david.uhlig@gmail.com
|
|
174
|
+
executables: []
|
|
175
|
+
extensions: []
|
|
176
|
+
extra_rdoc_files: []
|
|
177
|
+
files:
|
|
178
|
+
- CHANGELOG.md
|
|
179
|
+
- CODE_OF_CONDUCT.md
|
|
180
|
+
- LICENSE.md
|
|
181
|
+
- README.md
|
|
182
|
+
- Rakefile
|
|
183
|
+
- lib/sqinky.rb
|
|
184
|
+
- lib/sqinky/identifier_encoding.rb
|
|
185
|
+
- lib/sqinky/version.rb
|
|
186
|
+
homepage: https://github.com/david-uhlig/sqinky
|
|
187
|
+
licenses:
|
|
188
|
+
- MIT
|
|
189
|
+
metadata:
|
|
190
|
+
homepage_uri: https://github.com/david-uhlig/sqinky
|
|
191
|
+
source_code_uri: https://github.com/david-uhlig/sqinky
|
|
192
|
+
changelog_uri: https://github.com/david-uhlig/sqinky/blob/main/CHANGELOG.md
|
|
193
|
+
post_install_message:
|
|
194
|
+
rdoc_options: []
|
|
195
|
+
require_paths:
|
|
196
|
+
- lib
|
|
197
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
198
|
+
requirements:
|
|
199
|
+
- - ">="
|
|
200
|
+
- !ruby/object:Gem::Version
|
|
201
|
+
version: 3.2.0
|
|
202
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
203
|
+
requirements:
|
|
204
|
+
- - ">="
|
|
205
|
+
- !ruby/object:Gem::Version
|
|
206
|
+
version: '0'
|
|
207
|
+
requirements: []
|
|
208
|
+
rubygems_version: 3.5.22
|
|
209
|
+
signing_key:
|
|
210
|
+
specification_version: 4
|
|
211
|
+
summary: Add Sqids-based identifier encoding/decoding helpers to Active Record models.
|
|
212
|
+
test_files: []
|