mongoid_token 0.9.0 → 0.9.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +10 -1
- data/lib/mongoid_token.rb +9 -1
- data/lib/version.rb +1 -1
- data/mongoid_token.gemspec +1 -1
- data/spec/mongoid/token_spec.rb +6 -0
- metadata +2 -2
data/README.md
CHANGED
@@ -17,7 +17,7 @@ Into something more like this:
|
|
17
17
|
|
18
18
|
In your gemfile, add:
|
19
19
|
|
20
|
-
gem 'mongoid_token', '
|
20
|
+
gem 'mongoid_token', '~. 0.9.0'
|
21
21
|
|
22
22
|
Then update your bundle
|
23
23
|
|
@@ -68,6 +68,7 @@ The options for `contains` are as follows:
|
|
68
68
|
* `token :length => 4, :contains => :fixed_numeric` will generate
|
69
69
|
anything from `0000` to `9999` in a random order
|
70
70
|
|
71
|
+
|
71
72
|
## Finders
|
72
73
|
|
73
74
|
The library also contains a finder method for looking up your documents
|
@@ -75,6 +76,14 @@ called `find_by_token`, e.g:
|
|
75
76
|
|
76
77
|
Person.find_by_token('7dDn8q')
|
77
78
|
|
79
|
+
|
80
|
+
## Adding tokens to existing documents
|
81
|
+
|
82
|
+
If you've got an app with existing data that you would like to add
|
83
|
+
tokens to - all you need to do is save each of your models and they will
|
84
|
+
be assigned a token, if it's missing.
|
85
|
+
|
86
|
+
|
78
87
|
# Notes
|
79
88
|
|
80
89
|
If you find a problem, please [submit an issue](http://github.com/thetron/mongoid_token/issues) (and a failing test, if
|
data/lib/mongoid_token.rb
CHANGED
@@ -13,6 +13,10 @@ module Mongoid
|
|
13
13
|
set_callback(:create, :before) do |document|
|
14
14
|
document.create_token(options[:length], options[:contains])
|
15
15
|
end
|
16
|
+
|
17
|
+
set_callback(:save, :before) do |document|
|
18
|
+
document.create_token_if_nil(options[:length], options[:contains])
|
19
|
+
end
|
16
20
|
end
|
17
21
|
|
18
22
|
def find_by_token(token)
|
@@ -27,7 +31,11 @@ module Mongoid
|
|
27
31
|
|
28
32
|
protected
|
29
33
|
def create_token(length, characters)
|
30
|
-
self.token = self.generate_token(length, characters) while self.token
|
34
|
+
self.token = self.generate_token(length, characters) while self.token.nil? || self.class.exists?(:conditions => {:token => self.token})
|
35
|
+
end
|
36
|
+
|
37
|
+
def create_token_if_nil(length, characters)
|
38
|
+
self.create_token(length, characters) if self.token.nil?
|
31
39
|
end
|
32
40
|
|
33
41
|
def generate_token(length, characters)
|
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, when you want shorter URLs.}
|
14
14
|
|
15
15
|
s.rubyforge_project = "mongoid_token"
|
16
|
-
s.add_dependency '
|
16
|
+
s.add_dependency 'activesupport', '>= 3.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")
|
data/spec/mongoid/token_spec.rb
CHANGED
@@ -100,4 +100,10 @@ describe Mongoid::Token do
|
|
100
100
|
Account.find_by_token(@account.token).id.should == @account.id
|
101
101
|
Account.find_by_token(Account.last.token).id.should == Account.last.id
|
102
102
|
end
|
103
|
+
|
104
|
+
it "should create a token, if the token is missing" do
|
105
|
+
@account.token = nil
|
106
|
+
@account.save!
|
107
|
+
@account.token.should_not be_nil
|
108
|
+
end
|
103
109
|
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: mongoid_token
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.9.
|
5
|
+
version: 0.9.1
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Nicholas Bruning
|
@@ -13,7 +13,7 @@ cert_chain: []
|
|
13
13
|
date: 2011-06-04 00:00:00 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
|
-
name:
|
16
|
+
name: activesupport
|
17
17
|
prerelease: false
|
18
18
|
requirement: &id001 !ruby/object:Gem::Requirement
|
19
19
|
none: false
|