dawarich/app/models/user.rb
2024-04-04 20:14:11 +02:00

39 lines
910 B
Ruby

class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
has_many :imports, dependent: :destroy
has_many :points, through: :imports
has_many :stats
after_create :create_api_key
def export_data
::ExportSerializer.new(points, self.email).call
end
def total_km
Stat.where(user: self).sum(:distance)
end
def total_countries
Stat.where(user: self).pluck(:toponyms).flatten.map { _1['country'] }.uniq.size
end
def total_cities
Stat.where(user: self).pluck(:toponyms).flatten.size
end
def total_reverse_geocoded
points.where.not(country: nil, city: nil).count
end
private
def create_api_key
self.api_key = SecureRandom.hex(16)
save
end
end