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 CHANGED
@@ -1 +1 @@
1
- 0.3.4
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.3.4"
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-07}
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 LaserLemon
4
- module HasToken
5
- def self.included(base)
6
- base.extend ClassMethods
7
- end
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
- module ClassMethods
10
- def has_token(*args)
11
- options = args.extract_options!.symbolize_keys
12
- options[:column] = (args.first || :token)
13
- options.reverse_merge!(
14
- :length => 6,
15
- :characters => [('a'..'z'), ('A'..'Z'), ('0'..'9')].map(&:to_a).sum,
16
- :constructor => proc(&:has_token_value),
17
- :to_param => false,
18
- :readonly => true
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
- module InstanceMethods
36
- def has_token_options
37
- self.class.has_token_options
33
+ if options[:readonly]
34
+ attr_readonly(options[:column])
38
35
  end
39
36
 
40
- def has_token_value
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 set_token
45
- begin
46
- token_value = has_token_options[:constructor].call(self)
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
- build_global_token(:value => token_value)
50
- write_attribute(has_token_options[:column], token_value)
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, LaserLemon::HasToken)
72
+ ActiveRecord::Base.send(:include, HasToken)
data/lib/token.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  class Token < ActiveRecord::Base
2
2
  belongs_to :parent, :polymorphic => true
3
3
 
4
- named_scope :for, lambda{|k| {:conditions => {:parent_type => k.to_s.classify}} }
4
+ validates_uniqueness_of :value
5
5
 
6
6
  def self.get(value)
7
7
  find_by_value(value).try(:parent)
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.3.4
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-07 00:00:00 -04:00
12
+ date: 2009-10-22 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies: []
15
15