encoded_id-rails 0.4.0 → 0.6.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.

Potentially problematic release.


This version of encoded_id-rails might be problematic. Click here for more details.

checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0aceb28e50697bb0257054c2731936a7cad5f6617d479543c63999a156c153bf
4
- data.tar.gz: 59be3ddf7327f26726df0aa3120f9a70de0ac225d77291ad8e35b10b937e6264
3
+ metadata.gz: 599b4c236c59881b00eefbe0feaf4fa6f1a04015573ba5d807f041ba2932e602
4
+ data.tar.gz: ff6ee760d956606accfb98a75be00cf04a545695e0dcce56e9eb271b1d824fee
5
5
  SHA512:
6
- metadata.gz: 821e8ffcb7f77bdc84160a69bbc504ec891f9b11d925e4aa5d52f1209f426d894671cc603b850ee1a408dfa255c9b00eeaed6938eb1440aec4a31af27209f484
7
- data.tar.gz: a168a3da9c882a0e1c9f159cfcfeaeaf02109049aed2f2330b9a51708cee927896a911cae63833ec11955a0139af6aa9a3302a9e970a9f461024d4a870730bf2
6
+ metadata.gz: 9f176effb4bc926a4aa61dcbb7d0ca0e88b972186438ffe03a96b68d5e78e1eb0b6a485efcad924aff2070097356b8ced016fb1da0253b38f1d9e80eef21e18e
7
+ data.tar.gz: 2f318e0a04cbd5a45fc3edca02c1a8e053590b70963f4b851f8aac150ea465018aea6f21d3f5951bf4c8f7e31bd650a05a8a46328c2ce0812e8e26d2d4e92099
data/CHANGELOG.md CHANGED
@@ -1,5 +1,31 @@
1
1
  ## [Unreleased]
2
2
 
3
- ## [0.1.0] - 2022-10-11
3
+ ## [0.6.0] - 2022-12-21
4
+
5
+ - Rename mixin to `Model`
6
+ - Introduce optional mixins for overriding `#to_param`
7
+
8
+ ## [0.5.0] - 2022-12-21
9
+
10
+ - `name_for_encoded_id_slug` no longer uses the return value from name but rather just uses the `class` `name`.
11
+ - If you want to change the name used in the slug, override `name_for_encoded_id_slug`
12
+
13
+ ## [0.4.0] - 2022-12-18
14
+
15
+ - Refactor internals, remove any methods not actually related to creating `encoded_id`, (eg `slugged_id` was removed).
16
+
17
+ ## [0.3.1] - 2022-12-15
18
+
19
+ - Fix default config
20
+
21
+ ## [0.3.0] - 2022-12-15
22
+
23
+ - Updates gem `encoded_id` dependency and fixes configuration
24
+
25
+ ## [0.2.0] - 2022-12-14
26
+
27
+ - No notes...
28
+
29
+ ## [0.1.0] - 2022-11-17
4
30
 
5
31
  - Initial release
data/README.md CHANGED
@@ -6,16 +6,16 @@ EncodedID lets you turn numeric or hex IDs into reversible and human friendly ob
6
6
 
7
7
  ```ruby
8
8
  class User < ApplicationRecord
9
- include EncodedId::WithEncodedId
10
-
9
+ include EncodedId::Model
10
+
11
11
  def name_for_encoded_id_slug
12
- full_name.parameterize
12
+ full_name
13
13
  end
14
14
  end
15
15
 
16
- user = User.find_by_encoded_id("p5w9-z27j") # => #<User id: 78>
17
- user.encoded_id # => "p5w9-z27j"
18
- user.slugged_encoded_id # => "bob-smith--p5w9-z27j"
16
+ user = User.find_by_encoded_id("p5w9-z27j") # => #<User id: 78>
17
+ user.encoded_id # => "p5w9-z27j"
18
+ user.slugged_encoded_id # => "bob-smith--p5w9-z27j"
19
19
  ```
20
20
 
21
21
  # Features
@@ -85,21 +85,45 @@ You can configure:
85
85
 
86
86
  ### ActiveRecord model setup
87
87
 
88
- Include `EncodedId::WithEncodedId` in your model and optionally specify a encoded id salt (or not if using a global one):
88
+ Include `EncodedId::Model` in your model and optionally specify a encoded id salt (or not if using a global one):
89
89
 
90
90
  ```ruby
91
91
  class User < ApplicationRecord
92
- include EncodedId::WithEncodedId
93
-
92
+ include EncodedId::Model
93
+
94
94
  # and optionally the model's salt
95
95
  def encoded_id_salt
96
96
  "my-user-model-salt"
97
97
  end
98
-
98
+
99
99
  # ...
100
100
  end
101
101
  ```
102
102
 
103
+ ### Optional mixins
104
+
105
+ You can optionally include one of the following mixins to add default overrides to `#to_param`.
106
+
107
+ - `EncodedId::PathParam`
108
+ - `EncodedId::SluggedPathParam`
109
+
110
+ This is so that an instance of the model can be used in path helpers and
111
+ return the encoded ID string instead of the record ID by default.
112
+
113
+ ```ruby
114
+ class User < ApplicationRecord
115
+ include EncodedId::Model
116
+ include EncodedId::SluggedPathParam
117
+
118
+ def name_for_encoded_id_slug
119
+ full_name
120
+ end
121
+ end
122
+
123
+ user = User.create(full_name: "Bob Smith")
124
+ Rails.application.routes.url_helpers.user_path(user) # => "/users/bob-smith--p5w9-z27j"
125
+ ```
126
+
103
127
  ## Documentation
104
128
 
105
129
  ### `.find_by_encoded_id`
@@ -164,12 +188,13 @@ Otherwise override this method to return a salt specific to the model.
164
188
 
165
189
  ```ruby
166
190
  class User < ApplicationRecord
167
- include EncodedId::WithEncodedId
168
-
191
+ include EncodedId::Model
192
+
169
193
  def encoded_id_salt
170
194
  "my-user-model-salt"
171
195
  end
172
196
  end
197
+
173
198
  User.encoded_id_salt # => "my-user-model-salt"
174
199
  ```
175
200
 
@@ -201,8 +226,8 @@ By default it calls `#name` on the instance, or if the instance does not respond
201
226
 
202
227
  ```ruby
203
228
  class User < ApplicationRecord
204
- include EncodedId::WithEncodedId
205
-
229
+ include EncodedId::Model
230
+
206
231
  # If User has an attribute `name`, that will be used for the slug,
207
232
  # otherwise `user` will be used as determined by the class name.
208
233
  end
@@ -217,8 +242,8 @@ You can optionally override this method to define your own slug:
217
242
 
218
243
  ```ruby
219
244
  class User < ApplicationRecord
220
- include EncodedId::WithEncodedId
221
-
245
+ include EncodedId::Model
246
+
222
247
  def name_for_encoded_id_slug
223
248
  superhero_name
224
249
  end
@@ -232,11 +257,11 @@ user.slugged_encoded_id # => "super-dev--37nw-8nh7"
232
257
 
233
258
  Simply add the mixin to your `ApplicationRecord`:
234
259
 
235
- ```ruby
260
+ ```ruby
236
261
  class ApplicationRecord < ActiveRecord::Base
237
262
  self.abstract_class = true
238
- include EncodedId::WithEncodedId
239
-
263
+ include EncodedId::Model
264
+
240
265
  ...
241
266
  end
242
267
  ```
data/Steepfile CHANGED
@@ -1,5 +1,5 @@
1
1
  target :lib do
2
- check "lib/encoded_id/rails"
2
+ check "lib/encoded_id"
3
3
  signature "sig"
4
4
 
5
5
  library "encoded_id"
@@ -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,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_record"
4
+ require "encoded_id"
5
+
6
+ module EncodedId
7
+ module Rails
8
+ module Model
9
+ def self.included(base)
10
+ base.extend(EncoderMethods)
11
+ base.extend(FinderMethods)
12
+ base.extend(QueryMethods)
13
+ end
14
+
15
+ def encoded_id
16
+ @encoded_id ||= self.class.encode_encoded_id(id)
17
+ end
18
+
19
+ def slugged_encoded_id(with: :name_for_encoded_id_slug)
20
+ @slugged_encoded_id ||= EncodedId::Rails::SluggedId.new(
21
+ self,
22
+ slug_method: with,
23
+ id_method: :encoded_id,
24
+ separator: EncodedId::Rails.configuration.slugged_id_separator
25
+ ).slugged_id
26
+ end
27
+
28
+ # By default slug created from class name, but can be overridden
29
+ def name_for_encoded_id_slug
30
+ class_name = self.class.name
31
+ raise StandardError, "Class must have a `name`, cannot create a slug" if !class_name || class_name.blank?
32
+ class_name.underscore
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_record"
4
+ require "encoded_id"
5
+
6
+ module EncodedId
7
+ module Rails
8
+ module PathParam
9
+ def to_param
10
+ encoded_id
11
+ end
12
+ end
13
+ end
14
+ 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
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_record"
4
+ require "encoded_id"
5
+
6
+ module EncodedId
7
+ module Rails
8
+ module SluggedPathParam
9
+ def to_param
10
+ slugged_encoded_id
11
+ end
12
+ end
13
+ end
14
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module EncodedId
4
4
  module Rails
5
- VERSION = "0.4.0"
5
+ VERSION = "0.6.0"
6
6
  end
7
7
  end
@@ -6,7 +6,12 @@ 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/with_encoded_id"
9
+ require_relative "rails/encoder_methods"
10
+ require_relative "rails/query_methods"
11
+ require_relative "rails/finder_methods"
12
+ require_relative "rails/path_param"
13
+ require_relative "rails/slugged_path_param"
14
+ require_relative "rails/model"
10
15
 
11
16
  module EncodedId
12
17
  module Rails
@@ -24,5 +29,7 @@ module EncodedId
24
29
  end
25
30
 
26
31
  # Expose directly on EncodedId
27
- WithEncodedId = Rails::WithEncodedId
32
+ Model = Rails::Model
33
+ PathParam = Rails::PathParam
34
+ SluggedPathParam = Rails::SluggedPathParam
28
35
  end
@@ -2,10 +2,25 @@ module EncodedId
2
2
  module Rails
3
3
  VERSION: ::String
4
4
 
5
+ class Configuration
6
+ attr_accessor salt: ::String
7
+ attr_accessor group_separator: ::String
8
+ attr_accessor character_group_size: ::Integer
9
+ attr_accessor alphabet: ::EncodedId::Alphabet
10
+ attr_accessor id_length: ::Integer
11
+ attr_accessor slugged_id_separator: ::String
12
+
13
+ def initialize: () -> void
14
+ end
15
+
16
+ attr_reader self.configuration: Configuration
17
+
18
+ def self.configure: () { (Configuration config) -> void } -> void
19
+
5
20
  class Coder
6
21
  def initialize: (salt: ::String, id_length: ::Integer, character_group_size: ::Integer, separator: ::String, alphabet: ::EncodedId::Alphabet) -> void
7
22
  def encode: (::Integer | ::Array[::Integer]) -> String
8
- def decode: (::String) -> (nil | ::Array[::Integer])
23
+ def decode: (::String) -> ::Array[::Integer]?
9
24
 
10
25
  @salt: ::String
11
26
  @id_length: ::Integer
@@ -18,21 +33,10 @@ module EncodedId
18
33
  def coder: -> ::EncodedId::ReversibleId
19
34
  end
20
35
 
21
- class Configuration
22
- attr_accessor salt: ::String
23
- attr_accessor group_separator: ::String
24
- attr_accessor character_group_size: ::Integer
25
- attr_accessor alphabet: ::EncodedId::Alphabet
26
- attr_accessor id_length: ::Integer
27
- attr_accessor slugged_id_separator: ::String
28
-
29
- def initialize: () -> void
30
- end
31
-
32
36
  class Salt
33
- def initialize: (untyped klass, ::String salt) -> void
37
+ def initialize: (Class klass, ::String salt) -> void
34
38
 
35
- @klass: untyped
39
+ @klass: Class
36
40
  @salt: ::String
37
41
 
38
42
  def generate!: -> ::String
@@ -52,43 +56,42 @@ module EncodedId
52
56
  class SluggedIdParser
53
57
  def initialize: (::String slugged_id, ?separator: ::String) -> void
54
58
 
55
- attr_reader slug: (nil | ::String)
56
- attr_reader id: (nil | ::String)
59
+ attr_reader slug: ::String?
60
+ attr_reader id: ::String?
57
61
  end
58
62
 
59
- attr_reader self.configuration: Configuration
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
60
70
 
61
- def self.configure: () { (Configuration config) -> void } -> void
71
+ interface _ActiveRecordFinderMethod
72
+ def find_by: (*untyped) -> (nil | untyped)
73
+ end
62
74
 
63
- module WithEncodedId
64
- module ClassMethods
65
- def find_by_encoded_id: (::String slugged_encoded_id, ?with_id: ::Symbol?) -> (nil | untyped)
66
- def find_by_encoded_id!: (::String slugged_encoded_id, ?with_id: ::Symbol?) -> untyped
67
- def where_encoded_id: (::String slugged_encoded_id) -> untyped
68
- def encode_encoded_id: (untyped id, ?::Hash[::Symbol, untyped] options) -> ::String
69
- def decode_encoded_id: (::String slugged_encoded_id, ?::Hash[::Symbol, untyped] options) -> (nil | ::Array[::Integer])
70
- def encoded_id_salt: () -> ::String
71
- def encoded_id_parser: (::String slugged_encoded_id) -> ::EncodedId::Rails::SluggedIdParser
72
- def encoded_id_coder: (?::Hash[::Symbol, untyped] options) -> ::EncodedId::Rails::Coder
73
-
74
- # FIXME: Methods defined on AR, how to tell steep that this will be composed with AR?
75
- def where: (*untyped) -> untyped
76
- def find: (*untyped) -> (nil | untyped)
77
- def find!: (*untyped) -> untyped
78
- def find_by: (*untyped) -> (nil | untyped)
79
- def find_by!: (*untyped) -> untyped
80
- end
81
-
82
- # FIXME: steep doesnt understand that methods defined in ClassMethods will be added to the class, hence
83
- # the duplication here
84
- def self.find_by_encoded_id: (::String slugged_encoded_id, ?with_id: ::Symbol?) -> (nil | untyped)
85
- def self.find_by_encoded_id!: (::String slugged_encoded_id, ?with_id: ::Symbol?) -> untyped
86
- def self.where_encoded_id: (::String slugged_encoded_id) -> untyped
87
- def self.encode_encoded_id: (untyped id, ?::Hash[::Symbol, untyped] options) -> ::String
88
- def self.decode_encoded_id: (::String slugged_encoded_id, ?::Hash[::Symbol, untyped] options) -> (nil | ::Array[::Integer])
89
- def self.encoded_id_salt: () -> ::String
90
- def self.encoded_id_parser: (::String slugged_encoded_id) -> ::EncodedId::Rails::SluggedIdParser
91
- def self.encoded_id_coder: (?::Hash[::Symbol, untyped] options) -> ::EncodedId::Rails::Coder
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 Model : 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,20 @@ 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
102
+ end
99
103
 
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
+ interface _ActiveRecordToParam
105
+ def to_param: () -> ::String
106
+ end
107
+
108
+ module PathParam : Model, _ActiveRecordToParam
109
+ end
110
+
111
+ module SluggedPathParam : Model, _ActiveRecordToParam
104
112
  end
105
113
  end
114
+
115
+ Model: singleton(Rails::Model)
116
+ PathParam: singleton(Rails::PathParam)
117
+ SluggedPathParam: singleton(Rails::SluggedPathParam)
106
118
  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.0
4
+ version: 0.6.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-18 00:00:00.000000000 Z
11
+ date: 2022-12-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -84,11 +84,16 @@ 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/model.rb
90
+ - lib/encoded_id/rails/path_param.rb
91
+ - lib/encoded_id/rails/query_methods.rb
87
92
  - lib/encoded_id/rails/salt.rb
88
93
  - lib/encoded_id/rails/slugged_id.rb
89
94
  - lib/encoded_id/rails/slugged_id_parser.rb
95
+ - lib/encoded_id/rails/slugged_path_param.rb
90
96
  - lib/encoded_id/rails/version.rb
91
- - lib/encoded_id/rails/with_encoded_id.rb
92
97
  - lib/generators/encoded_id/rails/USAGE
93
98
  - lib/generators/encoded_id/rails/install_generator.rb
94
99
  - lib/generators/encoded_id/rails/templates/encoded_id.rb
@@ -1,100 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "active_record"
4
- require "encoded_id"
5
-
6
- module EncodedId
7
- module Rails
8
- module WithEncodedId
9
- def self.included(base)
10
- base.extend(ClassMethods)
11
- end
12
-
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
- def encoded_id
76
- @encoded_id ||= self.class.encode_encoded_id(id)
77
- end
78
-
79
- def slugged_encoded_id(with: :name_for_encoded_id_slug)
80
- @slugged_encoded_id ||= EncodedId::Rails::SluggedId.new(
81
- self,
82
- slug_method: with,
83
- id_method: :encoded_id,
84
- separator: EncodedId::Rails.configuration.slugged_id_separator
85
- ).slugged_id
86
- end
87
-
88
- # By default slug calls `name` if it exists or returns class name
89
- 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
- class_name = self.class.name
95
- raise StandardError, "No name or class name found, cannot create a slug" if !class_name || class_name.blank?
96
- class_name.underscore
97
- end
98
- end
99
- end
100
- end