Class: User

Inherits:
ApplicationRecord show all
Includes:
DeviseTokenAuth::Concerns::ActiveRecordSupport, DeviseTokenAuth::Concerns::User
Defined in:
app/models/user.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.assign_property(user_id, property_id) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/models/user.rb', line 42

def self.assign_property(user_id, property_id)
  user = User.find_by_id(user_id)

  if user
    ids = user.properties ? user.properties['property_ids'] : nil
    if ids.present?
      ids = ids.to_s.split(',')
      ids = ids.map(&:to_i)
      ids.push(property_id)
      ids = ids.uniq
      user.properties['property_ids'] = ids.join(',')
    else
      properties = {
        'property_ids': property_id.to_s
      }
      user.properties = properties
    end
    user.save
  end
end

.from_omniauth(auth) ⇒ Object



72
73
74
75
76
77
78
79
80
81
# File 'app/models/user.rb', line 72

def self.from_omniauth(auth)
  where(provider: auth.provider.to_s, uid: auth.uid.to_s).first_or_create do |user|
    user.provider = auth.provider
    user.uid = auth.uid
    user.email = auth.info.email
    user.password = Devise.friendly_token[0, 20]
    user.name = auth.info.name
    user.image = auth.info.image
  end
end

.generate_uidObject



87
88
89
90
91
92
# File 'app/models/user.rb', line 87

def self.generate_uid
  loop do
    token = Devise.friendly_token
    break token unless to_adapter.find_first({ uid: token })
  end
end

.inspector_name(id) ⇒ Object



63
64
65
66
# File 'app/models/user.rb', line 63

def self.inspector_name(id)
  user = User.find_by_id(id)
  user.first_name + ' ' + user.last_name unless user.nil?
end

.search(username, type) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/models/user.rb', line 18

def self.search(username, type)
  if username && type && username != '' && type != ''
    where('username ILIKE ?', "%#{username}%")
      .where('user_type LIKE ?', type.to_s)
  elsif username && username != ''
    where('username ILIKE ?', "%#{username}%")
  elsif type && type != ''
    where('user_type LIKE ?', type.to_s)
  else
    all
  end
end

.unassign_property(user_id, property_id) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'app/models/user.rb', line 31

def self.unassign_property(user_id, property_id)
  user = User.find_by_id(user_id)
  if user&.properties
    ids = user.properties['property_ids'].to_s.split(',')
    ids = ids.map(&:to_i)
    ids.delete(property_id) if ids.include? property_id
    user.properties['property_ids'] = ids.join(',')
    user.save
  end
end

Instance Method Details

#confirmed_atObject



68
69
70
# File 'app/models/user.rb', line 68

def confirmed_at
  Time.now.utc
end

#set_uidObject



83
84
85
# File 'app/models/user.rb', line 83

def set_uid
  self.uid = self.class.generate_uid if uid.blank?
end