random_unique_id 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/random_unique_id.rb +18 -16
- data/lib/random_unique_id/version.rb +1 -1
- metadata +2 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ee955fa4eb84ebac6b2096ecc5c4324088e1fc3f
|
4
|
+
data.tar.gz: 00a2a57af285376857ead45e3c1e9445f93bbbbf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 81c9840b8ae51892e18205b0e80a7e9e2f7e8d372aaa68308107d553ec8d1458c065a2822285856d000634827767c0a81fe82220441568aa24797aaa18cb10ea
|
7
|
+
data.tar.gz: 644037903b77dbd52c50bda634e3dcddd0393ec3eb6a9db7d64a6a0da72fc3c43ef5bf856060fbf305b47600054414762652441e3365776d475188c6aeca7f72
|
data/lib/random_unique_id.rb
CHANGED
@@ -32,25 +32,14 @@ module RandomUniqueId
|
|
32
32
|
define_rid_method = attrs[1].try(:delete, :rid)
|
33
33
|
super.tap do
|
34
34
|
if define_rid_method != false
|
35
|
-
|
36
|
-
rel = reflections[
|
35
|
+
relationship_name = attrs[0]
|
36
|
+
rel = reflections[relationship_name]
|
37
37
|
|
38
38
|
return if rel.options[:polymorphic] # If we don't know the class, we cannot find the record by rid.
|
39
39
|
|
40
|
-
class_name = rel.options[:class_name] ||
|
41
|
-
|
42
|
-
|
43
|
-
if klass.attribute_names.include? "rid"
|
44
|
-
define_method("#{rel_name}_rid") do
|
45
|
-
self.send(rel_name).try(:rid)
|
46
|
-
end
|
47
|
-
|
48
|
-
define_method("#{rel_name}_rid=") do |rid|
|
49
|
-
record = klass.find_by_rid(rid)
|
50
|
-
self.send("#{rel_name}=", record)
|
51
|
-
record
|
52
|
-
end
|
53
|
-
end
|
40
|
+
class_name = rel.options[:class_name] || relationship_name.to_s.classify
|
41
|
+
related_class = class_name.constantize
|
42
|
+
define_rid_accessors(related_class, relationship_name) if related_class.attribute_names.include? "rid"
|
54
43
|
end
|
55
44
|
end
|
56
45
|
end
|
@@ -80,6 +69,19 @@ module RandomUniqueId
|
|
80
69
|
end
|
81
70
|
end
|
82
71
|
|
72
|
+
private
|
73
|
+
|
74
|
+
def define_rid_accessors(related_class, relationship_name)
|
75
|
+
define_method("#{relationship_name}_rid") do
|
76
|
+
self.send(relationship_name).try(:rid)
|
77
|
+
end
|
78
|
+
|
79
|
+
define_method("#{relationship_name}_rid=") do |rid|
|
80
|
+
record = related_class.find_by_rid(rid)
|
81
|
+
self.send("#{relationship_name}=", record)
|
82
|
+
record
|
83
|
+
end
|
84
|
+
end
|
83
85
|
end
|
84
86
|
|
85
87
|
def generate_random_unique_id(n=5, field="rid")
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: random_unique_id
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- J. Pablo Fernández
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-05-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -85,4 +85,3 @@ summary: Generate random but unique ids for your active record records.
|
|
85
85
|
test_files:
|
86
86
|
- test/random_unique_id_test.rb
|
87
87
|
- test/test_helper.rb
|
88
|
-
has_rdoc:
|