uniqable 0.3.2 → 0.3.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/uniqable.rb +14 -6
- data/lib/uniqable/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a938de6230632eef5187ee27088c417905c36a9cb3fa01e040890d3e1c55de40
|
4
|
+
data.tar.gz: 7a3fc11f03cfd858431d66805b5b3d22ee567e045bcdeec600d0149fe9af5637
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7d750df042075f052d51023c04aa3ff5fc6df91d653abf13d1414d56ab73b0772862669e5377576f1f72c738b397a0fc5ddb214192effbd16fff649fcd9c9638
|
7
|
+
data.tar.gz: 6656b9555fcf0ff34255cb70b323c829e7965a694bf2e624fc3a68cfba51fb886f68af5f9c7dd71c2501706385c22652aee7c78e61fb8cd83cb5ec6620b931ab
|
data/lib/uniqable.rb
CHANGED
@@ -33,10 +33,10 @@ module Uniqable
|
|
33
33
|
|
34
34
|
# @return [self]
|
35
35
|
def where_uniqable(uid)
|
36
|
-
|
37
|
-
|
38
|
-
|
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
|
-
|
65
|
-
|
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
|
data/lib/uniqable/version.rb
CHANGED