Module: Challah::Authorizeable::ClassMethods

Defined in:
lib/challah/concerns/authorizeable.rb

Instance Method Summary collapse

Instance Method Details

#del(options = {}) ⇒ Object

Remove an authorization



21
22
23
24
25
26
# File 'lib/challah/concerns/authorizeable.rb', line 21

def del(options = {})
  provider  = options.fetch(:provider)
  user_id   = options.fetch(:user_id)

  where(provider: provider, user_id: user_id).delete_all
end

#get(options = {}) ⇒ Object

Grab the user/provider record



29
30
31
32
33
34
# File 'lib/challah/concerns/authorizeable.rb', line 29

def get(options = {})
  provider  = options.fetch(:provider)
  user_id   = options.fetch(:user_id)

  where(provider: provider, user_id: user_id).first
end

#hashable_attributesObject



15
16
17
18
# File 'lib/challah/concerns/authorizeable.rb', line 15

def hashable_attributes
  protected_attributes = %w( user_id provider last_session_at last_session_ip session_count created_at updated_at )
  @hashable_attributes ||= self.columns.map(&:name) - protected_attributes
end

#set(options = {}) ⇒ Object

Create a new authorization record for the given user



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/challah/concerns/authorizeable.rb', line 37

def set(options = {})
  provider    = options.delete(:provider)
  user_id     = options.delete(:user_id).to_i
  uid         = options.delete(:uid)
  token       = options.delete(:token)
  expires_at  = options.delete(:expires_at) || nil

  del(provider: provider, user_id: user_id)

  record = self.new()
  record.provider = provider
  record.user_id = user_id
  record.uid = uid
  record.token = token
  record.expires_at = expires_at

  record.attributes = options if options.any?

  record.save!
  record
end

#user_modelObject



63
64
65
# File 'lib/challah/concerns/authorizeable.rb', line 63

def user_model
  @user_model ||= Challah.user
end

#users_table_nameObject



59
60
61
# File 'lib/challah/concerns/authorizeable.rb', line 59

def users_table_name
  @users_table_name ||= user_model.table_name
end