dawarich/app/models/user.rb
Eugene Burmakin 151e5cf042 Add areas
2024-07-21 20:09:42 +02:00

59 lines
1.3 KiB
Ruby

# frozen_string_literal: true
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, dependent: :destroy
has_many :tracked_points, class_name: 'Point', dependent: :destroy
has_many :exports, dependent: :destroy
has_many :notifications, dependent: :destroy
has_many :areas, dependent: :destroy
after_create :create_api_key
def countries_visited
stats.pluck(:toponyms).flatten.map { _1['country'] }.uniq.compact
end
def cities_visited
stats
.where.not(toponyms: nil)
.pluck(:toponyms)
.flatten
.reject { |toponym| toponym['cities'].blank? }
.pluck('cities')
.flatten
.pluck('city')
.uniq
.compact
end
def total_km
stats.sum(:distance)
end
def total_countries
countries_visited.size
end
def total_cities
cities_visited.size
end
def total_reverse_geocoded
tracked_points.select(:id).where.not(geodata: {}).count
end
private
def create_api_key
self.api_key = SecureRandom.hex(16)
save
end
end