lib/sorcery/adapters/mongoid_adapter.rb in sorcery-0.12.0 vs lib/sorcery/adapters/mongoid_adapter.rb in sorcery-0.13.0
- old
+ new
@@ -8,11 +8,11 @@
def update_attributes(attrs)
attrs.each do |name, value|
attrs[name] = value.utc if value.is_a?(ActiveSupport::TimeWithZone)
@model.send(:"#{name}=", value)
end
- @model.class.where(:_id => @model.id).update_all(attrs)
+ @model.class.where(_id: @model.id).update_all(attrs)
end
def update_attribute(name, value)
update_attributes(name => value)
end
@@ -21,25 +21,33 @@
mthd = options.delete(:raise_on_failure) ? :save! : :save
@model.send(mthd, options)
end
def mongoid_4?
- Gem::Version.new(::Mongoid::VERSION) >= Gem::Version.new("4.0.0.alpha")
+ Gem::Version.new(::Mongoid::VERSION) >= Gem::Version.new('4.0.0.alpha')
end
class << self
-
- def define_field(name, type, options={})
+ def define_field(name, type, options = {})
@klass.field name, options.slice(:default).merge(type: type)
end
- def define_callback(time, event, method_name, options={})
- @klass.send "#{time}_#{event}", method_name, options.slice(:if)
+ def define_callback(time, event, method_name, options = {})
+ @klass.send callback_name(time, event, options), method_name, options.slice(:if)
end
+ def callback_name(time, event, options)
+ if event == :commit
+ options[:on] == :create ? "#{time}_create" : "#{time}_save"
+ else
+ "#{time}_#{event}"
+ end
+ end
+
def credential_regex(credential)
- return { :$regex => /^#{Regexp.escape(credential)}$/i } if (@klass.sorcery_config.downcase_username_before_authenticating)
+ return { :$regex => /^#{Regexp.escape(credential)}$/i } if @klass.sorcery_config.downcase_username_before_authenticating
+
credential
end
def find_by_credentials(credentials)
@klass.sorcery_config.username_attribute_names.each do |attribute|
@@ -71,11 +79,11 @@
rescue ::Mongoid::Errors::DocumentNotFound
nil
end
def find_by_username(username)
- query = @klass.sorcery_config.username_attribute_names.map {|name| {name => username}}
+ query = @klass.sorcery_config.username_attribute_names.map { |name| { name => username } }
@klass.any_of(*query).first
end
def find_by_token(token_attr_name, token)
@klass.where(token_attr_name => token).first
@@ -85,12 +93,16 @@
@klass.where(@klass.sorcery_config.email_attribute_name => email).first
end
def get_current_users
config = @klass.sorcery_config
- @klass.where(config.last_activity_at_attribute_name.ne => nil) \
- .where("this.#{config.last_logout_at_attribute_name} == null || this.#{config.last_activity_at_attribute_name} > this.#{config.last_logout_at_attribute_name}") \
- .where(config.last_activity_at_attribute_name.gt => config.activity_timeout.seconds.ago.utc).order_by([:_id,:asc])
+ @klass.where(
+ config.last_activity_at_attribute_name.ne => nil
+ ).where(
+ "this.#{config.last_logout_at_attribute_name} == null || this.#{config.last_activity_at_attribute_name} > this.#{config.last_logout_at_attribute_name}"
+ ).where(
+ config.last_activity_at_attribute_name.gt => config.activity_timeout.seconds.ago.utc
+ ).order_by(%i[_id asc])
end
end
end
end
end