Clean up some code

This commit is contained in:
Eugene Burmakin 2025-10-04 23:19:00 +02:00
parent e7df54d738
commit cd303bce01
13 changed files with 16 additions and 126 deletions

View file

@ -16,12 +16,6 @@ class Api::V1::FamiliesController < ApiController
private
def ensure_family_feature_enabled!
return if DawarichSettings.family_feature_enabled?
render json: { error: 'Family feature is not enabled' }, status: :forbidden
end
def ensure_user_in_family!
return if current_api_user.in_family?

View file

@ -56,6 +56,12 @@ class ApplicationController < ActionController::Base
end
end
def ensure_family_feature_enabled!
return if DawarichSettings.family_feature_enabled?
render json: { error: 'Family feature is not enabled' }, status: :forbidden
end
private
def set_self_hosted_status

View file

@ -36,14 +36,12 @@ class FamiliesController < ApplicationController
else
@family = Family.new(family_params)
# Handle validation errors
if service.errors.any?
service.errors.each do |error|
@family.errors.add(error.attribute, error.message)
end
end
# Handle service-level errors
if service.error_message.present?
@family.errors.add(:base, service.error_message)
end
@ -130,12 +128,6 @@ class FamiliesController < ApplicationController
end
end
def ensure_family_feature_enabled!
unless DawarichSettings.family_feature_enabled?
redirect_to root_path, alert: 'Family feature is not available'
end
end
def set_family
@family = current_user.family
redirect_to new_family_path, alert: 'You are not in a family' unless @family

View file

@ -92,12 +92,6 @@ class Family::InvitationsController < ApplicationController
private
def ensure_family_feature_enabled!
unless DawarichSettings.family_feature_enabled?
redirect_to root_path, alert: 'Family feature is not available'
end
end
def set_family
@family = current_user.family

View file

@ -14,10 +14,8 @@ class Family::MembershipsController < ApplicationController
if service.call
if member_user == current_user
# User removed themselves
redirect_to new_family_path, notice: 'You have left the family'
else
# Owner removed another member
redirect_to family_path, notice: "#{member_user.email} has been removed from the family"
end
else
@ -27,12 +25,6 @@ class Family::MembershipsController < ApplicationController
private
def ensure_family_feature_enabled!
unless DawarichSettings.family_feature_enabled?
redirect_to root_path, alert: 'Family feature is not available'
end
end
def set_family
@family = current_user.family

View file

@ -1,84 +0,0 @@
# frozen_string_literal: true
module FamiliesHelper
def family_member_role_badge(membership)
case membership.role
when 'owner'
content_tag :span, 'Owner', class: 'badge badge-primary badge-sm'
when 'member'
content_tag :span, 'Member', class: 'badge badge-secondary badge-sm'
else
content_tag :span, membership.role.humanize, class: 'badge badge-ghost badge-sm'
end
end
def family_invitation_status_badge(invitation)
case invitation.status
when 'pending'
content_tag :span, 'Pending', class: 'badge badge-warning badge-sm'
when 'accepted'
content_tag :span, 'Accepted', class: 'badge badge-success badge-sm'
when 'expired'
content_tag :span, 'Expired', class: 'badge badge-error badge-sm'
when 'cancelled'
content_tag :span, 'Cancelled', class: 'badge badge-ghost badge-sm'
else
content_tag :span, invitation.status.humanize, class: 'badge badge-ghost badge-sm'
end
end
def family_capacity_warning(family)
return unless family.members.count >= Family::MAX_MEMBERS - 1
content_tag :div, class: 'alert alert-warning mt-2' do
content_tag :div do
if family.members.count >= Family::MAX_MEMBERS
'This family has reached the maximum number of members.'
else
"This family is almost full (#{family.members.count}/#{Family::MAX_MEMBERS} members)."
end
end
end
end
def invitation_expiry_warning(invitation)
return unless invitation.pending?
time_left = invitation.expires_at - Time.current
return unless time_left < 24.hours
warning_class = time_left < 1.hour ? 'alert-error' : 'alert-warning'
content_tag :div, class: "alert #{warning_class} mt-2" do
content_tag :div do
if time_left < 1.hour
'This invitation expires in less than 1 hour!'
else
"This invitation expires in #{time_ago_in_words(invitation.expires_at)}."
end
end
end
end
def family_member_location_status(member)
# This would integrate with location sharing when implemented
content_tag :span, class: 'text-sm text-gray-500' do
'Location sharing not implemented yet'
end
end
def family_creation_benefits
content_tag :div, class: 'bg-base-200 p-4 rounded-lg' do
content_tag :h3, 'Family Features:', class: 'font-semibold mb-2' do
concat content_tag(:h3, 'Family Features:', class: 'font-semibold mb-2')
concat content_tag(:ul, class: 'list-disc list-inside space-y-1 text-sm') do
concat content_tag(:li, "Share your current location with up to #{Family::MAX_MEMBERS - 1} family members")
concat content_tag(:li, 'See where your family members are right now')
concat content_tag(:li, 'Control your privacy with sharing toggles')
concat content_tag(:li, 'Invite members by email')
concat content_tag(:li, 'Secure and private - only family members can see your location')
end
end
end
end
end

View file

@ -6,14 +6,12 @@ class FamilyInvitationsCleanupJob < ApplicationJob
def perform
Rails.logger.info 'Starting family invitations cleanup'
# Update expired invitations
expired_count = FamilyInvitation.where(status: :pending)
.where('expires_at < ?', Time.current)
.update_all(status: :expired)
Rails.logger.info "Updated #{expired_count} expired family invitations"
# Delete old expired/cancelled invitations (older than 30 days)
cleanup_threshold = 30.days.ago
deleted_count = FamilyInvitation.where(status: [:expired, :cancelled])
.where('updated_at < ?', cleanup_threshold)
@ -23,4 +21,4 @@ class FamilyInvitationsCleanupJob < ApplicationJob
Rails.logger.info 'Family invitations cleanup completed'
end
end
end

View file

@ -10,12 +10,10 @@ class Family < ApplicationRecord
MAX_MEMBERS = 5
# Optimized scopes for better performance
scope :with_members, -> { includes(:members, :family_memberships) }
scope :with_pending_invitations, -> { includes(family_invitations: :invited_by) }
def can_add_members?
# Check if family can accept more members (including pending invitations)
(member_count + pending_invitations_count) < MAX_MEMBERS
end
@ -28,13 +26,11 @@ class Family < ApplicationRecord
end
def owners
# Get family owners efficiently
members.joins(:family_membership)
.where(family_memberships: { role: :owner })
end
def owner
# Get the primary owner (creator) efficiently
@owner ||= creator
end
@ -46,7 +42,6 @@ class Family < ApplicationRecord
family_invitations.active.includes(:invited_by)
end
# Clear cached counters when members change
def clear_member_cache!
@member_count = nil
@pending_invitations_count = nil

View file

@ -8,8 +8,7 @@ class FamilyInvitation < ApplicationRecord
validates :email, presence: true, format: { with: URI::MailTo::EMAIL_REGEXP }
validates :token, presence: true, uniqueness: true
validates :expires_at, presence: true
validates :status, presence: true
validates :expires_at, :status, presence: true
enum :status, { pending: 0, accepted: 1, expired: 2, cancelled: 3 }
@ -17,7 +16,6 @@ class FamilyInvitation < ApplicationRecord
before_validation :generate_token, :set_expiry, on: :create
# Clear family cache when invitation status changes or is created/destroyed
after_create :clear_family_cache
after_update :clear_family_cache, if: :saved_change_to_status?
after_destroy :clear_family_cache

View file

@ -4,12 +4,11 @@ class FamilyMembership < ApplicationRecord
belongs_to :family
belongs_to :user
validates :user_id, presence: true, uniqueness: true # One family per user
validates :user_id, presence: true, uniqueness: true
validates :role, presence: true
enum :role, { owner: 0, member: 1 }
# Clear family cache when membership changes
after_create :clear_family_cache
after_update :clear_family_cache
after_destroy :clear_family_cache

View file

@ -4,7 +4,7 @@ class User < ApplicationRecord # rubocop:disable Metrics/ClassLength
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable, :trackable
has_many :points, dependent: :destroy
has_many :points, dependent: :destroy, counter_cache: true
has_many :imports, dependent: :destroy
has_many :stats, dependent: :destroy
has_many :exports, dependent: :destroy

View file

@ -44,3 +44,8 @@ nightly_reverse_geocoding_job:
cron: "15 1 * * *" # every day at 01:15
class: "Points::NightlyReverseGeocodingJob"
queue: reverse_geocoding
nightly_family_invitations_cleanup_job:
cron: "30 2 * * *" # every day at 02:30
class: "Family::Invitations::CleanupJob"
queue: family

View file

@ -5,6 +5,7 @@
- points
- default
- mailers
- family
- imports
- exports
- stats