Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions lib/mongoid/token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ def initialize_copy(source)
def token(*args)
options = Mongoid::Token::Options.new(args.extract_options!)

add_token_field_and_index(options)
add_token_field(options)
add_index(options)
add_token_collision_resolver(options)
set_token_callbacks(options)

Expand All @@ -26,9 +27,12 @@ def token(*args)
end

private
def add_token_field_and_index(options)
def add_token_field(options)
self.field options.field_name, :type => String, :default => default_value(options)
self.index({ options.field_name => 1 }, { :unique => true, :sparse => true })
end

def add_index(options)
self.index({ options.field_name => 1 }, { :unique => true, :sparse => true }) unless options.skip_index?
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [112/80]
Redundant curly braces around a hash parameter.
Use the new Ruby 1.9 hash syntax.
Redundant self detected.

end

def add_token_collision_resolver(options)
Expand Down
5 changes: 5 additions & 0 deletions lib/mongoid/token/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ def skip_finders?
@options[:skip_finders]
end

def skip_index?
@options[:skip_index]
end

def override_to_param?
@options[:override_to_param]
end
Expand Down Expand Up @@ -71,6 +75,7 @@ def merge_defaults(options)
contains: :alphanumeric,
field_name: :token,
skip_finders: false,
skip_index: false,
override_to_param: true,
generate_on_init: false
}.merge(options)
Expand Down
2 changes: 1 addition & 1 deletion mongoid_token.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Gem::Specification.new do |s|
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.}

s.rubyforge_project = "mongoid_token"
s.add_dependency 'mongoid', '~> 5.0.0'
s.add_dependency 'mongoid', '>= 5.0'

s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
Expand Down
10 changes: 10 additions & 0 deletions spec/mongoid/token/options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@
end
end

describe "skip_index" do
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inconsistent indentation detected.

it "should be an option" do
expect(Mongoid::Token::Options.new({:skip_index => true}).skip_index?).to eq true
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [87/80]
Redundant curly braces around a hash parameter.
Use the new Ruby 1.9 hash syntax.
Space inside { missing.
Space inside } missing.

end

it "should default to false" do
expect(Mongoid::Token::Options.new.skip_index?).to eq false
end
end
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

end at 79, 2 is not aligned with describe "skip_index" do at 71, 3.


describe "id" do
context "when true" do
it "returns '_id' sa the field name" do
Expand Down