token_attribute 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/README.md +90 -0
- data/Rakefile +13 -0
- data/TODO.md +3 -0
- data/lib/token_attribute/version.rb +3 -0
- data/lib/token_attribute.rb +53 -0
- data/test/lib/token_attribute_test.rb +82 -0
- data/test/test_helper.rb +45 -0
- data/token_attribute.gemspec +35 -0
- metadata +157 -0
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
# token_attribute [<img src="https://secure.travis-ci.org/fujimura/token_attribute.png"/>](http://travis-ci.org/fujimura/token_attribute)
|
2
|
+
|
3
|
+
Small macro to define unique random token generator for ActiveRecord.
|
4
|
+
|
5
|
+
## How to Use
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
|
9
|
+
## Basic
|
10
|
+
|
11
|
+
class User < ActiveRecord::Base
|
12
|
+
include TokenAttribute
|
13
|
+
token_attribute :access_token
|
14
|
+
end
|
15
|
+
|
16
|
+
user = User.new
|
17
|
+
user.access_token # => nil
|
18
|
+
user.generate_access_token # => 'random string'
|
19
|
+
user.set_access_token # access_token will be set here
|
20
|
+
user.access_token # => 'some unique random string'
|
21
|
+
|
22
|
+
|
23
|
+
## Use own random string generator
|
24
|
+
|
25
|
+
class Coupon < ActiveRecord::Base
|
26
|
+
include TokenAttribute
|
27
|
+
token_attribute :code
|
28
|
+
def generate_code # Override this method
|
29
|
+
'my code'
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
coupon = Coupon.new
|
34
|
+
coupon.set_code
|
35
|
+
coupon.code # => 'my code'
|
36
|
+
|
37
|
+
|
38
|
+
## Scoping
|
39
|
+
|
40
|
+
class Ticket < ActiveRecord::Base
|
41
|
+
include TokenAttribute
|
42
|
+
token_attribute :token, :scope => :title # token will be unique per title
|
43
|
+
end
|
44
|
+
|
45
|
+
|
46
|
+
## Make attributes attr_protected
|
47
|
+
|
48
|
+
class Post < ActiveRecord::Base
|
49
|
+
include TokenAttribute
|
50
|
+
token_attribute :secret_key, :protected => true
|
51
|
+
end
|
52
|
+
|
53
|
+
post = Post.new(:secret_key => 'abcdefg')
|
54
|
+
post.secret_key # => nil
|
55
|
+
```
|
56
|
+
|
57
|
+
## Install
|
58
|
+
|
59
|
+
```
|
60
|
+
$ gem install token_attribute
|
61
|
+
```
|
62
|
+
|
63
|
+
or
|
64
|
+
|
65
|
+
```
|
66
|
+
gem 'token_attribute'
|
67
|
+
```
|
68
|
+
|
69
|
+
## Supported Versions
|
70
|
+
|
71
|
+
Ruby 1.8.7, 1.9.2, REE
|
72
|
+
|
73
|
+
Rails 3.0.x, 3.1.x
|
74
|
+
|
75
|
+
## Motivation
|
76
|
+
|
77
|
+
Someday I felt it's time to stop writing this kind of code in every project.
|
78
|
+
|
79
|
+
|
80
|
+
## Copyright
|
81
|
+
|
82
|
+
(The MIT License)
|
83
|
+
|
84
|
+
Copyright © 2011 Fujimura Daisuke
|
85
|
+
|
86
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
87
|
+
|
88
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
89
|
+
|
90
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
data/TODO.md
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'token_attribute', 'version')
|
2
|
+
require 'active_record'
|
3
|
+
require 'active_support'
|
4
|
+
|
5
|
+
module TokenAttribute
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
|
10
|
+
# Macro-ish method to define token-setter.
|
11
|
+
# #set_#{attribute_name} will be defined to set unique token.
|
12
|
+
#
|
13
|
+
def token_attribute(*args)
|
14
|
+
options = args.extract_options!
|
15
|
+
attribute_names = args
|
16
|
+
|
17
|
+
if options[:protected]
|
18
|
+
attr_protected *attribute_names
|
19
|
+
end
|
20
|
+
|
21
|
+
attribute_names.each do |attribute|
|
22
|
+
|
23
|
+
generator_method_name = "generate_#{attribute}"
|
24
|
+
|
25
|
+
define_method generator_method_name do
|
26
|
+
key = SecureRandom.hex(10)
|
27
|
+
end
|
28
|
+
scope_items = Array.wrap(options[:scope])
|
29
|
+
|
30
|
+
setter_method_name = "set_#{attribute}"
|
31
|
+
|
32
|
+
define_method setter_method_name do
|
33
|
+
|
34
|
+
key = send generator_method_name
|
35
|
+
|
36
|
+
cond = {}
|
37
|
+
scope_items.each do |scope_item|
|
38
|
+
cond[scope_item] = self.send(scope_item)
|
39
|
+
end
|
40
|
+
cond.merge!(attribute => key)
|
41
|
+
|
42
|
+
unless self.class.where(cond).exists?
|
43
|
+
self[attribute] = key
|
44
|
+
else
|
45
|
+
send setter_method_name
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
require File.expand_path('../../test_helper', __FILE__)
|
2
|
+
|
3
|
+
class TokenAttributeTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
setup do
|
6
|
+
@user = User.new
|
7
|
+
end
|
8
|
+
|
9
|
+
teardown do
|
10
|
+
User.delete_all
|
11
|
+
end
|
12
|
+
|
13
|
+
describe '.token_attribute' do
|
14
|
+
test 'can token_attribute-ize multiple attributes' do
|
15
|
+
klass = User.dup
|
16
|
+
klass.class_eval do
|
17
|
+
include TokenAttribute
|
18
|
+
token_attribute :download_ticket, :password_recovery
|
19
|
+
end
|
20
|
+
[:download_ticket, :password_recovery].each do |a|
|
21
|
+
method_name = ('set_' + a.to_s).to_sym
|
22
|
+
assert klass.new.methods.map(&:to_sym).include? method_name
|
23
|
+
end
|
24
|
+
end
|
25
|
+
test 'can make attr_protected with option' do
|
26
|
+
klass = User.dup
|
27
|
+
klass.class_eval do
|
28
|
+
include TokenAttribute
|
29
|
+
token_attribute :download_ticket, :protected => true
|
30
|
+
end
|
31
|
+
assert klass.protected_attributes.include? :download_ticket
|
32
|
+
end
|
33
|
+
test 'can be scoped' do
|
34
|
+
klass = User.dup
|
35
|
+
klass.class_eval do
|
36
|
+
include TokenAttribute
|
37
|
+
token_attribute :download_ticket, :scope => :name
|
38
|
+
before_save :set_download_ticket
|
39
|
+
end
|
40
|
+
one = klass.new :name => 'fujimura'
|
41
|
+
two = klass.new :name => 'tanaka'
|
42
|
+
[one, two].each {|u| stub(u).generate_download_ticket.once { 'a' } }
|
43
|
+
[one, two].each {|u| u.save }
|
44
|
+
assert one.download_ticket == two.download_ticket
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
context 'class with access_token as token_attribute' do
|
49
|
+
|
50
|
+
describe '#generate_access_token' do
|
51
|
+
test 'returns random string' do
|
52
|
+
stub(SecureRandom).hex(10) { 'abcde' }
|
53
|
+
assert_equal @user.generate_access_token, 'abcde'
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
describe '#set_access_token' do
|
58
|
+
test 'sets access_token' do
|
59
|
+
@user.set_access_token
|
60
|
+
assert @user.access_token != nil
|
61
|
+
end
|
62
|
+
test 'set again if it duplicates' do
|
63
|
+
dup = 'duplicating'
|
64
|
+
User.create :access_token => dup
|
65
|
+
uniq = 'notduplicating'
|
66
|
+
mock(@user).generate_access_token.times(1) { dup }
|
67
|
+
mock(@user).generate_access_token.times(1) { uniq }
|
68
|
+
@user.set_access_token
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
73
|
+
|
74
|
+
describe 'class with its own random string generator' do
|
75
|
+
test 'will use it instead of TokenAttribute#generate_random_string' do
|
76
|
+
coupon = Coupon.new
|
77
|
+
coupon.set_code
|
78
|
+
assert coupon.code == 'my code'
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require File.expand_path('../../lib/token_attribute', __FILE__)
|
3
|
+
require 'minitest/unit'
|
4
|
+
require 'contest'
|
5
|
+
require 'rr'
|
6
|
+
require 'ruby-debug'
|
7
|
+
require 'turn'
|
8
|
+
Test::Unit::TestCase.send :include, RR::Adapters::TestUnit
|
9
|
+
|
10
|
+
ActiveRecord::Base.configurations = {
|
11
|
+
'test' => {
|
12
|
+
:adapter => 'sqlite3',
|
13
|
+
:database => ':memory:'
|
14
|
+
}
|
15
|
+
}
|
16
|
+
ActiveRecord::Base.establish_connection('test')
|
17
|
+
|
18
|
+
class User < ActiveRecord::Base
|
19
|
+
include TokenAttribute
|
20
|
+
token_attribute :access_token
|
21
|
+
end
|
22
|
+
|
23
|
+
class Coupon < ActiveRecord::Base
|
24
|
+
include TokenAttribute
|
25
|
+
token_attribute :code
|
26
|
+
def generate_code
|
27
|
+
'my code'
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
class CreateAllTables < ActiveRecord::Migration
|
32
|
+
def self.up
|
33
|
+
create_table(:users) do |t|
|
34
|
+
t.string "name"
|
35
|
+
t.string "access_token"
|
36
|
+
t.string "download_ticket"
|
37
|
+
t.string "password_recovery"
|
38
|
+
end
|
39
|
+
create_table(:coupons) do |t|
|
40
|
+
t.string "code"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
CreateAllTables.up
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "token_attribute/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "token_attribute"
|
7
|
+
s.version = TokenAttribute::VERSION
|
8
|
+
s.authors = ["Fujimura Daisuke"]
|
9
|
+
s.email = ["me@fujimuradaisuke.com"]
|
10
|
+
s.homepage = "https://github.com/fujimura/token_attribute"
|
11
|
+
s.summary = %q{Small macro to generate unique random token attribute for ActiveRecord}
|
12
|
+
s.description = %q{Small macro to generate unique random token attribute for ActiveRecord}
|
13
|
+
|
14
|
+
s.rubyforge_project = "token_attribute"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
# specify any dependencies here; for example:
|
22
|
+
s.add_dependency "activerecord", '>= 3.0.0'
|
23
|
+
s.add_dependency "activesupport", '>= 3.0.0'
|
24
|
+
s.add_development_dependency "rake"
|
25
|
+
s.add_development_dependency "sqlite3"
|
26
|
+
s.add_development_dependency "minitest"
|
27
|
+
s.add_development_dependency "contest"
|
28
|
+
s.add_development_dependency "turn", '>= 0.8.3'
|
29
|
+
s.add_development_dependency "rr"
|
30
|
+
if RUBY_VERSION.to_f == 1.9
|
31
|
+
s.add_development_dependency "ruby-debug19"
|
32
|
+
else
|
33
|
+
s.add_development_dependency "ruby-debug"
|
34
|
+
end
|
35
|
+
end
|
metadata
ADDED
@@ -0,0 +1,157 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: token_attribute
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.4
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Fujimura Daisuke
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-10-25 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activerecord
|
16
|
+
requirement: &70158298353140 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.0.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70158298353140
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: activesupport
|
27
|
+
requirement: &70158298352680 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 3.0.0
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70158298352680
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rake
|
38
|
+
requirement: &70158298352300 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70158298352300
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: sqlite3
|
49
|
+
requirement: &70158298351880 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *70158298351880
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: minitest
|
60
|
+
requirement: &70158298351460 !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
type: :development
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: *70158298351460
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: contest
|
71
|
+
requirement: &70158298351040 !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: *70158298351040
|
80
|
+
- !ruby/object:Gem::Dependency
|
81
|
+
name: turn
|
82
|
+
requirement: &70158298350540 !ruby/object:Gem::Requirement
|
83
|
+
none: false
|
84
|
+
requirements:
|
85
|
+
- - ! '>='
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: 0.8.3
|
88
|
+
type: :development
|
89
|
+
prerelease: false
|
90
|
+
version_requirements: *70158298350540
|
91
|
+
- !ruby/object:Gem::Dependency
|
92
|
+
name: rr
|
93
|
+
requirement: &70158298350160 !ruby/object:Gem::Requirement
|
94
|
+
none: false
|
95
|
+
requirements:
|
96
|
+
- - ! '>='
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
type: :development
|
100
|
+
prerelease: false
|
101
|
+
version_requirements: *70158298350160
|
102
|
+
- !ruby/object:Gem::Dependency
|
103
|
+
name: ruby-debug19
|
104
|
+
requirement: &70158298349720 !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
type: :development
|
111
|
+
prerelease: false
|
112
|
+
version_requirements: *70158298349720
|
113
|
+
description: Small macro to generate unique random token attribute for ActiveRecord
|
114
|
+
email:
|
115
|
+
- me@fujimuradaisuke.com
|
116
|
+
executables: []
|
117
|
+
extensions: []
|
118
|
+
extra_rdoc_files: []
|
119
|
+
files:
|
120
|
+
- .gitignore
|
121
|
+
- .travis.yml
|
122
|
+
- Gemfile
|
123
|
+
- README.md
|
124
|
+
- Rakefile
|
125
|
+
- TODO.md
|
126
|
+
- lib/token_attribute.rb
|
127
|
+
- lib/token_attribute/version.rb
|
128
|
+
- test/lib/token_attribute_test.rb
|
129
|
+
- test/test_helper.rb
|
130
|
+
- token_attribute.gemspec
|
131
|
+
homepage: https://github.com/fujimura/token_attribute
|
132
|
+
licenses: []
|
133
|
+
post_install_message:
|
134
|
+
rdoc_options: []
|
135
|
+
require_paths:
|
136
|
+
- lib
|
137
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
138
|
+
none: false
|
139
|
+
requirements:
|
140
|
+
- - ! '>='
|
141
|
+
- !ruby/object:Gem::Version
|
142
|
+
version: '0'
|
143
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
144
|
+
none: false
|
145
|
+
requirements:
|
146
|
+
- - ! '>='
|
147
|
+
- !ruby/object:Gem::Version
|
148
|
+
version: '0'
|
149
|
+
requirements: []
|
150
|
+
rubyforge_project: token_attribute
|
151
|
+
rubygems_version: 1.8.10
|
152
|
+
signing_key:
|
153
|
+
specification_version: 3
|
154
|
+
summary: Small macro to generate unique random token attribute for ActiveRecord
|
155
|
+
test_files:
|
156
|
+
- test/lib/token_attribute_test.rb
|
157
|
+
- test/test_helper.rb
|