mongoid_token 0.9.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.
- data/.autotest +1 -0
- data/.gitignore +4 -0
- data/.rspec +2 -0
- data/Gemfile +14 -0
- data/README.md +81 -0
- data/Rakefile +2 -0
- data/lib/.DS_Store +0 -0
- data/lib/mongoid_token.rb +49 -0
- data/lib/version.rb +3 -0
- data/mongoid_token.gemspec +22 -0
- data/spec/.DS_Store +0 -0
- data/spec/mongoid/token_spec.rb +103 -0
- data/spec/spec_helper.rb +22 -0
- metadata +78 -0
data/.autotest
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'autotest/growl'
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in mongoid_token.gemspec
|
4
|
+
gemspec
|
5
|
+
|
6
|
+
group :test do
|
7
|
+
gem 'database_cleaner'
|
8
|
+
gem 'rspec'
|
9
|
+
gem 'autotest'
|
10
|
+
gem 'autotest-growl'
|
11
|
+
gem 'mongoid', '~> 2.0'
|
12
|
+
gem 'bson_ext'
|
13
|
+
gem 'mongoid-rspec'
|
14
|
+
end
|
data/README.md
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
# Mongoid::Token - Short snappy tokens for Mongoid documents
|
2
|
+
|
3
|
+
This library is a quick and simple way to generate unique, random tokens
|
4
|
+
for your mongoid documents, in the cases where you can't, or don't want
|
5
|
+
to use slugs, or the default MongoDB IDs.
|
6
|
+
|
7
|
+
Mongoid::Token can help turn this:
|
8
|
+
|
9
|
+
http://bestwebappever.com/video/4dcfbb3c6a4f1d4c4a000012/edit
|
10
|
+
|
11
|
+
Into something more like this:
|
12
|
+
|
13
|
+
http://bestwebappever.com/video/83xQ3r/edit
|
14
|
+
|
15
|
+
|
16
|
+
## Getting started
|
17
|
+
|
18
|
+
In your gemfile, add:
|
19
|
+
|
20
|
+
gem 'mongoid_token', '1.0.0'
|
21
|
+
|
22
|
+
Then update your bundle
|
23
|
+
|
24
|
+
$ bundle update
|
25
|
+
|
26
|
+
In your Mongoid documents, just add `include Mongoid::Token` and the
|
27
|
+
`token` method will take care of all the setup, like so:
|
28
|
+
|
29
|
+
````
|
30
|
+
class Person
|
31
|
+
include Mongoid::Document
|
32
|
+
include Mongoid::Timestamps
|
33
|
+
include Mongoid::Token
|
34
|
+
|
35
|
+
field :first_name
|
36
|
+
field :last_name
|
37
|
+
|
38
|
+
token :length => 8
|
39
|
+
end
|
40
|
+
|
41
|
+
````
|
42
|
+
|
43
|
+
Obviously, this will create tokens of 8 characters long - make them as
|
44
|
+
short or as long as you require.
|
45
|
+
|
46
|
+
|
47
|
+
## Options
|
48
|
+
|
49
|
+
The `token` method takes two options: `length`, which determines the
|
50
|
+
length (or maximum length, in some cases) and `contains`, which tells
|
51
|
+
Mongoid::Token which characters to use when generating the token.
|
52
|
+
|
53
|
+
The options for `contains` are as follows:
|
54
|
+
|
55
|
+
* `:alphanumeric` - letters (upper and lowercase) and numbers
|
56
|
+
* `:alpha` - letters (upper and lowercase) only
|
57
|
+
* `:numeric` - numbers only, anything from 1 character long, up to and
|
58
|
+
`length`
|
59
|
+
* `:fixed_numeric` - numbers only, but with the number of characters always the same as `length`
|
60
|
+
|
61
|
+
### Examples:
|
62
|
+
|
63
|
+
* `token :length => 8, :contains => :alphanumeric` generates something like `8Sdc98dQ`
|
64
|
+
* `token :length => 5, :contains => :alpha` gereates something like
|
65
|
+
`ASlkj`
|
66
|
+
* `token :length => 4, :contains => :numeric` could generate anything
|
67
|
+
from `0` upto `9999` - but in a random order
|
68
|
+
* `token :length => 4, :contains => :fixed_numeric` will generate
|
69
|
+
anything from `0000` to `9999` in a random order
|
70
|
+
|
71
|
+
## Finders
|
72
|
+
|
73
|
+
The library also contains a finder method for looking up your documents
|
74
|
+
called `find_by_token`, e.g:
|
75
|
+
|
76
|
+
Person.find_by_token('7dDn8q')
|
77
|
+
|
78
|
+
# Notes
|
79
|
+
|
80
|
+
If you find a problem, please [submit an issue](http://github.com/thetron/mongoid_token/issues) (and a failing test, if
|
81
|
+
you can). Pull requests and feature requests are always welcome.
|
data/Rakefile
ADDED
data/lib/.DS_Store
ADDED
Binary file
|
@@ -0,0 +1,49 @@
|
|
1
|
+
module Mongoid
|
2
|
+
module Token
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
module ClassMethods
|
6
|
+
def token(*args)
|
7
|
+
options = args.extract_options!
|
8
|
+
options[:length] ||= 4
|
9
|
+
options[:contains] ||= :alphanumeric
|
10
|
+
|
11
|
+
self.field :token, :type => String
|
12
|
+
|
13
|
+
set_callback(:create, :before) do |document|
|
14
|
+
document.create_token(options[:length], options[:contains])
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def find_by_token(token)
|
19
|
+
self.first(:conditions => {:token => token})
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
module InstanceMethods
|
24
|
+
def to_param
|
25
|
+
self.token
|
26
|
+
end
|
27
|
+
|
28
|
+
protected
|
29
|
+
def create_token(length, characters)
|
30
|
+
self.token = self.generate_token(length, characters) while self.token == nil || self.class.exists?(:conditions => {:token => self.token})
|
31
|
+
end
|
32
|
+
|
33
|
+
def generate_token(length, characters)
|
34
|
+
case characters
|
35
|
+
when :alphanumeric
|
36
|
+
ActiveSupport::SecureRandom.hex(length)[0...length]
|
37
|
+
when :numeric
|
38
|
+
rand(10**length).to_s
|
39
|
+
when :fixed_numeric
|
40
|
+
rand(10**length).to_s.rjust(length,rand(10).to_s)
|
41
|
+
when :alpha
|
42
|
+
Array.new(length).map{['A'..'Z','a'..'z'].map{|r|r.to_a}.flatten[rand(52)]}.join
|
43
|
+
else
|
44
|
+
ActiveSupport::SecureRandom.hex(length)[0...length]
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
data/lib/version.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "mongoid_token"
|
7
|
+
s.version = MongoidToken::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Nicholas Bruning"]
|
10
|
+
s.email = ["nicholas@bruning.com.au"]
|
11
|
+
s.homepage = "http://github.com/thetron/mongoid_token"
|
12
|
+
s.summary = %q{A little random, unique token generator for Mongoid documents.}
|
13
|
+
s.description = %q{Mongoid token is a gem for creating random, unique tokens for mongoid documents, when you want shorter URLs.}
|
14
|
+
|
15
|
+
s.rubyforge_project = "mongoid_token"
|
16
|
+
s.add_dependency 'active_support', '>= 3.0.0'
|
17
|
+
|
18
|
+
s.files = `git ls-files`.split("\n")
|
19
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
20
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
21
|
+
s.require_paths = ["lib"]
|
22
|
+
end
|
data/spec/.DS_Store
ADDED
Binary file
|
@@ -0,0 +1,103 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), %w[.. spec_helper])
|
2
|
+
|
3
|
+
class Account
|
4
|
+
include Mongoid::Document
|
5
|
+
include Mongoid::Token
|
6
|
+
field :name
|
7
|
+
token :length => 16, :contains => :fixed_numeric
|
8
|
+
end
|
9
|
+
|
10
|
+
class Person
|
11
|
+
include Mongoid::Document
|
12
|
+
include Mongoid::Token
|
13
|
+
field :email
|
14
|
+
token :length => 6, :contains => :numeric
|
15
|
+
end
|
16
|
+
|
17
|
+
class Link
|
18
|
+
include Mongoid::Document
|
19
|
+
include Mongoid::Token
|
20
|
+
|
21
|
+
field :url
|
22
|
+
token :length => 3, :contains => :alphanumeric
|
23
|
+
end
|
24
|
+
|
25
|
+
class Video
|
26
|
+
include Mongoid::Document
|
27
|
+
include Mongoid::Token
|
28
|
+
|
29
|
+
field :name
|
30
|
+
token :length => 8, :contains => :alpha
|
31
|
+
end
|
32
|
+
|
33
|
+
describe Mongoid::Token do
|
34
|
+
before :each do
|
35
|
+
@account = Account.create(:name => "Involved Pty. Ltd.")
|
36
|
+
@link = Link.create(:url => "http://involved.com.au")
|
37
|
+
@video = Video.create(:name => "Nyan nyan")
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should have a token field" do
|
41
|
+
@account.attributes.include?('token').should == true
|
42
|
+
@link.attributes.include?('token').should == true
|
43
|
+
@video.attributes.include?('token').should == true
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should have a token of correct length" do
|
47
|
+
@account.token.length.should == 16
|
48
|
+
@link.token.length.should == 3
|
49
|
+
@video.token.length.should == 8
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should only generate unique tokens" do
|
53
|
+
1000.times do
|
54
|
+
@link = Link.create(:url => "http://involved.com.au")
|
55
|
+
Link.count(:conditions => {:token => @link.token}).should == 1
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should have a token containing only the specified characters" do
|
60
|
+
50.times do
|
61
|
+
@account = Account.create(:name => "Smith & Co. LLC")
|
62
|
+
@person = Person.create(:email => "some_random_235@gmail.com")
|
63
|
+
|
64
|
+
@account.token.gsub(/[0-9]/, "").length.should == 0
|
65
|
+
@person.token.gsub(/[0-9]/, "").length.should == 0
|
66
|
+
end
|
67
|
+
|
68
|
+
50.times do
|
69
|
+
@link = Link.create(:url => "http://involved.com.au")
|
70
|
+
@link.token.gsub(/[A-Za-z0-9]/, "").length.should == 0
|
71
|
+
end
|
72
|
+
|
73
|
+
50.times do
|
74
|
+
@video = Video.create(:name => "A test video")
|
75
|
+
@video.token.gsub(/[A-Za-z]/, "").length.should == 0
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
it "should create the only after the first save" do
|
80
|
+
@account = Account.new(:name => "Smith & Co. LLC")
|
81
|
+
@account.token.should be_nil
|
82
|
+
@account.save!
|
83
|
+
@account.token.should_not be_nil
|
84
|
+
initial_token = @account.token
|
85
|
+
@account.save!
|
86
|
+
initial_token.should == @account.token
|
87
|
+
end
|
88
|
+
|
89
|
+
it "should return the token as its parameter" do
|
90
|
+
@account.to_param.should == @account.token
|
91
|
+
@link.to_param.should == @link.token
|
92
|
+
@video.to_param.should == @video.token
|
93
|
+
end
|
94
|
+
|
95
|
+
|
96
|
+
it "should be finable by token" do
|
97
|
+
50.times do |index|
|
98
|
+
Account.create(:name => "A random company #{index}")
|
99
|
+
end
|
100
|
+
Account.find_by_token(@account.token).id.should == @account.id
|
101
|
+
Account.find_by_token(Account.last.token).id.should == Account.last.id
|
102
|
+
end
|
103
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
$: << File.expand_path("../../lib", __FILE__)
|
2
|
+
|
3
|
+
require 'database_cleaner'
|
4
|
+
require 'mongoid'
|
5
|
+
require 'mongoid-rspec'
|
6
|
+
require 'mongoid_token'
|
7
|
+
|
8
|
+
RSpec.configure do |config|
|
9
|
+
config.include Mongoid::Matchers
|
10
|
+
config.before(:suite) do
|
11
|
+
DatabaseCleaner.strategy = :truncation
|
12
|
+
end
|
13
|
+
|
14
|
+
config.after(:each) do
|
15
|
+
DatabaseCleaner.clean
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
Mongoid.configure do |config|
|
20
|
+
config.master = Mongo::Connection.new.db("mongoid_token_test")
|
21
|
+
end
|
22
|
+
|
metadata
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mongoid_token
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.9.0
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Nicholas Bruning
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-06-04 00:00:00 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: active_support
|
17
|
+
prerelease: false
|
18
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 3.0.0
|
24
|
+
type: :runtime
|
25
|
+
version_requirements: *id001
|
26
|
+
description: Mongoid token is a gem for creating random, unique tokens for mongoid documents, when you want shorter URLs.
|
27
|
+
email:
|
28
|
+
- nicholas@bruning.com.au
|
29
|
+
executables: []
|
30
|
+
|
31
|
+
extensions: []
|
32
|
+
|
33
|
+
extra_rdoc_files: []
|
34
|
+
|
35
|
+
files:
|
36
|
+
- .autotest
|
37
|
+
- .gitignore
|
38
|
+
- .rspec
|
39
|
+
- Gemfile
|
40
|
+
- README.md
|
41
|
+
- Rakefile
|
42
|
+
- lib/.DS_Store
|
43
|
+
- lib/mongoid_token.rb
|
44
|
+
- lib/version.rb
|
45
|
+
- mongoid_token.gemspec
|
46
|
+
- spec/.DS_Store
|
47
|
+
- spec/mongoid/token_spec.rb
|
48
|
+
- spec/spec_helper.rb
|
49
|
+
homepage: http://github.com/thetron/mongoid_token
|
50
|
+
licenses: []
|
51
|
+
|
52
|
+
post_install_message:
|
53
|
+
rdoc_options: []
|
54
|
+
|
55
|
+
require_paths:
|
56
|
+
- lib
|
57
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: "0"
|
63
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
|
+
none: false
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: "0"
|
69
|
+
requirements: []
|
70
|
+
|
71
|
+
rubyforge_project: mongoid_token
|
72
|
+
rubygems_version: 1.8.3
|
73
|
+
signing_key:
|
74
|
+
specification_version: 3
|
75
|
+
summary: A little random, unique token generator for Mongoid documents.
|
76
|
+
test_files:
|
77
|
+
- spec/mongoid/token_spec.rb
|
78
|
+
- spec/spec_helper.rb
|