encoded_id-rails 0.5.0 → 0.6.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6cfa444903c74b47cf55b4c8aa45d862f6eecbe843ff0af9b987b713c4b226d4
4
- data.tar.gz: 7f530fdd9cd3b8a83a308f45e236c92c2bbdaae394cc8b455178ce1ff3de9802
3
+ metadata.gz: 245fb340a94ca73cff62e2e466ee1f6a8f10195b1df94e0c1d37334c66168925
4
+ data.tar.gz: ffdecb93e8b75ddd0e63da9f5c3d686f80e986fb73035c27bb57882c1982723d
5
5
  SHA512:
6
- metadata.gz: e8e9be56253a8956f1a850905c6a9c53e33f7593d419c7348a158e8ff76e3a0285319c116b8a81bfb22d964ceec6aac85fcbe128b3a4ad8265b0b790be27b83b
7
- data.tar.gz: bc63195e7d397c41350b80783526d1733e6109ddc9cd9cbace00d827ff95b19046b287030436da09e359bb3f92774efe702bc17dc4fea006192748463c5eb9b7
6
+ metadata.gz: c1add38768e797d2e57270a600bd84717141edcdd955c4e5b91c38cfc9585f8aa207099d737bae6f68d2f1364aace46877f50ea014ab2063a44fa718a2ace98a
7
+ data.tar.gz: 47629d384bca18518bbf69645c9e0e8c4ddb0fb1bd064fa006593e3d7ac32d78951486ff442ad6b0d7012a4fbdeb8c79268998e518d24a4435caa7dc9226ebeb
data/CHANGELOG.md CHANGED
@@ -1,5 +1,36 @@
1
1
  ## [Unreleased]
2
2
 
3
- ## [0.1.0] - 2022-10-11
3
+ ## [0.6.1] - 2023-02-09
4
+
5
+ - Fix `#encoded_id` to return nil if `#id` is nil
6
+ - Ensure `encoded_id` memoization is cleared when record is duplicated, or id changes
7
+
8
+ ## [0.6.0] - 2022-12-21
9
+
10
+ - Rename mixin to `Model`
11
+ - Introduce optional mixins for overriding `#to_param`
12
+
13
+ ## [0.5.0] - 2022-12-21
14
+
15
+ - `name_for_encoded_id_slug` no longer uses the return value from name but rather just uses the `class` `name`.
16
+ - If you want to change the name used in the slug, override `name_for_encoded_id_slug`
17
+
18
+ ## [0.4.0] - 2022-12-18
19
+
20
+ - Refactor internals, remove any methods not actually related to creating `encoded_id`, (eg `slugged_id` was removed).
21
+
22
+ ## [0.3.1] - 2022-12-15
23
+
24
+ - Fix default config
25
+
26
+ ## [0.3.0] - 2022-12-15
27
+
28
+ - Updates gem `encoded_id` dependency and fixes configuration
29
+
30
+ ## [0.2.0] - 2022-12-14
31
+
32
+ - No notes...
33
+
34
+ ## [0.1.0] - 2022-11-17
4
35
 
5
36
  - 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"
@@ -5,7 +5,7 @@ require "encoded_id"
5
5
 
6
6
  module EncodedId
7
7
  module Rails
8
- module WithEncodedId
8
+ module Model
9
9
  def self.included(base)
10
10
  base.extend(EncoderMethods)
11
11
  base.extend(FinderMethods)
@@ -13,11 +13,15 @@ module EncodedId
13
13
  end
14
14
 
15
15
  def encoded_id
16
- @encoded_id ||= self.class.encode_encoded_id(id)
16
+ return unless id
17
+ return @encoded_id if defined?(@encoded_id) && !id_changed?
18
+ @encoded_id = self.class.encode_encoded_id(id)
17
19
  end
18
20
 
19
21
  def slugged_encoded_id(with: :name_for_encoded_id_slug)
20
- @slugged_encoded_id ||= EncodedId::Rails::SluggedId.new(
22
+ return unless id
23
+ return @slugged_encoded_id if defined?(@slugged_encoded_id) && !id_changed?
24
+ @slugged_encoded_id = EncodedId::Rails::SluggedId.new(
21
25
  self,
22
26
  slug_method: with,
23
27
  id_method: :encoded_id,
@@ -31,6 +35,14 @@ module EncodedId
31
35
  raise StandardError, "Class must have a `name`, cannot create a slug" if !class_name || class_name.blank?
32
36
  class_name.underscore
33
37
  end
38
+
39
+ # When duplicating an ActiveRecord object, we want to reset the memoized encoded_id
40
+ def dup
41
+ super.tap do |new_record|
42
+ new_record.instance_variable_set(:@encoded_id, nil)
43
+ new_record.instance_variable_set(:@slugged_encoded_id, nil)
44
+ end
45
+ end
34
46
  end
35
47
  end
36
48
  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,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.5.0"
5
+ VERSION = "0.6.1"
6
6
  end
7
7
  end
@@ -9,7 +9,9 @@ require_relative "rails/salt"
9
9
  require_relative "rails/encoder_methods"
10
10
  require_relative "rails/query_methods"
11
11
  require_relative "rails/finder_methods"
12
- require_relative "rails/with_encoded_id"
12
+ require_relative "rails/path_param"
13
+ require_relative "rails/slugged_path_param"
14
+ require_relative "rails/model"
13
15
 
14
16
  module EncodedId
15
17
  module Rails
@@ -27,5 +29,7 @@ module EncodedId
27
29
  end
28
30
 
29
31
  # Expose directly on EncodedId
30
- WithEncodedId = Rails::WithEncodedId
32
+ Model = Rails::Model
33
+ PathParam = Rails::PathParam
34
+ SluggedPathParam = Rails::SluggedPathParam
31
35
  end
@@ -2,6 +2,21 @@ 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
@@ -18,17 +33,6 @@ 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
37
  def initialize: (Class klass, ::String salt) -> void
34
38
 
@@ -56,10 +60,6 @@ module EncodedId
56
60
  attr_reader id: ::String?
57
61
  end
58
62
 
59
- attr_reader self.configuration: Configuration
60
-
61
- def self.configure: () { (Configuration config) -> void } -> void
62
-
63
63
  module EncoderMethods
64
64
  def encode_encoded_id: (untyped id, ?::Hash[::Symbol, untyped] options) -> ::String
65
65
  def decode_encoded_id: (::String slugged_encoded_id, ?::Hash[::Symbol, untyped] options) -> ::Array[::Integer]?
@@ -85,7 +85,7 @@ module EncodedId
85
85
  def where_encoded_id: (::String slugged_encoded_id) -> untyped
86
86
  end
87
87
 
88
- module WithEncodedId : ActiveRecord::Base
88
+ module Model : ActiveRecord::Base
89
89
  extend ActiveRecord::FinderMethods
90
90
  extend ActiveRecord::QueryMethods
91
91
 
@@ -100,5 +100,19 @@ module EncodedId
100
100
  def slugged_encoded_id: (?with: ::Symbol) -> ::String
101
101
  def name_for_encoded_id_slug: () -> ::String
102
102
  end
103
+
104
+ interface _ActiveRecordToParam
105
+ def to_param: () -> ::String
106
+ end
107
+
108
+ module PathParam : Model, _ActiveRecordToParam
109
+ end
110
+
111
+ module SluggedPathParam : Model, _ActiveRecordToParam
112
+ end
103
113
  end
114
+
115
+ Model: singleton(Rails::Model)
116
+ PathParam: singleton(Rails::PathParam)
117
+ SluggedPathParam: singleton(Rails::SluggedPathParam)
104
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.5.0
4
+ version: 0.6.1
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-21 00:00:00.000000000 Z
11
+ date: 2023-02-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -86,12 +86,14 @@ files:
86
86
  - lib/encoded_id/rails/configuration.rb
87
87
  - lib/encoded_id/rails/encoder_methods.rb
88
88
  - lib/encoded_id/rails/finder_methods.rb
89
+ - lib/encoded_id/rails/model.rb
90
+ - lib/encoded_id/rails/path_param.rb
89
91
  - lib/encoded_id/rails/query_methods.rb
90
92
  - lib/encoded_id/rails/salt.rb
91
93
  - lib/encoded_id/rails/slugged_id.rb
92
94
  - lib/encoded_id/rails/slugged_id_parser.rb
95
+ - lib/encoded_id/rails/slugged_path_param.rb
93
96
  - lib/encoded_id/rails/version.rb
94
- - lib/encoded_id/rails/with_encoded_id.rb
95
97
  - lib/generators/encoded_id/rails/USAGE
96
98
  - lib/generators/encoded_id/rails/install_generator.rb
97
99
  - lib/generators/encoded_id/rails/templates/encoded_id.rb