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



43
44
45
46
47
48
# File 'app/models/user.rb', line 43

def self.assign_property(user_id, property_id)
  user = User.find_by_id(user_id)
  if user
    user.user_properties.create(property_id: property_id)
  end
end

.from_omniauth(auth) ⇒ Object



59
60
61
62
63
64
65
66
67
68
# File 'app/models/user.rb', line 59

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



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

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



50
51
52
53
# File 'app/models/user.rb', line 50

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



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

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



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

def self.unassign_property(user_id, property_id)
  user = User.find_by_id(user_id)
  if user
    user_property = user.user_properties.find_by(property_id: property_id)
    if user_property
      user_property.destroy
    end
  end
end

Instance Method Details

#confirmed_atObject



55
56
57
# File 'app/models/user.rb', line 55

def confirmed_at
  Time.now.utc
end

#set_uidObject



70
71
72
# File 'app/models/user.rb', line 70

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

#sync_propertiesObject



81
82
83
84
85
86
87
88
89
90
# File 'app/models/user.rb', line 81

def sync_properties
  if self[:properties].present? && self[:properties].key?("property_ids")
    property_ids = self[:properties]["property_ids"].split(',').map(&:strip)
    property_ids.each do |property_id|
      unless self.user_properties.exists?(property_id: property_id)
        self.user_properties.create(property_id: property_id)
      end
    end
  end
end