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



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

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

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



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

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

.find_by_activation_token(token) ⇒ Object



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

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

.find_by_credentials(credentials) ⇒ Object



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

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

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

  @klass.where(relation).first
end

.find_by_email(email) ⇒ Object



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

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

.find_by_id(id) ⇒ Object



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

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

.find_by_oauth_credentials(provider, uid) ⇒ Object



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

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



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

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



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

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



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

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

.transaction(&blk) ⇒ Object



104
105
106
# File 'lib/sorcery/adapters/active_record_adapter.rb', line 104

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

Instance Method Details

#find_authentication_by_oauth_credentials(relation_name, provider, uid) ⇒ Object



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

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



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

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

#save(options = {}) ⇒ Object



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

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
11
# 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
  updated_count = @model.class.where(:"#{primary_key}" => @model.send(:"#{primary_key}")).update_all(attrs)
  updated_count == 1
end