mongoid_token 2.2.0 → 3.0.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.
- checksums.yaml +4 -4
- data/README.md +8 -11
- data/lib/mongoid/token/finders.rb +0 -9
- data/lib/mongoid/token/options.rb +13 -7
- data/lib/mongoid/token.rb +6 -2
- data/lib/version.rb +1 -1
- data/spec/mongoid/token/finders_spec.rb +0 -46
- data/spec/mongoid/token/options_spec.rb +32 -0
- data/spec/mongoid/token_spec.rb +25 -0
- 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: 8c76063246f7bcb7225d68d373cfbc1b1ec569e1
|
4
|
+
data.tar.gz: cb1d18edee2bd3c1b201d51345c0d28bf0bdecf0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7f5e3b6924961274b881d0e9fa74fb524fad4eb622135d2668e4756ffc97c1ff28700d19db3d046aaaba22a9ed5a688efaf95cc1e361fb279b2aeebf74c1778a
|
7
|
+
data.tar.gz: f801efb0a8d8541b08d0b15c9dc777bbb821a37b6a4dfeac4a4b32096276e6a6117d3dabd9450995579e00262cdb01f68b9d756ae979566c602ae139c03ccf6d
|
data/README.md
CHANGED
@@ -60,20 +60,18 @@ in your app
|
|
60
60
|
|
61
61
|
## Finders
|
62
62
|
|
63
|
-
|
64
|
-
|
63
|
+
By default, the gem will add a convenience `find_by_[token name]` method, to
|
64
|
+
make it a tiny bit simpler to query by your tokens. If you'd rather it didn't,
|
65
|
+
you can disable these with the
|
66
|
+
[`skip_finders` configuration option](#skip-finders-skip_finders).
|
65
67
|
|
66
|
-
|
67
|
-
in order to search for documents based on their token first (although
|
68
|
-
the default behaviour of ObjectIDs is also there). You can disable these
|
69
|
-
with the [`skip_finders` configuration option](#skip-finders-skip_finders).
|
68
|
+
The methods accept either a single token, or an array of tokens:
|
70
69
|
|
71
70
|
```ruby
|
72
|
-
Video.
|
73
|
-
Account.
|
71
|
+
Video.find_by_token("x3v98") # If your token was named 'token'
|
72
|
+
Account.find_by_account_number(["ACC-123456", "ACC-567890"]) # If your token was named 'account_number'
|
74
73
|
```
|
75
74
|
|
76
|
-
|
77
75
|
## Configuration
|
78
76
|
|
79
77
|
### Tokens
|
@@ -184,8 +182,7 @@ token :field_name => :yet_another
|
|
184
182
|
|
185
183
|
### Skip Finders (`:skip_finders`)
|
186
184
|
|
187
|
-
This will prevent the gem from creating the
|
188
|
-
overrides for the default `find` behaviour used by Mongoid.
|
185
|
+
This will prevent the gem from creating the extra `find_by_*` methods.
|
189
186
|
|
190
187
|
__Example:__
|
191
188
|
```ruby
|
@@ -9,15 +9,6 @@ module Mongoid
|
|
9
9
|
self.find_by field_name.to_sym => token
|
10
10
|
end
|
11
11
|
end
|
12
|
-
|
13
|
-
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
|
14
|
-
warn "[DEPRECATION] Using `find' from Mongoid::Token has been deprecated and will be removed in version 3.0.0."
|
15
|
-
args.all?{|arg| BSON::ObjectId.legal?(arg)} ? send(:"find_without_#{field_name}",*args) : klass.send(:"find_by_#{field_name.to_s}", args.first)
|
16
|
-
end
|
17
|
-
|
18
|
-
# this craziness taken from, and then compacted into a string class_eval
|
19
|
-
# http://geoffgarside.co.uk/2007/02/19/activesupport-alias-method-chain-modules-and-class-methods/
|
20
|
-
klass.class_eval("class << self; alias_method_chain :find, :#{field_name} if self.method_defined?(:find); end")
|
21
12
|
end
|
22
13
|
end
|
23
14
|
end
|
@@ -16,7 +16,7 @@ class Mongoid::Token::Options
|
|
16
16
|
end
|
17
17
|
|
18
18
|
def field_name
|
19
|
-
@options[:field_name]
|
19
|
+
!@options[:id] && @options[:field_name] || :_id
|
20
20
|
end
|
21
21
|
|
22
22
|
def skip_finders?
|
@@ -27,6 +27,10 @@ class Mongoid::Token::Options
|
|
27
27
|
@options[:override_to_param]
|
28
28
|
end
|
29
29
|
|
30
|
+
def generate_on_init
|
31
|
+
@options[:id] || @options[:generate_on_init]
|
32
|
+
end
|
33
|
+
|
30
34
|
def pattern
|
31
35
|
@options[:pattern] ||= case @options[:contains].to_sym
|
32
36
|
when :alphanumeric
|
@@ -61,12 +65,14 @@ class Mongoid::Token::Options
|
|
61
65
|
|
62
66
|
def merge_defaults(options)
|
63
67
|
{
|
64
|
-
:
|
65
|
-
:
|
66
|
-
:
|
67
|
-
:
|
68
|
-
:
|
69
|
-
:
|
68
|
+
id: false,
|
69
|
+
length: 4,
|
70
|
+
retry_count: 3,
|
71
|
+
contains: :alphanumeric,
|
72
|
+
field_name: :token,
|
73
|
+
skip_finders: false,
|
74
|
+
override_to_param: true,
|
75
|
+
generate_on_init: false
|
70
76
|
}.merge(options)
|
71
77
|
end
|
72
78
|
end
|
data/lib/mongoid/token.rb
CHANGED
@@ -20,14 +20,14 @@ module Mongoid
|
|
20
20
|
add_token_field_and_index(options)
|
21
21
|
add_token_collision_resolver(options)
|
22
22
|
set_token_callbacks(options)
|
23
|
-
|
23
|
+
|
24
24
|
define_custom_finders(options) if options.skip_finders? == false
|
25
25
|
override_to_param(options) if options.override_to_param?
|
26
26
|
end
|
27
27
|
|
28
28
|
private
|
29
29
|
def add_token_field_and_index(options)
|
30
|
-
self.field options.field_name, :type => String, :default =>
|
30
|
+
self.field options.field_name, :type => String, :default => default_value(options)
|
31
31
|
self.index({ options.field_name => 1 }, { :unique => true, :sparse => true })
|
32
32
|
end
|
33
33
|
|
@@ -57,6 +57,10 @@ module Mongoid
|
|
57
57
|
self.send(options.field_name) || super()
|
58
58
|
end
|
59
59
|
end
|
60
|
+
|
61
|
+
def default_value(options)
|
62
|
+
options.generate_on_init && Mongoid::Token::Generator.generate(options.pattern) || nil
|
63
|
+
end
|
60
64
|
end
|
61
65
|
|
62
66
|
protected
|
data/lib/version.rb
CHANGED
@@ -1,13 +1,7 @@
|
|
1
1
|
require File.join(File.dirname(__FILE__), %w[.. .. spec_helper])
|
2
2
|
|
3
3
|
describe Mongoid::Token::Finders do
|
4
|
-
before do
|
5
|
-
@orig_stderr = $stderr
|
6
|
-
$stderr = StringIO.new
|
7
|
-
end
|
8
|
-
|
9
4
|
after do
|
10
|
-
$stderr = @orig_stderr
|
11
5
|
Object.send(:remove_const, :Document) if Object.constants.include?(:Document)
|
12
6
|
Object.send(:remove_const, :AnotherDocument) if Object.constants.include?(:AnotherDocument)
|
13
7
|
end
|
@@ -19,20 +13,6 @@ describe Mongoid::Token::Finders do
|
|
19
13
|
klass.singleton_methods.should include(:"find_by_#{field}")
|
20
14
|
end
|
21
15
|
|
22
|
-
it "override the `find` method of the document" do
|
23
|
-
klass = Class.new
|
24
|
-
klass.define_singleton_method(:find) {|*args| :original_find }
|
25
|
-
klass.define_singleton_method(:find_by) {|*args| :token_find }
|
26
|
-
|
27
|
-
Mongoid::Token::Finders.define_custom_token_finder_for(klass)
|
28
|
-
|
29
|
-
klass.find(BSON::ObjectId.new).should == :original_find
|
30
|
-
klass.find(BSON::ObjectId.new, BSON::ObjectId.new).should == :original_find
|
31
|
-
klass.find().should == :original_find
|
32
|
-
klass.find(BSON::ObjectId.new, "token").should == :token_find
|
33
|
-
klass.find("token").should == :token_find
|
34
|
-
end
|
35
|
-
|
36
16
|
it "retrieve a document using the dynamic finder" do
|
37
17
|
class Document; include Mongoid::Document; field :token; end
|
38
18
|
document = Document.create!(:token => "1234")
|
@@ -47,30 +27,4 @@ describe Mongoid::Token::Finders do
|
|
47
27
|
Mongoid::Token::Finders.define_custom_token_finder_for(Document)
|
48
28
|
Document.find_by_token(["1234", "5678"]).should == [document, document2]
|
49
29
|
end
|
50
|
-
|
51
|
-
it "retrieve a document using the `find` method" do
|
52
|
-
class AnotherDocument; include Mongoid::Document; field :token; end
|
53
|
-
document = AnotherDocument.create! :token => "1234"
|
54
|
-
Mongoid::Token::Finders.define_custom_token_finder_for(AnotherDocument)
|
55
|
-
AnotherDocument.find("1234").should == document
|
56
|
-
end
|
57
|
-
|
58
|
-
it 'retrieves multiple documents using the `find` method' do
|
59
|
-
class AnotherDocument; include Mongoid::Document; field :token; end
|
60
|
-
document = AnotherDocument.create! :token => "1234"
|
61
|
-
document2 = AnotherDocument.create! :token => "5678"
|
62
|
-
Mongoid::Token::Finders.define_custom_token_finder_for(AnotherDocument)
|
63
|
-
AnotherDocument.find(["1234", "5678"]).should == [document, document2]
|
64
|
-
end
|
65
|
-
|
66
|
-
describe :deprecations do
|
67
|
-
it "includes `find`" do
|
68
|
-
class AnotherDocument; include Mongoid::Document; field :token; end
|
69
|
-
document = AnotherDocument.create! :token => "1234"
|
70
|
-
Mongoid::Token::Finders.define_custom_token_finder_for(AnotherDocument)
|
71
|
-
AnotherDocument.find("1234")
|
72
|
-
$stderr.rewind
|
73
|
-
$stderr.string.chomp.should start_with("[DEPRECATION]"), "Expected deprecation warning for `find` method"
|
74
|
-
end
|
75
|
-
end
|
76
30
|
end
|
@@ -67,4 +67,36 @@ describe Mongoid::Token::Options do
|
|
67
67
|
expect(Mongoid::Token::Options.new.skip_finders?).to eq false
|
68
68
|
end
|
69
69
|
end
|
70
|
+
|
71
|
+
describe "id" do
|
72
|
+
context "when true" do
|
73
|
+
it "returns '_id' sa the field name" do
|
74
|
+
expect(Mongoid::Token::Options.new({id: true, field_name: :a_token}).field_name).to eq :_id
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
context "when false" do
|
79
|
+
it "returns the field_name option as the field name" do
|
80
|
+
expect(Mongoid::Token::Options.new({id: false, field_name: :a_token}).field_name).to eq :a_token
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
describe :generate_on_init do
|
86
|
+
it "defaults to false" do
|
87
|
+
expect(Mongoid::Token::Options.new({}).generate_on_init).to eq false
|
88
|
+
end
|
89
|
+
|
90
|
+
context "when id option set" do
|
91
|
+
it "is true" do
|
92
|
+
expect(Mongoid::Token::Options.new({id: true}).generate_on_init).to eq true
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
context "when id option is not set" do
|
97
|
+
it "is false" do
|
98
|
+
expect(Mongoid::Token::Options.new({id: false}).generate_on_init).to eq false
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
70
102
|
end
|
data/spec/mongoid/token_spec.rb
CHANGED
@@ -4,6 +4,7 @@ describe Mongoid::Token do
|
|
4
4
|
after do
|
5
5
|
Object.send(:remove_const, :Document) if Object.constants.include?(:Document)
|
6
6
|
Object.send(:remove_const, :AnotherDocument) if Object.constants.include?(:AnotherDocument)
|
7
|
+
Object.send(:remove_const, :UntaintedDocument) if Object.constants.include?(:UntaintedDocument)
|
7
8
|
end
|
8
9
|
|
9
10
|
let(:document_class) do
|
@@ -259,4 +260,28 @@ describe Mongoid::Token do
|
|
259
260
|
end
|
260
261
|
end
|
261
262
|
end
|
263
|
+
|
264
|
+
describe "with overriden id" do
|
265
|
+
# Capture warnings about overriding _id, for cleaner test output
|
266
|
+
before do
|
267
|
+
@orig_stdout = $stdout
|
268
|
+
$stdout = StringIO.new
|
269
|
+
document_class.send(:token, id: true)
|
270
|
+
end
|
271
|
+
|
272
|
+
after do
|
273
|
+
$stdout = @orig_stdout
|
274
|
+
end
|
275
|
+
|
276
|
+
let(:document){ document_class.new }
|
277
|
+
|
278
|
+
it "should replace the _id field with a token" do
|
279
|
+
expect(document).to have_field(:_id)
|
280
|
+
expect(document).to_not have_field(:token)
|
281
|
+
end
|
282
|
+
|
283
|
+
it "should have a default token" do
|
284
|
+
expect(document.id).to_not be_blank
|
285
|
+
end
|
286
|
+
end
|
262
287
|
end
|