class Sorcery::Adapters::MongoidAdapter

Public Class Methods

callback_name(time, event, options) click to toggle source
# File lib/sorcery/adapters/mongoid_adapter.rb, line 38
def callback_name(time, event, options)
  if event == :commit
    options[:on] == :create ? "#{time}_create" : "#{time}_save"
  else
    "#{time}_#{event}"
  end
end
credential_regex(credential) click to toggle source
# File lib/sorcery/adapters/mongoid_adapter.rb, line 46
def credential_regex(credential)
  return { :$regex => /^#{Regexp.escape(credential)}$/i } if @klass.sorcery_config.downcase_username_before_authenticating

  credential
end
define_callback(time, event, method_name, options = {}) click to toggle source
# File lib/sorcery/adapters/mongoid_adapter.rb, line 34
def define_callback(time, event, method_name, options = {})
  @klass.send callback_name(time, event, options), method_name, options.slice(:if)
end
define_field(name, type, options = {}) click to toggle source
# File lib/sorcery/adapters/mongoid_adapter.rb, line 30
def define_field(name, type, options = {})
  @klass.field name, options.slice(:default).merge(type: type)
end
find_by_activation_token(token) click to toggle source
# File lib/sorcery/adapters/mongoid_adapter.rb, line 65
def find_by_activation_token(token)
  @klass.where(@klass.sorcery_config.activation_token_attribute_name => token).first
end
find_by_credentials(credentials) click to toggle source
# File lib/sorcery/adapters/mongoid_adapter.rb, line 52
def find_by_credentials(credentials)
  @klass.sorcery_config.username_attribute_names.each do |attribute|
    @user = @klass.where(attribute => credential_regex(credentials[0])).first
    break if @user
  end
  @user
end
find_by_email(email) click to toggle source
# File lib/sorcery/adapters/mongoid_adapter.rb, line 92
def find_by_email(email)
  @klass.where(@klass.sorcery_config.email_attribute_name => email).first
end
find_by_id(id) click to toggle source
# File lib/sorcery/adapters/mongoid_adapter.rb, line 77
def find_by_id(id)
  @klass.find(id)
rescue ::Mongoid::Errors::DocumentNotFound
  nil
end
find_by_oauth_credentials(provider, uid) click to toggle source
# File lib/sorcery/adapters/mongoid_adapter.rb, line 60
def find_by_oauth_credentials(provider, uid)
  @user_config ||= ::Sorcery::Controller::Config.user_class.to_s.constantize.sorcery_config
  @klass.where(@user_config.provider_attribute_name => provider, @user_config.provider_uid_attribute_name => uid).first
end
find_by_remember_me_token(token) click to toggle source
# File lib/sorcery/adapters/mongoid_adapter.rb, line 69
def find_by_remember_me_token(token)
  @klass.where(@klass.sorcery_config.remember_me_token_attribute_name => token).first
end
find_by_token(token_attr_name, token) click to toggle source
# File lib/sorcery/adapters/mongoid_adapter.rb, line 88
def find_by_token(token_attr_name, token)
  @klass.where(token_attr_name => token).first
end
find_by_username(username) click to toggle source
# File lib/sorcery/adapters/mongoid_adapter.rb, line 83
def find_by_username(username)
  query = @klass.sorcery_config.username_attribute_names.map { |name| { name => username } }
  @klass.any_of(*query).first
end
get_current_users() click to toggle source
# File lib/sorcery/adapters/mongoid_adapter.rb, line 96
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(%i[_id asc])
end
transaction(&blk) click to toggle source
# File lib/sorcery/adapters/mongoid_adapter.rb, line 73
def transaction(&blk)
  tap(&blk)
end

Public Instance Methods

increment(attr) click to toggle source
# File lib/sorcery/adapters/mongoid_adapter.rb, line 4
def increment(attr)
  mongoid_4? ? @model.inc(attr => 1) : @model.inc(attr, 1)
end
mongoid_4?() click to toggle source
# File lib/sorcery/adapters/mongoid_adapter.rb, line 25
def mongoid_4?
  Gem::Version.new(::Mongoid::VERSION) >= Gem::Version.new('4.0.0.alpha')
end
save(options = {}) click to toggle source
# File lib/sorcery/adapters/mongoid_adapter.rb, line 20
def save(options = {})
  mthd = options.delete(:raise_on_failure) ? :save! : :save
  @model.send(mthd, options)
end
update_attribute(name, value) click to toggle source
# File lib/sorcery/adapters/mongoid_adapter.rb, line 16
def update_attribute(name, value)
  update_attributes(name => value)
end
update_attributes(attrs) click to toggle source
# File lib/sorcery/adapters/mongoid_adapter.rb, line 8
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)
end