mirror of
https://github.com/Freika/dawarich.git
synced 2026-01-10 17:21:38 -05:00
58 lines
1.3 KiB
Ruby
58 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
|
|
|
|
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
|