dawarich/app/controllers/points_controller.rb

72 lines
1.6 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2024-05-23 14:12:23 -04:00
2024-03-15 18:27:31 -04:00
class PointsController < ApplicationController
before_action :authenticate_user!
2024-03-15 18:27:31 -04:00
def index
@points = points
.without_raw_data
.where(timestamp: start_at..end_at)
.order(timestamp: order_by)
.page(params[:page])
.per(50)
2024-03-15 18:27:31 -04:00
2024-03-24 13:55:35 -04:00
@start_at = Time.zone.at(start_at)
@end_at = Time.zone.at(end_at)
@imports = current_user.imports.order(created_at: :desc)
2024-05-23 14:12:23 -04:00
end
def bulk_destroy
2025-07-26 08:46:53 -04:00
point_ids = params[:point_ids]&.compact&.reject(&:blank?)
redirect_to points_url(preserved_params),
alert: 'No points selected.',
2025-07-26 08:46:53 -04:00
status: :see_other and return if point_ids.blank?
current_user.points.where(id: point_ids).destroy_all
2024-09-28 10:50:49 -04:00
redirect_to points_url(preserved_params),
notice: 'Points were successfully destroyed.',
status: :see_other
2024-03-15 18:27:31 -04:00
end
private
2024-05-23 14:12:23 -04:00
def point_params
params.fetch(:point, {})
end
def start_at
return 1.month.ago.beginning_of_day.to_i if params[:start_at].nil?
2024-04-21 12:12:07 -04:00
Time.zone.parse(params[:start_at]).to_i
end
def end_at
2024-03-24 13:55:35 -04:00
return Time.zone.today.end_of_day.to_i if params[:end_at].nil?
2024-04-21 12:12:07 -04:00
Time.zone.parse(params[:end_at]).to_i
end
def points
params[:import_id].present? ? import_points : user_points
end
def import_points
current_user.imports.find(params[:import_id]).points
end
def user_points
current_user.points
end
def order_by
params[:order_by] || 'desc'
end
2024-09-28 10:50:49 -04:00
def preserved_params
params.to_enum.to_h.with_indifferent_access.slice(:start_at, :end_at, :order_by, :import_id)
end
2024-03-15 18:27:31 -04:00
end