module Admin class LogsController < BaseController def index items_per_page = params[:items]&.to_i || 50 items_per_page = 50 unless [10, 25, 50, 100].include?(items_per_page) @pagy, @messages = pagy( apply_filters(SmsMessage).order(created_at: :desc), items: items_per_page ) respond_to do |format| format.html format.turbo_stream end end private def apply_filters(scope) scope = scope.where(direction: params[:direction]) if params[:direction].present? scope = scope.where(status: params[:status]) if params[:status].present? scope = scope.where(gateway_id: params[:gateway_id]) if params[:gateway_id].present? if params[:phone_number].present? scope = scope.where("phone_number LIKE ?", "%#{params[:phone_number]}%") end if params[:start_date].present? scope = scope.where("created_at >= ?", Time.parse(params[:start_date])) end if params[:end_date].present? scope = scope.where("created_at <= ?", Time.parse(params[:end_date]).end_of_day) end scope end end end