mongoid-ids 0.1.1 → 0.1.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +7 -1
- data/README.md +68 -55
- data/lib/mongoid/ids.rb +0 -7
- data/lib/mongoid/ids/options.rb +1 -6
- data/lib/mongoid/ids/version.rb +1 -1
- data/spec/mongoid/ids/options_spec.rb +0 -11
- data/spec/mongoid/token_spec.rb +2 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e9972a9121c4854be14d38fe4e5c21c51af16e9c
|
4
|
+
data.tar.gz: 1c210ceb6bdb7e6388572ccd5a0145f9baa44b75
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8f2549a80a6c1599e8d1ab9f471691aca2b6601e173532c73e5632fbc9ef1bb118fc21faf27d2447e978a5f0d29007a40818c8a3a5bf1a1a385bf8a9b4548652
|
7
|
+
data.tar.gz: 6257c1b856c9e43c1179d65c703ab61cf0f73abd21d7f6bd78d0d3d9d02b391ddd45d813e8084706d14c7f99534321eda92ef6adfc133eb3f1d6a509d45ab95f
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -1,9 +1,18 @@
|
|
1
|
-
# Mongoid::Ids
|
2
|
-
|
3
|
-
[![Build Status](https://secure.travis-ci.org/nofxx/mongoid-ids.png)](http://travis-ci.org/nofxx/mongoid-ids)
|
1
|
+
# Mongoid::Ids
|
4
2
|
[![Code Climate](https://codeclimate.com/github/nofxx/mongoid-ids.png)](https://codeclimate.com/github/nofxx/mongoid-ids)
|
3
|
+
[![Dependency Status](https://gemnasium.com/nofxx/mongoid-ids.svg)](https://gemnasium.com/nofxx/mongoid-ids)
|
4
|
+
[![Build Status](https://secure.travis-ci.org/nofxx/mongoid-ids.png)](http://travis-ci.org/nofxx/mongoid-ids)
|
5
|
+
|
6
|
+
## Mongoid::Token || Mongoid::Ids
|
7
|
+
|
8
|
+
This gem is a fork that changes the default behaviour of original
|
9
|
+
Mongoid::Token: instead of custom fields it changes _id by default.
|
10
|
+
But you may still use tokens on custom fields.
|
5
11
|
|
6
|
-
|
12
|
+
|
13
|
+
## Short snappy tokens for Mongoid documents
|
14
|
+
|
15
|
+
This library is a quick and simple way to generate unique, random ids
|
7
16
|
for your mongoid documents, in the cases where you can't, or don't want
|
8
17
|
to use slugs, or the default MongoDB ObjectIDs.
|
9
18
|
|
@@ -20,11 +29,7 @@ Into something more like this:
|
|
20
29
|
|
21
30
|
In your gemfile, add:
|
22
31
|
|
23
|
-
gem 'mongoid-ids'
|
24
|
-
|
25
|
-
Then update your bundle
|
26
|
-
|
27
|
-
$ bundle update
|
32
|
+
gem 'mongoid-ids'
|
28
33
|
|
29
34
|
In your Mongoid documents, just add `include Mongoid::Ids` and the
|
30
35
|
`token` method will take care of all the setup, like so:
|
@@ -45,40 +50,56 @@ And that's it! There's lots of configuration options too - which are all
|
|
45
50
|
listed [below](#configuration). By default, the `token` method will
|
46
51
|
create tokens 4 characters long, containing random alphanumeric characters.
|
47
52
|
|
48
|
-
|
53
|
+
## Custom/Extra fields
|
54
|
+
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
class Person
|
58
|
+
include Mongoid::Document
|
59
|
+
include Mongoid::Ids
|
60
|
+
|
61
|
+
field :name
|
62
|
+
|
63
|
+
token :code
|
64
|
+
token :token
|
65
|
+
token :other_code
|
66
|
+
end
|
67
|
+
|
68
|
+
```
|
69
|
+
|
70
|
+
__Note on custom field:__ Mongoid::Ids leverages Mongoid's 'safe mode' by
|
49
71
|
automatically creating a unique index on your documents using the token
|
50
72
|
field. In order to take advantage of this feature (and ensure that your
|
51
73
|
documents always have unique tokens) remember to create your indexes.
|
74
|
+
Also, `Mongoid::Ids` will never override `to_param`.
|
52
75
|
|
53
76
|
|
54
77
|
## Finders
|
55
78
|
|
56
|
-
|
57
|
-
|
58
|
-
the default behaviour of ObjectIDs is also there). You can disable these
|
59
|
-
with the [`skip_finders` configuration option](#skip-finders-skip_finders).
|
79
|
+
`Mongoid::Ids` will **never** override `find`.
|
80
|
+
There's some helpers for custom fields:
|
60
81
|
|
61
82
|
```ruby
|
62
|
-
Video.
|
63
|
-
Account.
|
83
|
+
Video.find_by_code("x3v98")
|
84
|
+
Account.find_by_token("ACC-123456")
|
64
85
|
```
|
65
86
|
|
87
|
+
You can disable these with the
|
88
|
+
[`skip_finders` configuration option](#skip-finders-skip_finders).
|
66
89
|
|
67
|
-
## Configuration
|
68
90
|
|
69
|
-
|
91
|
+
## Configuration
|
70
92
|
|
71
|
-
|
72
|
-
systems for managing how your tokens look.
|
93
|
+
You may choose between two different systems for how your tokens look:
|
73
94
|
|
74
|
-
For simple setup, you can use
|
75
|
-
|
76
|
-
types of characters to use.
|
95
|
+
For simple setup, you can use combination of the
|
96
|
+
[`length`](#length-length) and [`contains`](#contains-contains),
|
97
|
+
which modify the length and types of characters to use.
|
77
98
|
|
78
99
|
For when you need to generate more complex tokens, you can use the
|
79
|
-
[`pattern`](#patterns-pattern) option, which allows for very low-level
|
80
|
-
structure of your tokens, as well as allowing
|
81
|
-
prefixes, infixes or suffixes.
|
100
|
+
[`pattern`](#patterns-pattern) option, which allows for very low-level
|
101
|
+
control of the precise structure of your tokens, as well as allowing
|
102
|
+
for static strings, like prefixes, infixes or suffixes.
|
82
103
|
|
83
104
|
#### Length (`:length`)
|
84
105
|
|
@@ -87,8 +108,8 @@ This one is easy, it's just an integer.
|
|
87
108
|
__Example:__
|
88
109
|
|
89
110
|
```ruby
|
90
|
-
token :
|
91
|
-
token :
|
111
|
+
token length: 8 # Tokens are now of length 8
|
112
|
+
token length: 12 # Whow, whow, whow. Slow down egghead.
|
92
113
|
```
|
93
114
|
|
94
115
|
You get the idea.
|
@@ -113,17 +134,16 @@ never start with zeros
|
|
113
134
|
|
114
135
|
__Examples:__
|
115
136
|
```ruby
|
116
|
-
token :
|
117
|
-
token :
|
137
|
+
token contains: :alpha_upper, length: 8
|
138
|
+
token contains: :fixed_numeric
|
118
139
|
```
|
119
140
|
|
120
141
|
#### Patterns (`:pattern`)
|
121
142
|
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
options.
|
143
|
+
Patterns allow you fine-grained control over how your tokens look.
|
144
|
+
It's great for generating random data that has a requirements to
|
145
|
+
also have some basic structure. If you use the `:pattern` option,
|
146
|
+
it will override both the `:length` and `:contains` options.
|
127
147
|
|
128
148
|
This was designed to operate in a similar way to something like `strftime`,
|
129
149
|
if the syntax offends you - please open an issue, I'd love to get some
|
@@ -143,7 +163,7 @@ generated character, and are as follows:
|
|
143
163
|
__Example:__
|
144
164
|
|
145
165
|
```ruby
|
146
|
-
token :
|
166
|
+
token pattern: "PRE-%C%C-%d%d%d%d" # Generates something like: 'PRE-ND-3485'
|
147
167
|
```
|
148
168
|
|
149
169
|
You can also add a repetition modifier, which can help improve readability on
|
@@ -152,7 +172,7 @@ more complex patterns. You simply add any integer after the letter.
|
|
152
172
|
__Examples:__
|
153
173
|
|
154
174
|
```ruby
|
155
|
-
token :
|
175
|
+
token pattern: "APP-%d6" # Generates something like; "APP-638924"
|
156
176
|
```
|
157
177
|
|
158
178
|
### Field Name
|
@@ -162,8 +182,8 @@ This is particularly handy to use multiple tokens one a single document.
|
|
162
182
|
|
163
183
|
__Examples:__
|
164
184
|
```ruby
|
165
|
-
token :
|
166
|
-
token :sharing_token, :
|
185
|
+
token length: 6
|
186
|
+
token :sharing_token, length: 12
|
167
187
|
token :yet_another
|
168
188
|
```
|
169
189
|
|
@@ -175,19 +195,7 @@ overrides for the default `find` behaviour used by Mongoid.
|
|
175
195
|
|
176
196
|
__Example:__
|
177
197
|
```ruby
|
178
|
-
token :
|
179
|
-
```
|
180
|
-
|
181
|
-
|
182
|
-
### Override to_param (`:override_to_param`)
|
183
|
-
|
184
|
-
By default, `Mongoid::Ids` will override to_param, to make it an easy
|
185
|
-
drop-in replacement for the default ObjectIDs. If needed, you can turn
|
186
|
-
this behaviour off:
|
187
|
-
|
188
|
-
__Example:__
|
189
|
-
```ruby
|
190
|
-
token :override_to_param => false
|
198
|
+
token skip_finders: true
|
191
199
|
```
|
192
200
|
|
193
201
|
|
@@ -200,12 +208,17 @@ option is for you.
|
|
200
208
|
|
201
209
|
__Examples:__
|
202
210
|
```ruby
|
203
|
-
token :
|
204
|
-
token :
|
211
|
+
token retry_count: 9
|
212
|
+
token retry_count: 0
|
205
213
|
```
|
206
214
|
|
207
215
|
# Notes
|
208
216
|
|
217
|
+
This gem just changes the Mongoid::Token behaviour, use it if you don't
|
218
|
+
need to change ids:
|
219
|
+
|
220
|
+
http://github.com/thetron/mongoid_token
|
221
|
+
|
209
222
|
If you find a problem, please [submit an issue](http://github.com/nofxx/mongoid-ids/issues) (and a failing test, if
|
210
223
|
you can). Pull requests and feature requests are always welcome and
|
211
224
|
greatly appreciated.
|
@@ -214,7 +227,7 @@ Thanks to everyone that has contributed to this gem over the past year.
|
|
214
227
|
Many, many thanks - you guys rawk.
|
215
228
|
|
216
229
|
|
217
|
-
## Contributors:
|
230
|
+
## Mongoid::Token Contributors:
|
218
231
|
|
219
232
|
Thanks to everyone who has provided support for this gem over the years.
|
220
233
|
In particular: [olliem](https://github.com/olliem),
|
data/lib/mongoid/ids.rb
CHANGED
@@ -27,7 +27,6 @@ module Mongoid
|
|
27
27
|
add_token_field_and_index(options)
|
28
28
|
|
29
29
|
define_custom_finders(options) if options.skip_finders? == false
|
30
|
-
override_to_param(options) if options.override_to_param?
|
31
30
|
end
|
32
31
|
end
|
33
32
|
|
@@ -57,12 +56,6 @@ module Mongoid
|
|
57
56
|
document.create_token_if_nil options.field_name, options.pattern
|
58
57
|
end
|
59
58
|
end
|
60
|
-
|
61
|
-
def override_to_param(options)
|
62
|
-
self.send(:define_method, :to_param) do
|
63
|
-
self.send(options.field_name) || super()
|
64
|
-
end
|
65
|
-
end
|
66
59
|
end
|
67
60
|
|
68
61
|
protected
|
data/lib/mongoid/ids/options.rb
CHANGED
@@ -30,10 +30,6 @@ module Mongoid
|
|
30
30
|
@options[:skip_finders]
|
31
31
|
end
|
32
32
|
|
33
|
-
def override_to_param?
|
34
|
-
@options[:override_to_param]
|
35
|
-
end
|
36
|
-
|
37
33
|
def pattern
|
38
34
|
@options[:pattern] ||= \
|
39
35
|
case @options[:contains].to_sym
|
@@ -69,8 +65,7 @@ module Mongoid
|
|
69
65
|
:retry_count => 3,
|
70
66
|
:contains => :alphanumeric,
|
71
67
|
:field_name => :_id,
|
72
|
-
:skip_finders => false
|
73
|
-
:override_to_param => true,
|
68
|
+
:skip_finders => false
|
74
69
|
}.merge(options)
|
75
70
|
end
|
76
71
|
end
|
data/lib/mongoid/ids/version.rb
CHANGED
@@ -50,17 +50,6 @@ describe Mongoid::Ids::Options do
|
|
50
50
|
expect(Mongoid::Ids::Options.new.pattern).to eq('%s4')
|
51
51
|
end
|
52
52
|
|
53
|
-
describe 'override_to_param' do
|
54
|
-
it 'should be an option' do
|
55
|
-
expect(Mongoid::Ids::Options.new(override_to_param: false)
|
56
|
-
.override_to_param?).to eq false
|
57
|
-
end
|
58
|
-
|
59
|
-
it 'should default to true' do
|
60
|
-
expect(Mongoid::Ids::Options.new.override_to_param?).to eq true
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
53
|
describe 'skip_finder' do
|
65
54
|
it 'should be an option' do
|
66
55
|
expect(Mongoid::Ids::Options.new(skip_finders: true)
|
data/spec/mongoid/token_spec.rb
CHANGED
@@ -283,9 +283,9 @@ describe Mongoid::Ids do
|
|
283
283
|
end
|
284
284
|
|
285
285
|
describe '.to_param' do
|
286
|
-
it 'should
|
286
|
+
it 'should be removed from this gem' do
|
287
287
|
document_class.send(:token, :token)
|
288
|
-
expect(document.to_param).to eq document.
|
288
|
+
expect(document.to_param).to eq document.id.to_s # why to_s?
|
289
289
|
end
|
290
290
|
end
|
291
291
|
|