Skip to content

Commit

Permalink
Initial activity pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanTG committed Jan 16, 2025
1 parent 815657b commit 19e7720
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 3 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ gem 'has_scope'
gem 'inherited_resources'
gem 'kaminari'
gem 'kt-paperclip'
gem 'pagy'
gem 'paper_trail'
gem 'paper_trail-association_tracking'
gem 'phonelib'
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ GEM
racc (~> 1.4)
orm_adapter (0.5.0)
ostruct (0.6.0)
pagy (9.3.3)
paper_trail (15.2.0)
activerecord (>= 6.1)
request_store (~> 1.4)
Expand Down Expand Up @@ -545,6 +546,7 @@ DEPENDENCIES
launchy
listen
ostruct
pagy
paper_trail
paper_trail-association_tracking
pg
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ class ApplicationController < ActionController::Base
FILTERING_REQUIRED_MSG = 'Filtering is required for this action. Please provide a filter when using this endpoint.'.freeze
AUTH_REQUIRED_MSG = 'Authentication is required for this action. If you are using the app, you may need to confirm your account (see the email from us) or log out and back in.'.freeze

include Pagy::Backend

def append_info_to_payload(payload)
super
payload[:user_id] = current_user&.id
Expand Down
9 changes: 7 additions & 2 deletions app/controllers/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,19 @@ def profile; end

def activity
if @region
@recent_activity = UserSubmission.where(submission_type: %w[new_lmx remove_machine new_condition new_msx confirm_location], region_id: @region.id).order('created_at DESC').limit(200)
@pagy, @recent_activity = pagy_countless(UserSubmission.where(submission_type: %w[new_lmx remove_machine new_condition new_msx confirm_location], region_id: @region.id).order('created_at DESC'), limit: 10)
@region_fullname = 'the ' + @region.full_name
@region_name = @region.name
else
@recent_activity = UserSubmission.where(submission_type: %w[new_lmx remove_machine new_condition new_msx confirm_location]).order('created_at DESC').limit(200)
@pagy, @recent_activity = pagy_countless(UserSubmission.where(submission_type: %w[new_lmx remove_machine new_condition new_msx confirm_location]).order('created_at DESC'), limit: 10)
@region_fullname = ''
@region_name = 'map'
end

respond_to do |format|
format.html # GET
format.turbo_stream # POST
end
end

def contact
Expand Down
1 change: 1 addition & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module ApplicationHelper
include Pagy::Frontend
def sortable(column, title = nil)
title ||= column.titleize
css_class = column == sort_column ? "current #{sort_direction}" : nil
Expand Down
5 changes: 4 additions & 1 deletion app/views/pages/activity.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
This list contains the last 200 edits to #{@region_fullname} Pinball Map.
.clear

.column.w_640
#activity.column.w_640
- @recent_activity.each do |recent_activity|
%div.recent_activity_container
%div.recent_activity_icon
Expand Down Expand Up @@ -66,3 +66,6 @@
by
%span.bold #{recent_activity.user_name}
.activity_hr

#next_link
= button_to "next", pagy_url_for(@pagy, @pagy.next)
5 changes: 5 additions & 0 deletions app/views/pages/activity.turbo_stream.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
= turbo_stream.append "activity" do
= render partial: "activity", collection: @pages
= turbo_stream.update "next_link" do
- if @pagy.next.present?
= button_to "next", pagy_url_for(@pagy, @pagy.next)
1 change: 1 addition & 0 deletions config/initializers/pagy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require 'pagy/extras/countless'
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
get '/apple-app-site-association' => 'pages#apple_app_site_association'
get '/robots.txt' => 'pages#robots'
get '/activity' => 'pages#activity'
post '/activity' => 'pages#activity'

scope ':region', constraints: lambda { |request| Region.where('lower(name) = ?', request.params[:region].downcase).any? } do
get 'app' => redirect('/app')
Expand Down Expand Up @@ -131,6 +132,7 @@
get '/suggest' => 'pages#suggest_new_location'
post '/submitted_new_location' => 'pages#submitted_new_location'
get '/activity' => 'pages#activity', as: 'region_activity'
post '/activity' => 'pages#activity', as: 'regio_activity'

get '*page', to: 'locations#unknown_route'
end
Expand Down

0 comments on commit 19e7720

Please sign in to comment.