mirror of
https://github.com/Freika/dawarich.git
synced 2026-01-10 17:21:38 -05:00
Add some refactorings
This commit is contained in:
parent
608fa41fa8
commit
dcd1c7ab2b
8 changed files with 30 additions and 23 deletions
2
Gemfile
2
Gemfile
|
|
@ -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'
|
||||
|
|
|
|||
|
|
@ -545,7 +545,7 @@ DEPENDENCIES
|
|||
groupdate
|
||||
httparty
|
||||
importmap-rails
|
||||
jwt
|
||||
jwt (~> 2.8)
|
||||
kaminari
|
||||
lograge
|
||||
oj
|
||||
|
|
|
|||
|
|
@ -46,8 +46,8 @@ class ApplicationController < ActionController::Base
|
|||
end
|
||||
|
||||
def user_not_authorized
|
||||
redirect_back_or_to root_path,
|
||||
alert: 'You are not authorized to perform this action.',
|
||||
status: :see_other
|
||||
redirect_back fallback_location: root_path,
|
||||
alert: 'You are not authorized to perform this action.',
|
||||
status: :see_other
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
|
|||
|
|
@ -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!(
|
||||
|
|
@ -84,10 +90,10 @@ class Stat < ApplicationRecord
|
|||
def calculate_data_bounds
|
||||
start_date = Date.new(year, month, 1).beginning_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)
|
||||
point_count = points_relation.count
|
||||
|
||||
|
||||
return nil if point_count.zero?
|
||||
|
||||
bounds_result = ActiveRecord::Base.connection.exec_query(
|
||||
|
|
|
|||
|
|
@ -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 (
|
||||
|
|
|
|||
Loading…
Reference in a new issue