Class: Sorcery::Adapters::ActiveRecordAdapter

Inherits:
BaseAdapter
  • Object
show all
Defined in:
lib/sorcery/adapters/active_record_adapter.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseAdapter

delete_all, find, from, #initialize, #update_attribute

Constructor Details

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

Class Method Details

.define_callback(time, event, method_name, options = {}) ⇒ Object



36
37
38
# File 'lib/sorcery/adapters/active_record_adapter.rb', line 36

def define_callback(time, event, method_name, options={})
  @klass.send "#{time}_#{event}", method_name, options.slice(:if)
end

.define_field(name, type, options = {}) ⇒ Object



32
33
34
# File 'lib/sorcery/adapters/active_record_adapter.rb', line 32

def define_field(name, type, options={})
  # AR fields are defined through migrations, only validator here
end

.find_by_activation_token(token) ⇒ Object



80
81
82
# File 'lib/sorcery/adapters/active_record_adapter.rb', line 80

def find_by_activation_token(token)
  @klass.where(@klass.sorcery_config.activation_token_attribute_name => token).first
end

.find_by_credentials(credentials) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/sorcery/adapters/active_record_adapter.rb', line 54

def find_by_credentials(credentials)
  relation = nil

  @klass.sorcery_config.username_attribute_names.each do |attribute|
    if @klass.sorcery_config.downcase_username_before_authenticating
      condition = @klass.arel_table[attribute].lower.eq(@klass.arel_table.lower(credentials[0]))
    else
      condition = @klass.arel_table[attribute].eq(credentials[0])
    end

    if relation.nil?
      relation = condition
    else
      relation = relation.or(condition)
    end
  end

  @klass.where(relation).first
end

.find_by_email(email) ⇒ Object



99
100
101
# File 'lib/sorcery/adapters/active_record_adapter.rb', line 99

def find_by_email(email)
  @klass.where(@klass.sorcery_config.email_attribute_name => email).first
end

.find_by_id(id) ⇒ Object



84
85
86
# File 'lib/sorcery/adapters/active_record_adapter.rb', line 84

def find_by_id(id)
  @klass.find_by_id(id)
end

.find_by_oauth_credentials(provider, uid) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/sorcery/adapters/active_record_adapter.rb', line 40

def find_by_oauth_credentials(provider, uid)
  @user_config ||= ::Sorcery::Controller::Config.user_class.to_s.constantize.sorcery_config
  conditions = {
    @user_config.provider_uid_attribute_name => uid,
    @user_config.provider_attribute_name     => provider
  }

  @klass.where(conditions).first
end

.find_by_remember_me_token(token) ⇒ Object



50
51
52
# File 'lib/sorcery/adapters/active_record_adapter.rb', line 50

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



74
75
76
77
78
# File 'lib/sorcery/adapters/active_record_adapter.rb', line 74

def find_by_token(token_attr_name, token)
  condition = @klass.arel_table[token_attr_name].eq(token)

  @klass.where(condition).first
end

.find_by_username(username) ⇒ Object



88
89
90
91
92
93
94
95
96
97
# File 'lib/sorcery/adapters/active_record_adapter.rb', line 88

def find_by_username(username)
  @klass.sorcery_config.username_attribute_names.each do |attribute|
    if @klass.sorcery_config.downcase_username_before_authenticating
      username = username.downcase
    end

    result = @klass.where(attribute => username).first
    return result if result
  end
end

.get_current_usersObject



103
104
105
106
107
108
109
110
# File 'lib/sorcery/adapters/active_record_adapter.rb', line 103

def get_current_users
  config = @klass.sorcery_config

  @klass
    .where("#{config.last_activity_at_attribute_name} IS NOT NULL") \
    .where("#{config.last_logout_at_attribute_name} IS NULL OR #{config.last_activity_at_attribute_name} > #{config.last_logout_at_attribute_name}") \
    .where("#{config.last_activity_at_attribute_name} > ? ", config.activity_timeout.seconds.ago.utc.to_s(:db))
end

.transaction(&blk) ⇒ Object



112
113
114
# File 'lib/sorcery/adapters/active_record_adapter.rb', line 112

def transaction(&blk)
  @klass.tap(&blk)
end

Instance Method Details

#find_authentication_by_oauth_credentials(relation_name, provider, uid) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/sorcery/adapters/active_record_adapter.rb', line 21

def find_authentication_by_oauth_credentials(relation_name, provider, uid)
  @user_config ||= ::Sorcery::Controller::Config.user_class.to_s.constantize.sorcery_config
  conditions = {
    @user_config.provider_uid_attribute_name => uid,
    @user_config.provider_attribute_name     => provider
  }

  @model.public_send(relation_name).where(conditions).first
end

#increment(field) ⇒ Object



17
18
19
# File 'lib/sorcery/adapters/active_record_adapter.rb', line 17

def increment(field)
  @model.increment!(field)
end

#save(options = {}) ⇒ Object



12
13
14
15
# File 'lib/sorcery/adapters/active_record_adapter.rb', line 12

def save(options = {})
  mthd = options.delete(:raise_on_failure) ? :save! : :save
  @model.send(mthd, options)
end

#update_attributes(attrs) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/sorcery/adapters/active_record_adapter.rb', line 4

def update_attributes(attrs)
  attrs.each do |name, value|
    @model.send(:"#{name}=", value)
  end
  primary_key = @model.class.primary_key
  @model.class.where(:"#{primary_key}" => @model.send(:"#{primary_key}")).update_all(attrs)
end