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 'groupdate'
gem 'httparty' gem 'httparty'
gem 'importmap-rails' gem 'importmap-rails'
gem 'jwt' gem 'jwt', '~> 2.8'
gem 'kaminari' gem 'kaminari'
gem 'lograge' gem 'lograge'
gem 'oj' gem 'oj'

View file

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

View file

@ -46,8 +46,8 @@ class ApplicationController < ActionController::Base
end end
def user_not_authorized 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.', alert: 'You are not authorized to perform this action.',
status: :see_other status: :see_other
end end
end end

View file

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

View file

@ -21,7 +21,7 @@ class StatsController < ApplicationController
@month = params[:month].to_i @month = params[:month].to_i
@stat = current_user.stats.find_by(year: @year, month: @month) @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 @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 end
def update def update

View file

@ -67,10 +67,11 @@ module StatsHelper
end end
def x_than_average_distance(stat, average_distance_this_year) 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 current_km = stat.distance / 1000.0
percentage = ((difference / average_distance_this_year) * 100).round 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' more_or_less = difference.positive? ? 'more' : 'less'
"#{percentage.abs}% #{more_or_less} than your average this year" "#{percentage.abs}% #{more_or_less} than your average this year"

View file

@ -37,10 +37,19 @@ class Stat < ApplicationRecord
end end
def sharing_expired? def sharing_expired?
return false unless sharing_settings['expiration'] expiration = sharing_settings['expiration']
return false if sharing_settings['expiration'] == 'permanent' 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 end
def public_accessible? def public_accessible?
@ -53,12 +62,9 @@ class Stat < ApplicationRecord
def enable_sharing!(expiration: '1h') def enable_sharing!(expiration: '1h')
expires_at = case expiration expires_at = case expiration
when '1h' when '1h' then 1.hour.from_now
1.hour.from_now when '12h' then 12.hours.from_now
when '12h' when '24h' then 24.hours.from_now
12.hours.from_now
when '24h'
24.hours.from_now
end end
update!( update!(
@ -84,10 +90,10 @@ class Stat < ApplicationRecord
def calculate_data_bounds def calculate_data_bounds
start_date = Date.new(year, month, 1).beginning_of_day start_date = Date.new(year, month, 1).beginning_of_day
end_date = start_date.end_of_month.end_of_day end_date = start_date.end_of_month.end_of_day
points_relation = user.points.where(timestamp: start_date.to_i..end_date.to_i) points_relation = user.points.where(timestamp: start_date.to_i..end_date.to_i)
point_count = points_relation.count point_count = points_relation.count
return nil if point_count.zero? return nil if point_count.zero?
bounds_result = ActiveRecord::Base.connection.exec_query( bounds_result = ActiveRecord::Base.connection.exec_query(

View file

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