encoded_id-rails 0.4.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of encoded_id-rails might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/lib/encoded_id/rails/encoder_methods.rb +40 -0
- data/lib/encoded_id/rails/finder_methods.rb +27 -0
- data/lib/encoded_id/rails/query_methods.rb +13 -0
- data/lib/encoded_id/rails/version.rb +1 -1
- data/lib/encoded_id/rails/with_encoded_id.rb +5 -69
- data/lib/encoded_id/rails.rb +3 -0
- data/sig/encoded_id/rails.rbs +37 -39
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6cfa444903c74b47cf55b4c8aa45d862f6eecbe843ff0af9b987b713c4b226d4
|
4
|
+
data.tar.gz: 7f530fdd9cd3b8a83a308f45e236c92c2bbdaae394cc8b455178ce1ff3de9802
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e8e9be56253a8956f1a850905c6a9c53e33f7593d419c7348a158e8ff76e3a0285319c116b8a81bfb22d964ceec6aac85fcbe128b3a4ad8265b0b790be27b83b
|
7
|
+
data.tar.gz: bc63195e7d397c41350b80783526d1733e6109ddc9cd9cbace00d827ff95b19046b287030436da09e359bb3f92774efe702bc17dc4fea006192748463c5eb9b7
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module EncodedId
|
4
|
+
module Rails
|
5
|
+
module EncoderMethods
|
6
|
+
def encode_encoded_id(ids, options = {})
|
7
|
+
raise StandardError, "You must pass an ID or array of IDs" if ids.blank?
|
8
|
+
encoded_id_coder(options).encode(ids)
|
9
|
+
end
|
10
|
+
|
11
|
+
def decode_encoded_id(slugged_encoded_id, options = {})
|
12
|
+
return if slugged_encoded_id.blank?
|
13
|
+
encoded_id = encoded_id_parser(slugged_encoded_id).id
|
14
|
+
return if !encoded_id || encoded_id.blank?
|
15
|
+
encoded_id_coder(options).decode(encoded_id)
|
16
|
+
end
|
17
|
+
|
18
|
+
# This can be overridden in the model to provide a custom salt
|
19
|
+
def encoded_id_salt
|
20
|
+
# @type self: Class
|
21
|
+
EncodedId::Rails::Salt.new(self, EncodedId::Rails.configuration.salt).generate!
|
22
|
+
end
|
23
|
+
|
24
|
+
def encoded_id_parser(slugged_encoded_id)
|
25
|
+
SluggedIdParser.new(slugged_encoded_id, separator: EncodedId::Rails.configuration.slugged_id_separator)
|
26
|
+
end
|
27
|
+
|
28
|
+
def encoded_id_coder(options = {})
|
29
|
+
config = EncodedId::Rails.configuration
|
30
|
+
EncodedId::Rails::Coder.new(
|
31
|
+
salt: options[:salt] || encoded_id_salt,
|
32
|
+
id_length: options[:id_length] || config.id_length,
|
33
|
+
character_group_size: options[:character_group_size] || config.character_group_size,
|
34
|
+
alphabet: options[:alphabet] || config.alphabet,
|
35
|
+
separator: options[:separator] || config.group_separator
|
36
|
+
)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module EncodedId
|
4
|
+
module Rails
|
5
|
+
module FinderMethods
|
6
|
+
# Find by encoded ID and optionally ensure record ID is the same as constraint (can be slugged)
|
7
|
+
def find_by_encoded_id(slugged_encoded_id, with_id: nil)
|
8
|
+
decoded_id = decode_encoded_id(slugged_encoded_id)
|
9
|
+
return if decoded_id.blank?
|
10
|
+
record = find_by(id: decoded_id)
|
11
|
+
return unless record
|
12
|
+
return if with_id && with_id != record.send(:id)
|
13
|
+
record
|
14
|
+
end
|
15
|
+
|
16
|
+
def find_by_encoded_id!(slugged_encoded_id, with_id: nil)
|
17
|
+
decoded_id = decode_encoded_id(slugged_encoded_id)
|
18
|
+
raise ActiveRecord::RecordNotFound if decoded_id.blank?
|
19
|
+
record = find_by(id: decoded_id)
|
20
|
+
if !record || (with_id && with_id != record.send(:id))
|
21
|
+
raise ActiveRecord::RecordNotFound
|
22
|
+
end
|
23
|
+
record
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module EncodedId
|
4
|
+
module Rails
|
5
|
+
module QueryMethods
|
6
|
+
def where_encoded_id(slugged_encoded_id)
|
7
|
+
decoded_id = decode_encoded_id(slugged_encoded_id)
|
8
|
+
raise ActiveRecord::RecordNotFound if decoded_id.nil?
|
9
|
+
where(id: decoded_id)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -7,71 +7,11 @@ module EncodedId
|
|
7
7
|
module Rails
|
8
8
|
module WithEncodedId
|
9
9
|
def self.included(base)
|
10
|
-
base.extend(
|
10
|
+
base.extend(EncoderMethods)
|
11
|
+
base.extend(FinderMethods)
|
12
|
+
base.extend(QueryMethods)
|
11
13
|
end
|
12
14
|
|
13
|
-
module ClassMethods
|
14
|
-
# Find by encoded ID and optionally ensure record ID is the same as constraint (can be slugged)
|
15
|
-
def find_by_encoded_id(slugged_encoded_id, with_id: nil)
|
16
|
-
decoded_id = decode_encoded_id(slugged_encoded_id)
|
17
|
-
return if decoded_id.blank?
|
18
|
-
record = find_by(id: decoded_id)
|
19
|
-
return unless record
|
20
|
-
return if with_id && with_id != record.send(:id)
|
21
|
-
record
|
22
|
-
end
|
23
|
-
|
24
|
-
def find_by_encoded_id!(slugged_encoded_id, with_id: nil)
|
25
|
-
decoded_id = decode_encoded_id(slugged_encoded_id)
|
26
|
-
raise ActiveRecord::RecordNotFound if decoded_id.blank?
|
27
|
-
record = find_by(id: decoded_id)
|
28
|
-
if !record || (with_id && with_id != record.send(:id))
|
29
|
-
raise ActiveRecord::RecordNotFound
|
30
|
-
end
|
31
|
-
record
|
32
|
-
end
|
33
|
-
|
34
|
-
def where_encoded_id(slugged_encoded_id)
|
35
|
-
decoded_id = decode_encoded_id(slugged_encoded_id)
|
36
|
-
raise ActiveRecord::RecordNotFound if decoded_id.nil?
|
37
|
-
where(id: decoded_id)
|
38
|
-
end
|
39
|
-
|
40
|
-
def encode_encoded_id(ids, options = {})
|
41
|
-
raise StandardError, "You must pass an ID or array of IDs" if ids.blank?
|
42
|
-
encoded_id_coder(options).encode(ids)
|
43
|
-
end
|
44
|
-
|
45
|
-
def decode_encoded_id(slugged_encoded_id, options = {})
|
46
|
-
return if slugged_encoded_id.blank?
|
47
|
-
encoded_id = encoded_id_parser(slugged_encoded_id).id
|
48
|
-
return if !encoded_id || encoded_id.blank?
|
49
|
-
encoded_id_coder(options).decode(encoded_id)
|
50
|
-
end
|
51
|
-
|
52
|
-
# This can be overridden in the model to provide a custom salt
|
53
|
-
def encoded_id_salt
|
54
|
-
EncodedId::Rails::Salt.new(self, EncodedId::Rails.configuration.salt).generate!
|
55
|
-
end
|
56
|
-
|
57
|
-
def encoded_id_parser(slugged_encoded_id)
|
58
|
-
SluggedIdParser.new(slugged_encoded_id, separator: EncodedId::Rails.configuration.slugged_id_separator)
|
59
|
-
end
|
60
|
-
|
61
|
-
def encoded_id_coder(options = {})
|
62
|
-
config = EncodedId::Rails.configuration
|
63
|
-
EncodedId::Rails::Coder.new(
|
64
|
-
salt: options[:salt] || encoded_id_salt,
|
65
|
-
id_length: options[:id_length] || config.id_length,
|
66
|
-
character_group_size: options[:character_group_size] || config.character_group_size,
|
67
|
-
alphabet: options[:alphabet] || config.alphabet,
|
68
|
-
separator: options[:separator] || config.group_separator
|
69
|
-
)
|
70
|
-
end
|
71
|
-
end
|
72
|
-
|
73
|
-
# Instance methods
|
74
|
-
|
75
15
|
def encoded_id
|
76
16
|
@encoded_id ||= self.class.encode_encoded_id(id)
|
77
17
|
end
|
@@ -85,14 +25,10 @@ module EncodedId
|
|
85
25
|
).slugged_id
|
86
26
|
end
|
87
27
|
|
88
|
-
# By default slug
|
28
|
+
# By default slug created from class name, but can be overridden
|
89
29
|
def name_for_encoded_id_slug
|
90
|
-
if respond_to? :name
|
91
|
-
given_name = name
|
92
|
-
return given_name if given_name.present?
|
93
|
-
end
|
94
30
|
class_name = self.class.name
|
95
|
-
raise StandardError, "
|
31
|
+
raise StandardError, "Class must have a `name`, cannot create a slug" if !class_name || class_name.blank?
|
96
32
|
class_name.underscore
|
97
33
|
end
|
98
34
|
end
|
data/lib/encoded_id/rails.rb
CHANGED
@@ -6,6 +6,9 @@ require_relative "rails/coder"
|
|
6
6
|
require_relative "rails/slugged_id"
|
7
7
|
require_relative "rails/slugged_id_parser"
|
8
8
|
require_relative "rails/salt"
|
9
|
+
require_relative "rails/encoder_methods"
|
10
|
+
require_relative "rails/query_methods"
|
11
|
+
require_relative "rails/finder_methods"
|
9
12
|
require_relative "rails/with_encoded_id"
|
10
13
|
|
11
14
|
module EncodedId
|
data/sig/encoded_id/rails.rbs
CHANGED
@@ -5,7 +5,7 @@ module EncodedId
|
|
5
5
|
class Coder
|
6
6
|
def initialize: (salt: ::String, id_length: ::Integer, character_group_size: ::Integer, separator: ::String, alphabet: ::EncodedId::Alphabet) -> void
|
7
7
|
def encode: (::Integer | ::Array[::Integer]) -> String
|
8
|
-
def decode: (::String) ->
|
8
|
+
def decode: (::String) -> ::Array[::Integer]?
|
9
9
|
|
10
10
|
@salt: ::String
|
11
11
|
@id_length: ::Integer
|
@@ -30,9 +30,9 @@ module EncodedId
|
|
30
30
|
end
|
31
31
|
|
32
32
|
class Salt
|
33
|
-
def initialize: (
|
33
|
+
def initialize: (Class klass, ::String salt) -> void
|
34
34
|
|
35
|
-
@klass:
|
35
|
+
@klass: Class
|
36
36
|
@salt: ::String
|
37
37
|
|
38
38
|
def generate!: -> ::String
|
@@ -52,43 +52,46 @@ module EncodedId
|
|
52
52
|
class SluggedIdParser
|
53
53
|
def initialize: (::String slugged_id, ?separator: ::String) -> void
|
54
54
|
|
55
|
-
attr_reader slug:
|
56
|
-
attr_reader id:
|
55
|
+
attr_reader slug: ::String?
|
56
|
+
attr_reader id: ::String?
|
57
57
|
end
|
58
58
|
|
59
59
|
attr_reader self.configuration: Configuration
|
60
60
|
|
61
61
|
def self.configure: () { (Configuration config) -> void } -> void
|
62
62
|
|
63
|
-
module
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
def
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
63
|
+
module EncoderMethods
|
64
|
+
def encode_encoded_id: (untyped id, ?::Hash[::Symbol, untyped] options) -> ::String
|
65
|
+
def decode_encoded_id: (::String slugged_encoded_id, ?::Hash[::Symbol, untyped] options) -> ::Array[::Integer]?
|
66
|
+
def encoded_id_salt: () -> ::String
|
67
|
+
def encoded_id_parser: (::String slugged_encoded_id) -> ::EncodedId::Rails::SluggedIdParser
|
68
|
+
def encoded_id_coder: (?::Hash[::Symbol, untyped] options) -> ::EncodedId::Rails::Coder
|
69
|
+
end
|
70
|
+
|
71
|
+
interface _ActiveRecordFinderMethod
|
72
|
+
def find_by: (*untyped) -> (nil | untyped)
|
73
|
+
end
|
74
|
+
|
75
|
+
module FinderMethods : EncoderMethods, _ActiveRecordFinderMethod
|
76
|
+
def find_by_encoded_id: (::String slugged_encoded_id, ?with_id: ::Symbol?) -> untyped?
|
77
|
+
def find_by_encoded_id!: (::String slugged_encoded_id, ?with_id: ::Symbol?) -> untyped
|
78
|
+
end
|
79
|
+
|
80
|
+
interface _ActiveRecordQueryMethod
|
81
|
+
def where: (*untyped) -> untyped
|
82
|
+
end
|
83
|
+
|
84
|
+
module QueryMethods : EncoderMethods, _ActiveRecordQueryMethod
|
85
|
+
def where_encoded_id: (::String slugged_encoded_id) -> untyped
|
86
|
+
end
|
87
|
+
|
88
|
+
module WithEncodedId : ActiveRecord::Base
|
89
|
+
extend ActiveRecord::FinderMethods
|
90
|
+
extend ActiveRecord::QueryMethods
|
91
|
+
|
92
|
+
extend EncoderMethods
|
93
|
+
extend FinderMethods
|
94
|
+
extend QueryMethods
|
92
95
|
|
93
96
|
@encoded_id: ::String
|
94
97
|
@slugged_encoded_id: ::String
|
@@ -96,11 +99,6 @@ module EncodedId
|
|
96
99
|
def encoded_id: () -> ::String
|
97
100
|
def slugged_encoded_id: (?with: ::Symbol) -> ::String
|
98
101
|
def name_for_encoded_id_slug: () -> ::String
|
99
|
-
|
100
|
-
# FIXME: To make type check happy, but may not exist!
|
101
|
-
# We call if respond_to? but type checker doesn't know that
|
102
|
-
def name: () -> ::String
|
103
|
-
def id: () -> ::String
|
104
102
|
end
|
105
103
|
end
|
106
104
|
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: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stephen Ierodiaconou
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-12-
|
11
|
+
date: 2022-12-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -84,6 +84,9 @@ files:
|
|
84
84
|
- lib/encoded_id/rails.rb
|
85
85
|
- lib/encoded_id/rails/coder.rb
|
86
86
|
- lib/encoded_id/rails/configuration.rb
|
87
|
+
- lib/encoded_id/rails/encoder_methods.rb
|
88
|
+
- lib/encoded_id/rails/finder_methods.rb
|
89
|
+
- lib/encoded_id/rails/query_methods.rb
|
87
90
|
- lib/encoded_id/rails/salt.rb
|
88
91
|
- lib/encoded_id/rails/slugged_id.rb
|
89
92
|
- lib/encoded_id/rails/slugged_id_parser.rb
|