has_token 0.3.4 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/has_token.gemspec +2 -2
- data/lib/has_token.rb +57 -41
- data/lib/token.rb +1 -1
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.4.0
|
data/has_token.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{has_token}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.4.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["laserlemon"]
|
12
|
-
s.date = %q{2009-10-
|
12
|
+
s.date = %q{2009-10-22}
|
13
13
|
s.description = %q{Generate unique tokens on your ActiveRecord models}
|
14
14
|
s.email = %q{steve@laserlemon.com}
|
15
15
|
s.extra_rdoc_files = [
|
data/lib/has_token.rb
CHANGED
@@ -1,56 +1,72 @@
|
|
1
1
|
require 'token'
|
2
2
|
|
3
|
-
module
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
3
|
+
module HasToken
|
4
|
+
def self.included(base)
|
5
|
+
base.extend ClassMethods
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
def has_token(*args)
|
10
|
+
options = args.extract_options!.symbolize_keys
|
11
|
+
options.merge!(
|
12
|
+
:column => args.first || :token
|
13
|
+
).reverse_merge!(
|
14
|
+
:length => 6,
|
15
|
+
:characters => [('a'..'z'), ('A'..'Z'), ('0'..'9')].map(&:to_a).sum,
|
16
|
+
:constructor => proc(&:generate_token),
|
17
|
+
:to_param => false,
|
18
|
+
:readonly => true
|
19
|
+
)
|
8
20
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
:
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
)
|
20
|
-
|
21
|
-
cattr_accessor :has_token_options
|
22
|
-
return(false) if self.has_token_options.present?
|
23
|
-
self.has_token_options = options
|
24
|
-
|
25
|
-
attr_readonly(options[:column]) if options[:readonly]
|
26
|
-
define_method(:to_param){ read_attribute(options[:column]) } if options[:to_param]
|
27
|
-
|
28
|
-
has_one :global_token, :class_name => 'Token', :as => :parent, :autosave => true, :dependent => :nullify
|
29
|
-
|
30
|
-
include InstanceMethods
|
31
|
-
before_create :set_token
|
21
|
+
class_inheritable_accessor :has_token_options
|
22
|
+
return false unless self.has_token_options.nil?
|
23
|
+
self.has_token_options = options
|
24
|
+
|
25
|
+
if options[:to_param]
|
26
|
+
def to_param_with_token
|
27
|
+
read_attribute(options[:column])
|
28
|
+
end
|
29
|
+
|
30
|
+
alias_method_chain :to_param, :token
|
32
31
|
end
|
33
|
-
end
|
34
32
|
|
35
|
-
|
36
|
-
|
37
|
-
self.class.has_token_options
|
33
|
+
if options[:readonly]
|
34
|
+
attr_readonly(options[:column])
|
38
35
|
end
|
39
36
|
|
40
|
-
|
37
|
+
has_one :global_token, :class_name => 'Token', :as => :parent, :dependent => :nullify
|
38
|
+
|
39
|
+
include InstanceMethods
|
40
|
+
|
41
|
+
before_create :create_token
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
module InstanceMethods
|
46
|
+
private
|
47
|
+
def generate_token
|
41
48
|
Array.new(has_token_options[:length]){ has_token_options[:characters].rand }.join
|
42
49
|
end
|
43
50
|
|
44
|
-
def
|
45
|
-
|
46
|
-
|
47
|
-
end while Token.exists?(:value => token_value)
|
51
|
+
def create_token
|
52
|
+
column = has_token_options[:column]
|
53
|
+
value = read_attribute(column)
|
48
54
|
|
49
|
-
|
50
|
-
|
55
|
+
if value
|
56
|
+
token = build_global_token(:value => value)
|
57
|
+
unless token.save
|
58
|
+
errors.add(column, token.errors.on(:value))
|
59
|
+
return false
|
60
|
+
end
|
61
|
+
else
|
62
|
+
begin
|
63
|
+
value = has_token_options[:constructor].call(self)
|
64
|
+
token = build_global_token(:value => value)
|
65
|
+
end until token.save
|
66
|
+
write_attribute(column, value)
|
67
|
+
end
|
51
68
|
end
|
52
|
-
end
|
53
69
|
end
|
54
70
|
end
|
55
71
|
|
56
|
-
ActiveRecord::Base.send(:include,
|
72
|
+
ActiveRecord::Base.send(:include, HasToken)
|
data/lib/token.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: has_token
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- laserlemon
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-10-
|
12
|
+
date: 2009-10-22 00:00:00 -04:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|