Class: Sorcery::Adapters::MongoidAdapter
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from BaseAdapter
delete_all, find, from, #initialize
Class Method Details
.callback_name(time, event, options) ⇒ Object
34
35
36
37
38
39
40
|
# File 'lib/sorcery/adapters/mongoid_adapter.rb', line 34
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
42
43
44
45
46
47
48
|
# File 'lib/sorcery/adapters/mongoid_adapter.rb', line 42
def credential_regex(credential)
if @klass.sorcery_config.downcase_username_before_authenticating
return { :$regex => /^#{Regexp.escape(credential)}$/i }
end
credential
end
|
.define_callback(time, event, method_name, options = {}) ⇒ Object
30
31
32
|
# File 'lib/sorcery/adapters/mongoid_adapter.rb', line 30
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
26
27
28
|
# File 'lib/sorcery/adapters/mongoid_adapter.rb', line 26
def define_field(name, type, options = {})
@klass.field name, options.slice(:default).merge(type: type)
end
|
.find_by_activation_token(token) ⇒ Object
63
64
65
|
# File 'lib/sorcery/adapters/mongoid_adapter.rb', line 63
def find_by_activation_token(token)
@klass.where(@klass.sorcery_config.activation_token_attribute_name => token).first
end
|
.find_by_credentials(credentials) ⇒ Object
50
51
52
53
54
55
56
|
# File 'lib/sorcery/adapters/mongoid_adapter.rb', line 50
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
90
91
92
|
# File 'lib/sorcery/adapters/mongoid_adapter.rb', line 90
def find_by_email(email)
@klass.where(@klass.sorcery_config.email_attribute_name => email).first
end
|
.find_by_id(id) ⇒ Object
75
76
77
78
79
|
# File 'lib/sorcery/adapters/mongoid_adapter.rb', line 75
def find_by_id(id)
@klass.find(id)
rescue ::Mongoid::Errors::DocumentNotFound
nil
end
|
.find_by_oauth_credentials(provider, uid) ⇒ Object
58
59
60
61
|
# File 'lib/sorcery/adapters/mongoid_adapter.rb', line 58
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
67
68
69
|
# File 'lib/sorcery/adapters/mongoid_adapter.rb', line 67
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
86
87
88
|
# File 'lib/sorcery/adapters/mongoid_adapter.rb', line 86
def find_by_token(token_attr_name, token)
@klass.where(token_attr_name => token).first
end
|
.find_by_username(username) ⇒ Object
81
82
83
84
|
# File 'lib/sorcery/adapters/mongoid_adapter.rb', line 81
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 ⇒ Object
94
95
96
97
98
99
100
101
102
103
|
# File 'lib/sorcery/adapters/mongoid_adapter.rb', line 94
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 ⇒ Object
71
72
73
|
# File 'lib/sorcery/adapters/mongoid_adapter.rb', line 71
def transaction(&)
tap(&)
end
|
Instance Method Details
#increment(attr) ⇒ Object
4
5
6
|
# File 'lib/sorcery/adapters/mongoid_adapter.rb', line 4
def increment(attr)
@model.inc(attr => 1)
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
|