uidable 1.0.3 → 1.1.0
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 +4 -4
- data/README.md +1 -1
- data/lib/uidable/version.rb +1 -1
- data/lib/uidable.rb +53 -35
- metadata +3 -4
- data/.travis.yml +0 -14
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 049db3fd13da41c90f3f26522a6a3fbe7ed7fc0a
|
4
|
+
data.tar.gz: e229b43eedd837dfa33a2fb7add8bf6e76cfbe15
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 666c26af8fcd4071393c5ad29faa6b3a5a51d9dca8ecefe899ad2683cad3fc199ff300eb51e826e1c82e6aad7ee26519214ef5cec554ad7610cac287bdb616f2
|
7
|
+
data.tar.gz: 6dc71358a57cf9dff53d8e5cb67d846e677587382d55244e2b1b4a592e4ebbe960d985d431ca787c8a5c750be498a1b24d414e3357a5aadb20248686ff8a6ba2
|
data/README.md
CHANGED
@@ -104,7 +104,7 @@ If the option `scope: true` is given, a scope `with_uid` is created and you can
|
|
104
104
|
|
105
105
|
## Redefine Uid Generation
|
106
106
|
|
107
|
-
You can override `
|
107
|
+
You can override `gen_<name>` method in your class/model if you want to generate your own uid. Here is an example:
|
108
108
|
|
109
109
|
require `random_token`
|
110
110
|
|
data/lib/uidable/version.rb
CHANGED
data/lib/uidable.rb
CHANGED
@@ -5,6 +5,24 @@ module Uidable
|
|
5
5
|
|
6
6
|
def self.included(base)
|
7
7
|
base.extend ClassMethods
|
8
|
+
unless defined?(::ActiveRecord::Base) && base < ::ActiveRecord::Base
|
9
|
+
base.prepend InitUid
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
def uidable_cols_init
|
15
|
+
self.class.uidable_cols.each do |col|
|
16
|
+
instance_variable_set("@#{col}", send("gen_#{col}"))
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
module InitUid
|
22
|
+
def initialize
|
23
|
+
uidable_cols_init
|
24
|
+
super
|
25
|
+
end
|
8
26
|
end
|
9
27
|
|
10
28
|
module ClassMethods
|
@@ -16,48 +34,48 @@ module Uidable
|
|
16
34
|
uniqueness: true,
|
17
35
|
set_to_param: false,
|
18
36
|
scope: false)
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
37
|
+
unless uidable_cols.include?(uid_name.to_sym)
|
38
|
+
uidable_cols << uid_name.to_sym
|
39
|
+
mod = Module.new
|
40
|
+
mod.module_eval <<-RUBY, __FILE__, __LINE__ + 1
|
41
|
+
def self.included(base)
|
42
|
+
if defined?(::ActiveRecord::Base) && base < ::ActiveRecord::Base
|
43
|
+
base.before_validation :uidable_assign_#{uid_name}, on: :create
|
44
|
+
#{ scope ? "base.scope :'with_#{uid_name}', -> (uid) { base.where(:'#{uid_name}' => uid) }" : "" }
|
45
|
+
#{ set_to_param ? "base.include SetToParam#{uid_name}" : "" }
|
46
|
+
#{ read_only ? "base.attr_readonly :'#{uid_name}'" : "" }
|
47
|
+
else
|
48
|
+
#{ read_only ? "attr_reader :'#{uid_name}'" : "attr_accessor :'#{uid_name}'" }
|
49
|
+
end
|
50
|
+
if base.respond_to?(:validates)
|
51
|
+
#{ presence ? "base.validates :'#{uid_name}', presence: true" : "" }
|
52
|
+
#{ uniqueness ? "base.validates :'#{uid_name}', uniqueness: true" : "" }
|
53
|
+
end
|
30
54
|
end
|
31
|
-
if base.respond_to?(:validates)
|
32
|
-
#{ presence ? "base.validates :'#{uid_name}', presence: true" : "" }
|
33
|
-
#{ uniqueness ? "base.validates :'#{uid_name}', uniqueness: true" : "" }
|
34
|
-
end
|
35
|
-
end
|
36
55
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
56
|
+
module SetToParam#{uid_name}
|
57
|
+
def to_param
|
58
|
+
self.#{uid_name}
|
59
|
+
end
|
41
60
|
end
|
42
|
-
end
|
43
61
|
|
44
|
-
|
45
|
-
def to_param
|
46
|
-
self.#{uid_name}
|
47
|
-
end
|
48
|
-
end
|
62
|
+
private
|
49
63
|
|
50
|
-
|
64
|
+
def uidable_assign_#{uid_name}
|
65
|
+
self.#{uid_name} = gen_#{uid_name}
|
66
|
+
end
|
51
67
|
|
52
|
-
|
53
|
-
|
54
|
-
|
68
|
+
def gen_#{uid_name}
|
69
|
+
Array.new(#{uid_size}){[*'a'..'z', *'0'..'9'].sample}.join
|
70
|
+
end
|
71
|
+
RUBY
|
72
|
+
include mod
|
73
|
+
end
|
74
|
+
end
|
55
75
|
|
56
|
-
|
57
|
-
|
58
|
-
end
|
59
|
-
RUBY
|
60
|
-
include mod
|
76
|
+
def uidable_cols
|
77
|
+
@uidable_cols ||= []
|
61
78
|
end
|
62
79
|
end
|
80
|
+
|
63
81
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: uidable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sibevin Wang
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-10-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -49,7 +49,6 @@ extra_rdoc_files: []
|
|
49
49
|
files:
|
50
50
|
- ".gitignore"
|
51
51
|
- ".rspec"
|
52
|
-
- ".travis.yml"
|
53
52
|
- CODE_OF_CONDUCT.md
|
54
53
|
- Gemfile
|
55
54
|
- LICENSE.txt
|
@@ -79,7 +78,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
79
78
|
version: '0'
|
80
79
|
requirements: []
|
81
80
|
rubyforge_project:
|
82
|
-
rubygems_version: 2.4.
|
81
|
+
rubygems_version: 2.4.8
|
83
82
|
signing_key:
|
84
83
|
specification_version: 4
|
85
84
|
summary: Create the uid attribute in your model or class.
|