module Admin class LogsController < BaseController def index @pagy, @messages = pagy( apply_filters(SmsMessage).order(created_at: :desc), items: 50 ) 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