uniqable 0.3.2 → 0.3.3

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: b28ea2e4128fd506d1983847dfde61915acf8550c417fbc5eaba75e11da56986
4
- data.tar.gz: 39ef933406d49dcf040aabf68f2c5469c317d52630dd4f65a52b4a40a993bb97
3
+ metadata.gz: a938de6230632eef5187ee27088c417905c36a9cb3fa01e040890d3e1c55de40
4
+ data.tar.gz: 7a3fc11f03cfd858431d66805b5b3d22ee567e045bcdeec600d0149fe9af5637
5
5
  SHA512:
6
- metadata.gz: 45d7d098218f0071b1373e23c824f119f0c9b03cf5ee097fb668e34653a11328b9d0684c519a6eaeaa9e450d265f34fa3fc4e7035c4989186b00faef987b088e
7
- data.tar.gz: 37802f6ce7ba3f591559a271d597111ca13743b43345b8b37e986426e2360a9e089b032de476567ed444a924864aca22dba1e339b9fb04603b7e48dcf2fac3d2
6
+ metadata.gz: 7d750df042075f052d51023c04aa3ff5fc6df91d653abf13d1414d56ab73b0772862669e5377576f1f72c738b397a0fc5ddb214192effbd16fff649fcd9c9638
7
+ data.tar.gz: 6656b9555fcf0ff34255cb70b323c829e7965a694bf2e624fc3a68cfba51fb886f68af5f9c7dd71c2501706385c22652aee7c78e61fb8cd83cb5ec6620b931ab
@@ -33,10 +33,10 @@ module Uniqable
33
33
 
34
34
  # @return [self]
35
35
  def where_uniqable(uid)
36
- where(
37
- uniqable_fields.map { |r| "#{table_name}.#{r} = :uid" }.join(' OR '),
38
- uid: uid
39
- )
36
+ where_sql = key_uid?(uid) ?
37
+ uniqable_fields.map { |r| "#{table_name}.#{r} = :uid" }.join(' OR ') :
38
+ "#{table_name}.#{self.primary_key} = :uid"
39
+ where(where_sql, uid: uid)
40
40
  end
41
41
 
42
42
  # Find record by one of the uniq field
@@ -54,6 +54,12 @@ module Uniqable
54
54
  def find_uniqable!(uid)
55
55
  where_uniqable(uid).take!
56
56
  end
57
+
58
+ private
59
+
60
+ def key_uid?(uid)
61
+ uid.to_s =~ /\D+/ || uid.to_s.length >= 16
62
+ end
57
63
  end
58
64
 
59
65
  # Generate and set random and uniq field
@@ -61,8 +67,10 @@ module Uniqable
61
67
  def uniqable_uid(field)
62
68
  loop do
63
69
  uniq_code = SecureRandom.hex(8)
64
- send("#{field}=", uniq_code)
65
- break unless self.class.where(field => uniq_code).exists?
70
+ if uniq_code =~ /\D+/
71
+ send("#{field}=", uniq_code)
72
+ break unless self.class.where(field => uniq_code).exists?
73
+ end
66
74
  end
67
75
  end
68
76
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Uniqable
4
- VERSION = '0.3.2'
4
+ VERSION = '0.3.3'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: uniqable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Renat "MpaK" Ibragimov