Class: Sorcery::Adapters::MongoidAdapter

Inherits:
BaseAdapter show all
Defined in:
lib/sorcery/adapters/mongoid_adapter.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseAdapter

delete_all, find, from, #initialize

Constructor Details

This class inherits a constructor from Sorcery::Adapters::BaseAdapter

Class Method Details

.callback_name(time, event, options) ⇒ Object



38
39
40
41
42
43
44
# 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) ⇒ Object



46
47
48
49
50
# 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 = {}) ⇒ Object



34
35
36
# 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 = {}) ⇒ Object



30
31
32
# 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) ⇒ Object



65
66
67
# 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) ⇒ Object



52
53
54
55
56
57
58
# 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) ⇒ Object



92
93
94
# 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) ⇒ Object



77
78
79
80
81
# 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) ⇒ Object



60
61
62
63
# 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) ⇒ Object



69
70
71
# 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) ⇒ Object



88
89
90
# 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) ⇒ Object



83
84
85
86
# 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_usersObject



96
97
98
99
100
101
102
103
104
105
# 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) ⇒ Object



73
74
75
# File 'lib/sorcery/adapters/mongoid_adapter.rb', line 73

def transaction(&blk)
  tap(&blk)
end

Instance Method Details

#increment(attr) ⇒ Object



4
5
6
# 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?Boolean

Returns:

  • (Boolean)


25
26
27
# 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 = {}) ⇒ Object



20
21
22
23
# 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) ⇒ Object



16
17
18
# File 'lib/sorcery/adapters/mongoid_adapter.rb', line 16

def update_attribute(name, value)
  update_attributes(name => value)
end

#update_attributes(attrs) ⇒ Object



8
9
10
11
12
13
14
# 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