mongoid_token 2.0.2 → 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -12
- data/.rspec +0 -1
- data/.travis.yml +10 -4
- data/Gemfile +2 -0
- data/README.md +26 -25
- data/lib/mongoid/token.rb +24 -10
- data/lib/mongoid/token/finders.rb +1 -1
- data/lib/mongoid/token/generator.rb +8 -4
- data/lib/version.rb +1 -1
- data/mongoid_token.gemspec +1 -1
- data/spec/mongoid/token/collisions_spec.rb +0 -2
- data/spec/mongoid/token/finders_spec.rb +8 -8
- data/spec/mongoid/token_spec.rb +17 -1
- data/spec/spec_helper.rb +12 -6
- metadata +5 -7
- data/spec/mongoid.yml +0 -9
checksums.yaml
CHANGED
@@ -1,15 +1,7 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
|
-
metadata.gz:
|
4
|
-
|
5
|
-
data.tar.gz: !binary |-
|
6
|
-
MjZiZDAzN2MyM2UxMjQ1Zjg5MWJhMWRhZDBiN2ZmMDczMDUzMDJjYg==
|
3
|
+
metadata.gz: 13ce75b253568cef707efd8bfefc9a36c528426b
|
4
|
+
data.tar.gz: eb17e324405d6a4a6845df7338f16f4e348d3242
|
7
5
|
SHA512:
|
8
|
-
metadata.gz:
|
9
|
-
|
10
|
-
MWEzYTEyYjA5MTFmMGE4NGM4ZDFlNWMwODcwMzNkNzliZDRkYTE3NzJjYmIy
|
11
|
-
OGMyOTkxOTE3M2JkNDNiNGZiZTFjN2VkZGY2MGQ1ZGI5MWQxNWQ=
|
12
|
-
data.tar.gz: !binary |-
|
13
|
-
NGYzN2NlZjlhZWIzYmNjZWEyNDJhZjcxMGIzMDgyYWVlZDhmMDhiMWVhYzMw
|
14
|
-
ZDE3NDIwZGMwY2Q0MzU2Y2NhM2IxZWMwZjBmYzBlMTMyMzU0NGUyNjM3YTFl
|
15
|
-
YTdmZjVlZTU0NmQwNzYwYjc5MWMxMjFkMjExNzk5MmVhNjVjNjg=
|
6
|
+
metadata.gz: a81e923d8f4c7c5f0472df99d90a45a3bf37fedf6a87c897ba989ddad7de6c194f84d79a2db3431506fd974d7023023151ad685bccbc8740ac17b57343b69d2b
|
7
|
+
data.tar.gz: ab820d32511412cda5f7730587a29697d147e720c3ad51ec106b089eeb0d70a4be112fd8dcfdaa2098c6a1af38b512e7ec0fe320a3b36833e1c83e30964fcc0f
|
data/.rspec
CHANGED
data/.travis.yml
CHANGED
@@ -1,12 +1,18 @@
|
|
1
|
-
script: "bundle exec rspec spec"
|
2
1
|
language: ruby
|
2
|
+
|
3
3
|
rvm:
|
4
4
|
- 1.9.3
|
5
5
|
- 2.0.0
|
6
|
+
- 2.1.0
|
7
|
+
- 2.1.1
|
8
|
+
|
6
9
|
gemfile:
|
7
10
|
- Gemfile
|
8
|
-
|
9
|
-
recipients:
|
10
|
-
- nicholas@bruning.com.au
|
11
|
+
|
11
12
|
services:
|
12
13
|
- mongodb
|
14
|
+
|
15
|
+
env:
|
16
|
+
- CODECLIMATE_REPO_TOKEN=b216164ab66da464aa02fe5b862811ba0526c8dc7ea291ebe53056be4b6b5e1f
|
17
|
+
|
18
|
+
script: "bundle exec rspec"
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# Mongoid::Token - Short snappy tokens for Mongoid documents
|
2
2
|
|
3
3
|
[![Build Status](https://secure.travis-ci.org/thetron/mongoid_token.png)](http://travis-ci.org/thetron/mongoid_token)
|
4
|
+
[![Code Climate](https://codeclimate.com/github/thetron/mongoid_token.png)](https://codeclimate.com/github/thetron/mongoid_token)
|
4
5
|
|
5
6
|
This library is a quick and simple way to generate unique, random tokens
|
6
7
|
for your mongoid documents, in the cases where you can't, or don't want
|
@@ -28,7 +29,7 @@ Then update your bundle
|
|
28
29
|
In your Mongoid documents, just add `include Mongoid::Token` and the
|
29
30
|
`token` method will take care of all the setup, like so:
|
30
31
|
|
31
|
-
|
32
|
+
```ruby
|
32
33
|
class Person
|
33
34
|
include Mongoid::Document
|
34
35
|
include Mongoid::Token
|
@@ -38,7 +39,7 @@ class Person
|
|
38
39
|
token
|
39
40
|
end
|
40
41
|
|
41
|
-
|
42
|
+
```
|
42
43
|
|
43
44
|
And that's it! There's lots of configuration options too - which are all
|
44
45
|
listed [below](#configuration). By default, the `token` method will
|
@@ -57,10 +58,10 @@ in order to search for documents based on their token first (although
|
|
57
58
|
the default behaviour of ObjectIDs is also there). You can disable these
|
58
59
|
with the [`skip_finders` configuration option](#skip-finders-skip_finders).
|
59
60
|
|
60
|
-
|
61
|
+
```ruby
|
61
62
|
Video.find("x3v98")
|
62
63
|
Account.find("ACC-123456")
|
63
|
-
|
64
|
+
```
|
64
65
|
|
65
66
|
|
66
67
|
## Configuration
|
@@ -68,7 +69,7 @@ Account.find("ACC-123456")
|
|
68
69
|
### Tokens
|
69
70
|
|
70
71
|
As of `Mongoid::Token` 2.0.0, you can now choose between two different
|
71
|
-
systems for managing how your tokens look.
|
72
|
+
systems for managing how your tokens look.
|
72
73
|
|
73
74
|
For simple setup, you can use
|
74
75
|
combination of the [`length`](#length-length) and [`contains`](#contains-contains), which modify the length and
|
@@ -81,18 +82,18 @@ prefixes, infixes or suffixes.
|
|
81
82
|
|
82
83
|
#### Length (`:length`)
|
83
84
|
|
84
|
-
This one is easy, it's just an integer.
|
85
|
+
This one is easy, it's just an integer.
|
85
86
|
|
86
87
|
__Example:__
|
87
88
|
|
88
|
-
|
89
|
+
```ruby
|
89
90
|
token :length => 6 # Tokens are now of length 6
|
90
91
|
token :length => 12 # Whow, whow, whow. Slow down egghead.
|
91
|
-
|
92
|
+
```
|
92
93
|
|
93
94
|
You get the idea.
|
94
95
|
|
95
|
-
The only caveat here is that if used in combination with the
|
96
|
+
The only caveat here is that if used in combination with the
|
96
97
|
`:contains => :numeric` option, tokens may vary in length _up to_ the
|
97
98
|
specified length.
|
98
99
|
|
@@ -111,14 +112,14 @@ as numbers
|
|
111
112
|
never start with zeros
|
112
113
|
|
113
114
|
__Examples:__
|
114
|
-
|
115
|
+
```ruby
|
115
116
|
token :contains => :alpha_upper, :length => 8
|
116
117
|
token :contains => :fixed_numeric
|
117
|
-
|
118
|
+
```
|
118
119
|
|
119
120
|
#### Patterns (`:pattern`)
|
120
121
|
|
121
|
-
New in 2.0.0, patterns allow you
|
122
|
+
New in 2.0.0, patterns allow you fine-grained control over how your
|
122
123
|
tokens look. It's great for generating random data that has a
|
123
124
|
requirements to also have some basic structure. If you use the
|
124
125
|
`:pattern` option, it will override both the `:length` and `:contains`
|
@@ -141,18 +142,18 @@ generated character, and are as follows:
|
|
141
142
|
|
142
143
|
__Example:__
|
143
144
|
|
144
|
-
|
145
|
+
```ruby
|
145
146
|
token :pattern => "PRE-%C%C-%d%d%d%d" # Generates something like: 'PRE-ND-3485'
|
146
|
-
|
147
|
+
```
|
147
148
|
|
148
149
|
You can also add a repetition modifier, which can help improve readability on
|
149
150
|
more complex patterns. You simply add any integer after the letter.
|
150
151
|
|
151
152
|
__Examples:__
|
152
153
|
|
153
|
-
|
154
|
-
token :
|
155
|
-
|
154
|
+
```ruby
|
155
|
+
token :pattern => "APP-%d6" # Generates something like; "APP-638924"
|
156
|
+
```
|
156
157
|
|
157
158
|
### Field Name (`:field_name`)
|
158
159
|
|
@@ -161,11 +162,11 @@ This allows you to change the field name used by `Mongoid::Token`
|
|
161
162
|
use multiple tokens one a single document.
|
162
163
|
|
163
164
|
__Examples:__
|
164
|
-
|
165
|
+
```ruby
|
165
166
|
token :length => 6
|
166
167
|
token :field_name => :sharing_token, :length => 12
|
167
168
|
token :field_name => :yet_another
|
168
|
-
|
169
|
+
```
|
169
170
|
|
170
171
|
|
171
172
|
### Skip Finders (`:skip_finders`)
|
@@ -174,9 +175,9 @@ This will prevent the gem from creating the customised finders and
|
|
174
175
|
overrides for the default `find` behaviour used by Mongoid.
|
175
176
|
|
176
177
|
__Example:__
|
177
|
-
|
178
|
+
```ruby
|
178
179
|
token :skip_finders => true
|
179
|
-
|
180
|
+
```
|
180
181
|
|
181
182
|
|
182
183
|
### Override to_param (`:override_to_param`)
|
@@ -186,9 +187,9 @@ drop-in replacement for the default ObjectIDs. If needed, you can turn
|
|
186
187
|
this behaviour off:
|
187
188
|
|
188
189
|
__Example:__
|
189
|
-
|
190
|
+
```ruby
|
190
191
|
token :override_to_param => false
|
191
|
-
|
192
|
+
```
|
192
193
|
|
193
194
|
|
194
195
|
### Retry Count (`:retry_count`)
|
@@ -199,10 +200,10 @@ error. If you're wanting it to try harder, or less hard, then this
|
|
199
200
|
option is for you.
|
200
201
|
|
201
202
|
__Examples:__
|
202
|
-
|
203
|
+
```ruby
|
203
204
|
token :retry_count => 9
|
204
205
|
token :retry_count => 0
|
205
|
-
|
206
|
+
```
|
206
207
|
|
207
208
|
# Notes
|
208
209
|
|
data/lib/mongoid/token.rb
CHANGED
@@ -10,37 +10,51 @@ module Mongoid
|
|
10
10
|
|
11
11
|
module ClassMethods
|
12
12
|
def initialize_copy(source)
|
13
|
-
super
|
13
|
+
super(source)
|
14
14
|
self.token = nil
|
15
15
|
end
|
16
16
|
|
17
17
|
def token(*args)
|
18
18
|
options = Mongoid::Token::Options.new(args.extract_options!)
|
19
19
|
|
20
|
+
add_token_field_and_index(options)
|
21
|
+
add_token_collision_resolver(options)
|
22
|
+
set_token_callbacks(options)
|
23
|
+
|
24
|
+
define_custom_finders(options) if options.skip_finders? == false
|
25
|
+
override_to_param(options) if options.override_to_param?
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
def add_token_field_and_index(options)
|
20
30
|
self.field options.field_name, :type => String, :default => nil
|
21
|
-
self.index({ options.field_name => 1 }, { :unique => true })
|
31
|
+
self.index({ options.field_name => 1 }, { :unique => true, :sparse => true })
|
32
|
+
end
|
22
33
|
|
34
|
+
def add_token_collision_resolver(options)
|
23
35
|
resolver = Mongoid::Token::CollisionResolver.new(self, options.field_name, options.retry_count)
|
24
36
|
resolver.create_new_token = Proc.new do |document|
|
25
37
|
document.send(:create_token, options.field_name, options.pattern)
|
26
38
|
end
|
39
|
+
end
|
27
40
|
|
28
|
-
|
29
|
-
|
30
|
-
|
41
|
+
def define_custom_finders(options)
|
42
|
+
Finders.define_custom_token_finder_for(self, options.field_name)
|
43
|
+
end
|
31
44
|
|
45
|
+
def set_token_callbacks(options)
|
32
46
|
set_callback(:create, :before) do |document|
|
33
|
-
document.
|
47
|
+
document.create_token_if_nil options.field_name, options.pattern
|
34
48
|
end
|
35
49
|
|
36
50
|
set_callback(:save, :before) do |document|
|
37
51
|
document.create_token_if_nil options.field_name, options.pattern
|
38
52
|
end
|
53
|
+
end
|
39
54
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
end
|
55
|
+
def override_to_param(options)
|
56
|
+
self.send(:define_method, :to_param) do
|
57
|
+
self.send(options.field_name) || super(*args)
|
44
58
|
end
|
45
59
|
end
|
46
60
|
end
|
@@ -7,7 +7,7 @@ module Mongoid
|
|
7
7
|
end
|
8
8
|
|
9
9
|
klass.define_singleton_method :"find_with_#{field_name}" do |*args| # this is going to be painful if tokens happen to look like legal object ids
|
10
|
-
args.all?{|arg|
|
10
|
+
args.all?{|arg| BSON::ObjectId.legal?(arg)} ? send(:"find_without_#{field_name}",*args) : klass.send(:"find_by_#{field_name.to_s}", args.first)
|
11
11
|
end
|
12
12
|
|
13
13
|
# this craziness taken from, and then compacted into a string class_eval
|
@@ -40,12 +40,16 @@ module Mongoid
|
|
40
40
|
end
|
41
41
|
|
42
42
|
private
|
43
|
+
def self.rand_string_from_chars(chars, length = 1)
|
44
|
+
Array.new(length).map{ chars.sample }.join
|
45
|
+
end
|
46
|
+
|
43
47
|
def self.down_character(length = 1)
|
44
|
-
|
48
|
+
self.rand_string_from_chars ('a'..'z').to_a, length
|
45
49
|
end
|
46
50
|
|
47
51
|
def self.up_character(length = 1)
|
48
|
-
|
52
|
+
self.rand_string_from_chars ('A'..'Z').to_a, length
|
49
53
|
end
|
50
54
|
|
51
55
|
def self.integer(length = 1)
|
@@ -57,7 +61,7 @@ module Mongoid
|
|
57
61
|
end
|
58
62
|
|
59
63
|
def self.alpha(length = 1)
|
60
|
-
|
64
|
+
self.rand_string_from_chars (('A'..'Z').to_a + ('a'..'z').to_a), length
|
61
65
|
end
|
62
66
|
|
63
67
|
def self.alphanumeric(length = 1)
|
@@ -65,7 +69,7 @@ module Mongoid
|
|
65
69
|
end
|
66
70
|
|
67
71
|
def self.punctuation(length = 1)
|
68
|
-
|
72
|
+
self.rand_string_from_chars ['.','-','_','=','+','$'], length
|
69
73
|
end
|
70
74
|
end
|
71
75
|
end
|
data/lib/version.rb
CHANGED
data/mongoid_token.gemspec
CHANGED
@@ -13,7 +13,7 @@ Gem::Specification.new do |s|
|
|
13
13
|
s.description = %q{Mongoid token is a gem for creating random, unique tokens for mongoid documents. Highly configurable and great for making URLs a little more compact.}
|
14
14
|
|
15
15
|
s.rubyforge_project = "mongoid_token"
|
16
|
-
s.add_dependency 'mongoid', '~>
|
16
|
+
s.add_dependency 'mongoid', '~> 4.0.0'
|
17
17
|
|
18
18
|
s.files = `git ls-files`.split("\n")
|
19
19
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
@@ -12,16 +12,16 @@ describe Mongoid::Token::Finders do
|
|
12
12
|
end
|
13
13
|
|
14
14
|
it "override the `find` method of the document" do
|
15
|
-
klass = Class.new
|
15
|
+
klass = Class.new
|
16
16
|
klass.define_singleton_method(:find) {|*args| :original_find }
|
17
17
|
klass.define_singleton_method(:find_by) {|*args| :token_find }
|
18
18
|
|
19
19
|
Mongoid::Token::Finders.define_custom_token_finder_for(klass)
|
20
20
|
|
21
|
-
klass.find(
|
22
|
-
klass.find(
|
21
|
+
klass.find(BSON::ObjectId.new).should == :original_find
|
22
|
+
klass.find(BSON::ObjectId.new, BSON::ObjectId.new).should == :original_find
|
23
23
|
klass.find().should == :original_find
|
24
|
-
klass.find(
|
24
|
+
klass.find(BSON::ObjectId.new, "token").should == :token_find
|
25
25
|
klass.find("token").should == :token_find
|
26
26
|
end
|
27
27
|
|
@@ -33,9 +33,9 @@ describe Mongoid::Token::Finders do
|
|
33
33
|
end
|
34
34
|
|
35
35
|
it "retrieve a document using the `find` method" do
|
36
|
-
class
|
37
|
-
document =
|
38
|
-
Mongoid::Token::Finders.define_custom_token_finder_for(
|
39
|
-
|
36
|
+
class AnotherDocument; include Mongoid::Document; field :token; end
|
37
|
+
document = AnotherDocument.create! :token => "1234"
|
38
|
+
Mongoid::Token::Finders.define_custom_token_finder_for(AnotherDocument)
|
39
|
+
AnotherDocument.find("1234").should == document
|
40
40
|
end
|
41
41
|
end
|
data/spec/mongoid/token_spec.rb
CHANGED
@@ -2,6 +2,11 @@ require File.join(File.dirname(__FILE__), %w[.. spec_helper])
|
|
2
2
|
|
3
3
|
describe Mongoid::Token do
|
4
4
|
let(:document_class) do
|
5
|
+
Object.send(:remove_const, :Document)
|
6
|
+
class Document
|
7
|
+
include Mongoid::Document
|
8
|
+
include Mongoid::Token
|
9
|
+
end
|
5
10
|
Class.new(Document)
|
6
11
|
end
|
7
12
|
|
@@ -17,7 +22,10 @@ describe Mongoid::Token do
|
|
17
22
|
end
|
18
23
|
|
19
24
|
it "should be indexed" do
|
20
|
-
|
25
|
+
index = document.index_specifications.first
|
26
|
+
expect(index.fields).to eq([:token])
|
27
|
+
expect(index.options).to have_key(:unique)
|
28
|
+
expect(index.options).to have_key(:sparse)
|
21
29
|
end
|
22
30
|
end
|
23
31
|
|
@@ -154,6 +162,13 @@ describe Mongoid::Token do
|
|
154
162
|
expect(document.token).to_not eq token_before
|
155
163
|
end
|
156
164
|
end
|
165
|
+
context "when the document is initialized with a token" do
|
166
|
+
it "should not change the token after being saved" do
|
167
|
+
document_class.send(:token)
|
168
|
+
token = 'test token'
|
169
|
+
expect(document_class.create!(token: token).token).to eq token
|
170
|
+
end
|
171
|
+
end
|
157
172
|
end
|
158
173
|
context "when the document is cloned" do
|
159
174
|
it "should set the token to nil" do
|
@@ -210,6 +225,7 @@ describe Mongoid::Token do
|
|
210
225
|
end
|
211
226
|
|
212
227
|
it "should not raise a custom error if another error is thrown during saving" do
|
228
|
+
I18n.enforce_available_locales = false # Supress warnings in this example
|
213
229
|
document_class.send(:field, :name)
|
214
230
|
document_class.send(:validates_presence_of, :name)
|
215
231
|
document_class.any_instance.stub(:generate_token).and_return("1234")
|
data/spec/spec_helper.rb
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
require "codeclimate-test-reporter"
|
2
|
+
CodeClimate::TestReporter.start
|
3
|
+
|
1
4
|
$: << File.expand_path("../../lib", __FILE__)
|
2
5
|
|
3
6
|
require 'database_cleaner'
|
@@ -7,11 +10,6 @@ require 'mongoid_token'
|
|
7
10
|
|
8
11
|
ENV['MONGOID_ENV'] = "test"
|
9
12
|
|
10
|
-
class Document
|
11
|
-
include Mongoid::Document
|
12
|
-
include Mongoid::Token
|
13
|
-
end
|
14
|
-
|
15
13
|
RSpec.configure do |config|
|
16
14
|
config.include Mongoid::Matchers
|
17
15
|
config.before(:suite) do
|
@@ -24,4 +22,12 @@ RSpec.configure do |config|
|
|
24
22
|
end
|
25
23
|
end
|
26
24
|
|
27
|
-
Mongoid.
|
25
|
+
Mongoid.configure do |config|
|
26
|
+
config.sessions = {
|
27
|
+
default: {
|
28
|
+
database: "mongoid_token_test",
|
29
|
+
hosts: [ "localhost:#{ENV['BOXEN_MONGODB_PORT'] || 27017}" ],
|
30
|
+
options: {}
|
31
|
+
}
|
32
|
+
}
|
33
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid_token
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0
|
4
|
+
version: 2.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nicholas Bruning
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2014-07-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mongoid
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ~>
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 4.0.0
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ~>
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: 4.0.0
|
27
27
|
description: Mongoid token is a gem for creating random, unique tokens for mongoid
|
28
28
|
documents. Highly configurable and great for making URLs a little more compact.
|
29
29
|
email:
|
@@ -50,7 +50,6 @@ files:
|
|
50
50
|
- lib/mongoid_token.rb
|
51
51
|
- lib/version.rb
|
52
52
|
- mongoid_token.gemspec
|
53
|
-
- spec/mongoid.yml
|
54
53
|
- spec/mongoid/token/collisions_spec.rb
|
55
54
|
- spec/mongoid/token/exceptions_spec.rb
|
56
55
|
- spec/mongoid/token/finders_spec.rb
|
@@ -77,12 +76,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
77
76
|
version: '0'
|
78
77
|
requirements: []
|
79
78
|
rubyforge_project: mongoid_token
|
80
|
-
rubygems_version: 2.
|
79
|
+
rubygems_version: 2.2.2
|
81
80
|
signing_key:
|
82
81
|
specification_version: 4
|
83
82
|
summary: A little random, unique token generator for Mongoid documents.
|
84
83
|
test_files:
|
85
|
-
- spec/mongoid.yml
|
86
84
|
- spec/mongoid/token/collisions_spec.rb
|
87
85
|
- spec/mongoid/token/exceptions_spec.rb
|
88
86
|
- spec/mongoid/token/finders_spec.rb
|