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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5f1c4b031d065a8bec8f97393e82ad781fc33aee
4
- data.tar.gz: 1e0fc4defa8a5dc2d6bef5f04739b23372ace1a0
3
+ metadata.gz: ee955fa4eb84ebac6b2096ecc5c4324088e1fc3f
4
+ data.tar.gz: 00a2a57af285376857ead45e3c1e9445f93bbbbf
5
5
  SHA512:
6
- metadata.gz: 76166e35364c68b99a623c9018efc28da9af99026d4a7f69a4886fcf61e1ce82604c59322f969c6d0c657a543bd2994afa004471dc720b43bd4391658a3c08e7
7
- data.tar.gz: e32b66eebcec4dc4649153f2fb6be5235b8868a5dd3613426969feb29b7a8e7e484735cef743a3a465a6dedd7257bdda19cef60eda0e4c43d6d90b29a46ec643
6
+ metadata.gz: 81c9840b8ae51892e18205b0e80a7e9e2f7e8d372aaa68308107d553ec8d1458c065a2822285856d000634827767c0a81fe82220441568aa24797aaa18cb10ea
7
+ data.tar.gz: 644037903b77dbd52c50bda634e3dcddd0393ec3eb6a9db7d64a6a0da72fc3c43ef5bf856060fbf305b47600054414762652441e3365776d475188c6aeca7f72
@@ -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
- rel_name = attrs[0]
36
- rel = reflections[rel_name]
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] || rel_name.to_s.classify
41
- klass = class_name.constantize
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")
@@ -2,5 +2,5 @@
2
2
  # Copyright © 2013, 2014, Watu
3
3
 
4
4
  module RandomUniqueId
5
- VERSION = "0.2.0"
5
+ VERSION = "0.2.1"
6
6
  end
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.0
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-04-05 00:00:00.000000000 Z
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: