lib/mongoid/token.rb in mongoid_token-2.2.0 vs lib/mongoid/token.rb in mongoid_token-3.0.0
- old
+ new
@@ -18,18 +18,18 @@
options = Mongoid::Token::Options.new(args.extract_options!)
add_token_field_and_index(options)
add_token_collision_resolver(options)
set_token_callbacks(options)
-
+
define_custom_finders(options) if options.skip_finders? == false
override_to_param(options) if options.override_to_param?
end
private
def add_token_field_and_index(options)
- self.field options.field_name, :type => String, :default => nil
+ self.field options.field_name, :type => String, :default => default_value(options)
self.index({ options.field_name => 1 }, { :unique => true, :sparse => true })
end
def add_token_collision_resolver(options)
resolver = Mongoid::Token::CollisionResolver.new(self, options.field_name, options.retry_count)
@@ -54,9 +54,13 @@
def override_to_param(options)
self.send(:define_method, :to_param) do
self.send(options.field_name) || super()
end
+ end
+
+ def default_value(options)
+ options.generate_on_init && Mongoid::Token::Generator.generate(options.pattern) || nil
end
end
protected
def create_token(field_name, pattern)