encoded_id-rails 0.5.0 → 0.6.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6cfa444903c74b47cf55b4c8aa45d862f6eecbe843ff0af9b987b713c4b226d4
4
- data.tar.gz: 7f530fdd9cd3b8a83a308f45e236c92c2bbdaae394cc8b455178ce1ff3de9802
3
+ metadata.gz: 599b4c236c59881b00eefbe0feaf4fa6f1a04015573ba5d807f041ba2932e602
4
+ data.tar.gz: ff6ee760d956606accfb98a75be00cf04a545695e0dcce56e9eb271b1d824fee
5
5
  SHA512:
6
- metadata.gz: e8e9be56253a8956f1a850905c6a9c53e33f7593d419c7348a158e8ff76e3a0285319c116b8a81bfb22d964ceec6aac85fcbe128b3a4ad8265b0b790be27b83b
7
- data.tar.gz: bc63195e7d397c41350b80783526d1733e6109ddc9cd9cbace00d827ff95b19046b287030436da09e359bb3f92774efe702bc17dc4fea006192748463c5eb9b7
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"
@@ -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)
@@ -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.0"
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,7 +1,7 @@
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.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stephen Ierodiaconou
@@ -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