Add some refactorings

This commit is contained in:
Eugene Burmakin 2025-09-13 16:41:28 +02:00
parent 608fa41fa8
commit dcd1c7ab2b
8 changed files with 30 additions and 23 deletions

View file

@ -19,7 +19,7 @@ gem 'gpx'
gem 'groupdate'
gem 'httparty'
gem 'importmap-rails'
gem 'jwt'
gem 'jwt', '~> 2.8'
gem 'kaminari'
gem 'lograge'
gem 'oj'

View file

@ -545,7 +545,7 @@ DEPENDENCIES
groupdate
httparty
importmap-rails
jwt
jwt (~> 2.8)
kaminari
lograge
oj

View file

@ -46,7 +46,7 @@ class ApplicationController < ActionController::Base
end
def user_not_authorized
redirect_back_or_to root_path,
redirect_back fallback_location: root_path,
alert: 'You are not authorized to perform this action.',
status: :see_other
end

View file

@ -2,8 +2,8 @@
class Settings::UsersController < ApplicationController
before_action :authenticate_self_hosted!, except: %i[export import]
before_action :authenticate_admin!, except: %i[export import]
before_action :authenticate_user!
before_action :authenticate_admin!, except: %i[export import]
def index
@users = User.order(created_at: :desc)

View file

@ -21,7 +21,7 @@ class StatsController < ApplicationController
@month = params[:month].to_i
@stat = current_user.stats.find_by(year: @year, month: @month)
@previous_stat = current_user.stats.find_by(year: @year, month: @month - 1) if @month > 1
@average_distance_this_year = current_user.stats.where(year: @year).average(:distance) / 1000
@average_distance_this_year = current_user.stats.where(year: @year).average(:distance).to_i / 1000
end
def update

View file

@ -67,10 +67,11 @@ module StatsHelper
end
def x_than_average_distance(stat, average_distance_this_year)
return '' if average_distance_this_year.zero?
return '' if average_distance_this_year&.zero?
difference = stat.distance / 1000 - average_distance_this_year
percentage = ((difference / average_distance_this_year) * 100).round
current_km = stat.distance / 1000.0
difference = current_km - average_distance_this_year.to_f
percentage = ((difference / average_distance_this_year.to_f) * 100).round
more_or_less = difference.positive? ? 'more' : 'less'
"#{percentage.abs}% #{more_or_less} than your average this year"

View file

@ -37,10 +37,19 @@ class Stat < ApplicationRecord
end
def sharing_expired?
return false unless sharing_settings['expiration']
return false if sharing_settings['expiration'] == 'permanent'
expiration = sharing_settings['expiration']
return false if expiration.blank? || expiration == 'permanent'
Time.current > sharing_settings['expires_at']
expires_at_value = sharing_settings['expires_at']
return true if expires_at_value.blank?
expires_at = begin
Time.zone.parse(expires_at_value)
rescue StandardError
nil
end
expires_at.present? ? Time.current > expires_at : true
end
def public_accessible?
@ -53,12 +62,9 @@ class Stat < ApplicationRecord
def enable_sharing!(expiration: '1h')
expires_at = case expiration
when '1h'
1.hour.from_now
when '12h'
12.hours.from_now
when '24h'
24.hours.from_now
when '1h' then 1.hour.from_now
when '12h' then 12.hours.from_now
when '24h' then 24.hours.from_now
end
update!(

View file

@ -47,8 +47,8 @@ class HexagonQuery
WHERE #{user_filter}
#{date_filter}
AND ST_Intersects(
lonlat::geometry,
(SELECT geom FROM bbox_geom)
lonlat,
(SELECT geom FROM bbox_geom)::geometry
)
),
hex_grid AS (