prefixed_ids 1.9.0 → 1.9.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1bac5b304f58877aefa5452f7e1532a65071b7f2eeadd6347a144326d86737cd
4
- data.tar.gz: 91facf11c39db364b50ca88138429308436328f22446b19d8d6a6e2c835c51d8
3
+ metadata.gz: 9f4a7dba39c3feff4c04155095501305cda1f83649a3084587276762bba4bf43
4
+ data.tar.gz: 94f8821fbe8f4b6144d9f1d88fad5c86d91f6b92c69718d5584214724d07d89e
5
5
  SHA512:
6
- metadata.gz: 48048178e689a688bae0f048a8b08fe246dc2b60de2a339e3458117d2945b15757b1c1888b655e2306883a78e345256cf23f565b29a94f72972cf4b8c7043883
7
- data.tar.gz: 5ae3f646bd1df6a314e15c12a732257b1eb242058ec7775d75a60f9862ff5034e20d2eed190d88e4190456109d76754e4d56e7c970ef2a0e229d2d4aea9a1156
6
+ metadata.gz: 800ac6f9df59cfe8d75a9606e66ee926f0d03eff86360d3ff23b1f1286a4746ca04d865e89ac2fabd377d0632310abbcfc7b37980ef2b595d0eb8e6f14ef0a3d
7
+ data.tar.gz: 9c90c492213661c063cdbe3510369fee96958fc00845a18cb6c831f3741bdd2a6ee52ebf6b94ceb1d2a55ee9fdd769b0526dddd9e4a8a4af8979604a2cbbd30b
@@ -5,6 +5,7 @@ module PrefixedIds
5
5
  TOKEN = 123
6
6
 
7
7
  def initialize(model, prefix, salt: PrefixedIds.salt, minimum_length: PrefixedIds.minimum_length, alphabet: PrefixedIds.alphabet, delimiter: PrefixedIds.delimiter, **options)
8
+ @model = model
8
9
  @prefix = prefix.to_s
9
10
  @delimiter = delimiter.to_s
10
11
  @hashids = Hashids.new("#{model.table_name}#{salt}", minimum_length, alphabet)
@@ -43,8 +44,14 @@ module PrefixedIds
43
44
  []
44
45
  end
45
46
 
47
+ # The number of IDs must match the primary key, so a hash of several IDs can't be used on a single column primary key
46
48
  def valid?(decoded_hashid)
47
- decoded_hashid.size >= 2 && decoded_hashid.first == TOKEN
49
+ decoded_hashid.first == TOKEN && decoded_hashid.size == primary_key_size + 1
50
+ end
51
+
52
+ # Read lazily since the primary key isn't known when `has_prefix_id` is called
53
+ def primary_key_size
54
+ Array.wrap(@model.primary_key).size
48
55
  end
49
56
  end
50
57
  end
@@ -1,3 +1,3 @@
1
1
  module PrefixedIds
2
- VERSION = "1.9.0"
2
+ VERSION = "1.9.2"
3
3
  end
data/lib/prefixed_ids.rb CHANGED
@@ -166,13 +166,11 @@ module PrefixedIds
166
166
  super.tap { |r| r.extend ClassMethods }
167
167
  end
168
168
 
169
- def belongs_to(*args, **options, &block)
170
- association = super
169
+ def belongs_to(name, *args, **options, &block)
170
+ super
171
171
 
172
- name = args.first
173
- reflection = association[name] || association[name.to_s]
174
-
175
- return association if reflection.polymorphic?
172
+ reflection = reflect_on_association(name)
173
+ return if reflection.polymorphic?
176
174
 
177
175
  # Skip helpers if the associated class can't be loaded yet, so Rails only raises when the association is used
178
176
  klass = begin
@@ -180,7 +178,7 @@ module PrefixedIds
180
178
  rescue NameError
181
179
  nil
182
180
  end
183
- return association if klass.nil? || klass._prefix_id.blank?
181
+ return if klass.nil? || klass._prefix_id.blank?
184
182
 
185
183
  generated_association_methods.class_eval <<-CODE, __FILE__, __LINE__ + 1
186
184
  def #{name}_prefix_id
@@ -192,8 +190,6 @@ module PrefixedIds
192
190
  send("#{reflection.foreign_key}=", decoded_id)
193
191
  end
194
192
  CODE
195
-
196
- association
197
193
  end
198
194
 
199
195
  def has_many(*args, &block)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prefixed_ids
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.9.0
4
+ version: 1.9.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Oliver