encoded_id-rails 1.0.0.beta1 → 1.0.0.beta2
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 +4 -4
- data/CHANGELOG.md +1 -1
- data/README.md +58 -27
- data/lib/encoded_id/rails/configuration.rb +30 -9
- data/lib/encoded_id/rails/version.rb +1 -1
- data/lib/generators/encoded_id/rails/templates/encoded_id.rb +26 -0
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0a92cfd619fa254d0996950214c14aa115d6c62327e4ec2a6715dc9d7d898ebf
|
4
|
+
data.tar.gz: 27b448535392de1e1e21c37b82c324e4b5aecdc5225d4c7b2757a1ce8d5d18a9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1ad6f6555bbdb19650f16220dedc81c733f847931a6eea27e28e69c9050f6542d28b8295a6ef3395c95bfe5480c22cc7e1ea224dc687616634ad34da58a71ddf
|
7
|
+
data.tar.gz: 53d42d543a9c86fd2ab3842bad841e7aff95cdc57059bbade5529183df3fa3ee426525d1100174d22821644c5f3915b0bef4e4c252ea56d4c08352bc662aa00f
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,29 @@
|
|
1
1
|
# EncodedId::Rails (`encoded_id-rails`)
|
2
2
|
|
3
|
-
[EncodedId](https://github.com/stevegeek/encoded_id)
|
3
|
+
`EncodedId::Rails` lets you turn numeric or hex **IDs into reversible and human friendly obfuscated strings**. The gem brings [EncodedId](https://github.com/stevegeek/encoded_id) to Rails and `ActiveRecord` models.
|
4
4
|
|
5
|
-
|
5
|
+
You can use it in routes for example, to go from something like `/users/725` to `/users/bob-smith--usr_p5w9-z27j` with miminal effort.
|
6
|
+
|
7
|
+
## Features
|
8
|
+
|
9
|
+
Under the hood it uses hashIds, but it offers more features.
|
10
|
+
|
11
|
+
- 🔄 encoded IDs are reversible (see [`encoded_id`](https://github.com/stevegeek/encoded_id))
|
12
|
+
- 💅 supports slugged IDs (eg `my-cool-product-name--p5w9-z27j`) that are URL friendly (assuming your alphabet is too)
|
13
|
+
- 🔖 supports annotated IDs to help identify the model the encoded ID belongs to (eg for a `User` the encoded ID might be `user_p5w9-z27j`)
|
14
|
+
- 👓 encoded string can be split into groups of letters to improve human-readability (eg `abcd-efgh`)
|
15
|
+
- 👥 supports multiple IDs encoded in one encoded string (eg imagine the encoded ID `7aq60zqw` might decode to two IDs `[78, 45]`)
|
16
|
+
- 🔡 supports custom alphabets for the encoded string (at least 16 characters needed)
|
17
|
+
- by default uses a variation of the Crockford reduced character set (https://www.crockford.com/base32.html)
|
18
|
+
- easily confused characters (eg i and j, 0 and O, 1 and I etc) are mapped to counterpart characters, to
|
19
|
+
help avoid common readability mistakes when reading/sharing
|
20
|
+
- build in profanity limitation
|
21
|
+
|
22
|
+
The gem provides:
|
23
|
+
|
24
|
+
- methods to mixin to ActiveRecord models which will allow you to encode and decode IDs, and find
|
25
|
+
or query by encoded IDs
|
26
|
+
- sensible defaults to allow you to get started out of the box
|
6
27
|
|
7
28
|
```ruby
|
8
29
|
class User < ApplicationRecord
|
@@ -34,43 +55,25 @@ user == User.find_by_encoded_id("bob-smith--usr_p5w9-z27j") # => true
|
|
34
55
|
users = User.find_all_by_encoded_id("7aq60zqw") # => [#<User id: 78>, #<User id: 45>]
|
35
56
|
```
|
36
57
|
|
37
|
-
|
38
|
-
|
39
|
-
- encoded IDs are reversible (see [`encoded_id`](https://github.com/stevegeek/encoded_id))
|
40
|
-
- supports slugged IDs (eg `my-cool-product-name--p5w9-z27j`) that are URL friendly (assuming your alphabet is too)
|
41
|
-
- supports annotated IDs to help identify the model the encoded ID belongs to (eg for a `User` the encoded ID might be `user_p5w9-z27j`)
|
42
|
-
- encoded string can be split into groups of letters to improve human-readability (eg `abcd-efgh`)
|
43
|
-
- supports multiple IDs encoded in one encoded string (eg imagine the encoded ID `7aq60zqw` might decode to two IDs `[78, 45]`)
|
44
|
-
- supports custom alphabets for the encoded string (at least 16 characters needed)
|
45
|
-
- by default uses a variation of the Crockford reduced character set (https://www.crockford.com/base32.html)
|
46
|
-
- easily confused characters (eg i and j, 0 and O, 1 and I etc) are mapped to counterpart characters, to
|
47
|
-
help avoid common readability mistakes when reading/sharing
|
48
|
-
- build in profanity limitation
|
49
|
-
|
50
|
-
The gem provides:
|
51
|
-
|
52
|
-
- methods to mixin to ActiveRecord models which will allow you to encode and decode IDs, and find
|
53
|
-
or query by encoded IDs
|
54
|
-
- sensible defaults to allow you to get started out of the box
|
55
|
-
|
56
|
-
### Coming in future (?)
|
57
|
-
|
58
|
-
- support for UUIDs for IDs (which will be encoded as an array of integers)
|
59
|
-
|
60
|
-
# Why this gem?
|
58
|
+
## Why this gem?
|
61
59
|
|
62
60
|
With this gem you can easily obfuscate your IDs in your URLs, and still be able to find records by using
|
63
61
|
the encoded IDs. The encoded IDs are meant to be somewhat human friendly, to make communication easier
|
64
62
|
when sharing encoded IDs with other people.
|
65
63
|
|
66
64
|
* Hashids are reversible, no need to persist the generated Id
|
67
|
-
* we don't override any AR methods. `encoded_id`s are intentionally not interchangeable with normal record `id`s
|
65
|
+
* we don't override any AR methods. `encoded_id`s are intentionally **not interchangeable** with normal record `id`s
|
68
66
|
(ie you can't use `.find` to find by encoded ID or record ID, you must be explicit)
|
69
67
|
* we support slugged IDs (eg `my-amazing-product--p5w9-z27j`)
|
70
68
|
* we support multiple model IDs encoded in one `EncodedId` (eg `7aq6-0zqw` might decode to `[78, 45]`)
|
71
69
|
* the gem is configurable
|
72
70
|
* encoded IDs can be stable across environments, or not (you can set the salt to different values per environment)
|
73
71
|
|
72
|
+
|
73
|
+
## Coming in future (?)
|
74
|
+
|
75
|
+
- support for UUIDs for IDs (which will be encoded as an array of integers)
|
76
|
+
|
74
77
|
## Installation
|
75
78
|
|
76
79
|
Install the gem and add to the application's Gemfile by executing:
|
@@ -360,6 +363,34 @@ end
|
|
360
363
|
|
361
364
|
However, I recommend you only use it on the models that need it.
|
362
365
|
|
366
|
+
## Example usage for a route and controller
|
367
|
+
|
368
|
+
```ruby
|
369
|
+
# Route
|
370
|
+
resources :users, param: :encoded_id, only: [:show]
|
371
|
+
```
|
372
|
+
|
373
|
+
```ruby
|
374
|
+
# Model
|
375
|
+
class User < ApplicationRecord
|
376
|
+
include EncodedId::Model
|
377
|
+
include EncodedId::PathParam
|
378
|
+
end
|
379
|
+
```
|
380
|
+
|
381
|
+
```ruby
|
382
|
+
# Controller
|
383
|
+
class UsersController < ApplicationController
|
384
|
+
def show
|
385
|
+
@user = User.find_by_encoded_id!(params[:encoded_id])
|
386
|
+
end
|
387
|
+
end
|
388
|
+
```
|
389
|
+
|
390
|
+
```erb
|
391
|
+
<%= link_to "User", user_path %>
|
392
|
+
```
|
393
|
+
|
363
394
|
## Development
|
364
395
|
|
365
396
|
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.
|
@@ -4,15 +4,9 @@ module EncodedId
|
|
4
4
|
module Rails
|
5
5
|
# Configuration class for initializer
|
6
6
|
class Configuration
|
7
|
-
attr_accessor :salt,
|
8
|
-
|
9
|
-
|
10
|
-
:alphabet,
|
11
|
-
:id_length,
|
12
|
-
:slug_value_method_name,
|
13
|
-
:slugged_id_separator,
|
14
|
-
:annotation_method_name, # Set to nil to disable annotated IDs
|
15
|
-
:annotated_id_separator
|
7
|
+
attr_accessor :salt, :character_group_size, :alphabet, :id_length
|
8
|
+
attr_accessor :slug_value_method_name, :annotation_method_name
|
9
|
+
attr_reader :group_separator, :slugged_id_separator, :annotated_id_separator
|
16
10
|
|
17
11
|
def initialize
|
18
12
|
@character_group_size = 4
|
@@ -24,6 +18,33 @@ module EncodedId
|
|
24
18
|
@annotation_method_name = :annotation_for_encoded_id
|
25
19
|
@annotated_id_separator = "_"
|
26
20
|
end
|
21
|
+
|
22
|
+
# Perform validation vs alphabet on these assignments
|
23
|
+
|
24
|
+
def group_separator=(value)
|
25
|
+
unless valid_separator?(value, alphabet)
|
26
|
+
raise ArgumentError, "Group separator characters must not be part of the alphabet"
|
27
|
+
end
|
28
|
+
@group_separator = value
|
29
|
+
end
|
30
|
+
|
31
|
+
def slugged_id_separator=(value)
|
32
|
+
if value.blank? || value == group_separator || !valid_separator?(value, alphabet)
|
33
|
+
raise ArgumentError, "Slugged ID separator characters must not be part of the alphabet or the same as the group separator"
|
34
|
+
end
|
35
|
+
@slugged_id_separator = value
|
36
|
+
end
|
37
|
+
|
38
|
+
def annotated_id_separator=(value)
|
39
|
+
if value.blank? || value == group_separator || !valid_separator?(value, alphabet)
|
40
|
+
raise ArgumentError, "Annotated ID separator characters must not be part of the alphabet or the same as the group separator"
|
41
|
+
end
|
42
|
+
@annotated_id_separator = value
|
43
|
+
end
|
44
|
+
|
45
|
+
def valid_separator?(separator, characters)
|
46
|
+
separator.chars.none? { |v| characters.include?(v) }
|
47
|
+
end
|
27
48
|
end
|
28
49
|
end
|
29
50
|
end
|
@@ -41,4 +41,30 @@ EncodedId::Rails.configure do |config|
|
|
41
41
|
# Default: 8
|
42
42
|
#
|
43
43
|
# config.id_length = 8
|
44
|
+
|
45
|
+
# The name of the method that returns the value to be used in the slug.
|
46
|
+
#
|
47
|
+
# Default: :name_for_encoded_id_slug
|
48
|
+
#
|
49
|
+
# config.slug_value_method_name = :name_for_encoded_id_slug
|
50
|
+
|
51
|
+
# The separator used between the slug and the encoded ID.
|
52
|
+
# `nil` disables grouping.
|
53
|
+
#
|
54
|
+
# Default: "--"
|
55
|
+
#
|
56
|
+
# config.slugged_id_separator = "--"
|
57
|
+
|
58
|
+
# The name of the method that returns the annotation to be used in the annotated ID.
|
59
|
+
#
|
60
|
+
# Default: :annotation_for_encoded_id
|
61
|
+
#
|
62
|
+
# config.annotation_method_name = :annotation_for_encoded_id
|
63
|
+
|
64
|
+
# The separator used between the annotation and the encoded ID.
|
65
|
+
# `nil` disables annotation.
|
66
|
+
#
|
67
|
+
# Default: "_"
|
68
|
+
#
|
69
|
+
# config.annotated_id_separator = "_"
|
44
70
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: encoded_id-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.0.
|
4
|
+
version: 1.0.0.beta2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stephen Ierodiaconou
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-10-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -56,14 +56,14 @@ dependencies:
|
|
56
56
|
requirements:
|
57
57
|
- - "~>"
|
58
58
|
- !ruby/object:Gem::Version
|
59
|
-
version: 1.0.0.
|
59
|
+
version: 1.0.0.rc3
|
60
60
|
type: :runtime
|
61
61
|
prerelease: false
|
62
62
|
version_requirements: !ruby/object:Gem::Requirement
|
63
63
|
requirements:
|
64
64
|
- - "~>"
|
65
65
|
- !ruby/object:Gem::Version
|
66
|
-
version: 1.0.0.
|
66
|
+
version: 1.0.0.rc3
|
67
67
|
description: ActiveRecord concern to use EncodedID to turn IDs into reversible and
|
68
68
|
human friendly obfuscated strings.
|
69
69
|
email:
|
@@ -123,7 +123,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
123
123
|
- !ruby/object:Gem::Version
|
124
124
|
version: 1.3.1
|
125
125
|
requirements: []
|
126
|
-
rubygems_version: 3.4.
|
126
|
+
rubygems_version: 3.4.20
|
127
127
|
signing_key:
|
128
128
|
specification_version: 4
|
129
129
|
summary: Use `encoded_id` with ActiveRecord models
|