Pagination working for public collections

This commit is contained in:
Jorge Manrubia
2025-06-09 14:36:53 +02:00
parent 87bbb79851
commit 7013b7f378
12 changed files with 81 additions and 23 deletions
@@ -0,0 +1,12 @@
module PublicCollectionScoped
extend ActiveSupport::Concern
included do
before_action :set_collection
end
private
def set_collection
@collection = Collection.find_by_published_key(params[:id] || params[:collection_id])
end
end
@@ -0,0 +1,23 @@
class Public::Collections::CardPreviewsController < ApplicationController
include PublicCollectionScoped
allow_unauthenticated_access only: :index
def index
set_page_and_extract_portion_from find_cards, per_page: CardsController::PAGE_SIZE
end
private
def find_cards
case params[:target]
when "considering-cards"
@collection.cards.considering.latest
when "doing-cards"
@collection.cards.doing.latest
when "closed-cards"
@collection.cards.closed.recently_closed_first
else
head :bad_request
end
end
end
@@ -1,20 +1,13 @@
class Public::CollectionsController < ApplicationController
allow_unauthenticated_access only: :show
include PublicCollectionScoped
before_action :set_collection
allow_unauthenticated_access only: :show
layout "public"
PAGE_SIZE = 50
def show
@considering = set_page_and_extract_portion_from @collection.cards.considering.latest, per_page: PAGE_SIZE
@doing = set_page_and_extract_portion_from @collection.cards.doing.latest, per_page: PAGE_SIZE
@closed = set_page_and_extract_portion_from @collection.cards.closed.recently_closed_first, per_page: PAGE_SIZE
@considering = current_page_from @collection.cards.considering.latest, per_page: CardsController::PAGE_SIZE
@doing = current_page_from @collection.cards.doing.latest, per_page: CardsController::PAGE_SIZE
@closed = current_page_from @collection.cards.closed.recently_closed_first, per_page: CardsController::PAGE_SIZE
end
private
def set_collection
@collection = Collection.find_by_published_key(params[:id])
end
end
+16
View File
@@ -15,6 +15,22 @@ module CardsHelper
**options
end
def public_collection_cards_next_page_link(collection, target, fetch_on_visible: false, page:, data: {}, **options)
url = public_collection_card_previews_path(collection.publication.key, target: target, page: page.next_param)
if fetch_on_visible
data[:controller] = "#{data[:controller]} fetch-on-visible"
data[:fetch_on_visible_url_value] = url
end
link_to "Load more",
url,
id: "#{target}-load-page-#{page.next_param}",
data: { turbo_stream: true, **data },
class: "btn txt-small",
**options
end
def card_article_tag(card, id: dom_id(card, :article), **options, &block)
classes = [
options.delete(:class),
@@ -1,8 +1,7 @@
<%= turbo_stream.remove "#{params[:target]}-load-page-#{@page.number}" %>
<%= turbo_stream.append params[:target] do %>
<%= render partial: @filter.engagement_status&.considering? ? "cards/display/mini" : "cards/display/preview",
collection: @page.records, as: :card, cached: true %>
<%= render partial: "cards/display/preview", collection: @page.records, as: :card, cached: true %>
<% unless @page.last? %>
<%= cards_next_page_link params[:target], page: @page, filter: @filter, fetch_on_visible: @filter.indexed_by.closed? %>
@@ -0,0 +1,10 @@
<%= turbo_stream.remove "#{params[:target]}-load-page-#{@page.number}" %>
<%= turbo_stream.append params[:target] do %>
<%= render partial: "cards/display/preview",
collection: @page.records, as: :card, cached: true %>
<% unless @page.last? %>
<%= public_collection_cards_next_page_link @collection, params[:target], page: @page, fetch_on_visible: params[:target] == "closed-cards" %>
<% end %>
<% end %>
+3 -3
View File
@@ -1,8 +1,8 @@
<% @page_title = @collection.name %>
<div class="card-columns">
<%= render "public/collections/show/considering", page: @page %>
<%= render "public/collections/show/doing", page: @page %>
<%= render "public/collections/show/considering", collection: @collection, page: @considering %>
<%= render "public/collections/show/doing", collection: @collection, page: @doing %>
</div>
<%= render "public/collections/show/closed", page: @page %>
<%= render "public/collections/show/closed", collection: @collection, page: @closed %>
@@ -3,6 +3,7 @@
<%= render partial: "cards/display/preview", collection: page.records, as: :card, cached: true %>
<% unless page.last? %>
<%= public_collection_cards_next_page_link collection, "closed-cards", fetch_on_visible: true, page: page %>
<% end %>
<% else %>
<p class="txt-medium translucent">Nothing here</p>
@@ -7,6 +7,7 @@
<%= render partial: "cards/display/preview", collection: page.records, as: :card, cached: true %>
<% unless page.last? %>
<%= public_collection_cards_next_page_link collection, "considering-cards", page: page %>
<% end %>
<% else %>
<p class="txt-medium translucent">Nothing here</p>
@@ -7,6 +7,7 @@
<%= render partial: "cards/display/preview", collection: page.records, as: :card, cached: ->(card) { cacheable_preview_parts_for(card) } %>
<% unless page.last? %>
<%= public_collection_cards_next_page_link collection, "doing-cards", page: page %>
<% end %>
<% else %>
<p class="txt-medium translucent">Nothing here</p>
+7 -5
View File
@@ -52,14 +52,13 @@ Rails.application.routes.draw do
end
end
resources :notifications do
scope module: :notifications do
get "tray", to: "trays#show", on: :collection
get "tray", to: "trays#show", on: :collection
get "settings", to: "settings#show", on: :collection
post "readings", to: "readings#create_all", on: :collection, as: :read_all
post "reading", to: "readings#create", on: :member, as: :read
post "reading", to: "readings#create", on: :member, as: :read
end
end
@@ -104,7 +103,11 @@ Rails.application.routes.draw do
end
namespace :public do
resources :collections
resources :collections do
scope module: :collections do
resources :card_previews
end
end
end
direct :published_collection do |collection, options|
@@ -135,7 +138,6 @@ Rails.application.routes.draw do
get "up", to: "rails/health#show", as: :rails_health_check
get "manifest" => "rails/pwa#manifest", as: :pwa_manifest
root "events#index"
namespace :admin do
+1 -1
View File
@@ -2,7 +2,7 @@ require_relative "../../config/environment"
CARDS_COUNT = 200
ApplicationRecord.current_tenant = "development-tenant"
ApplicationRecord.current_tenant = "37signals"
account = Account.first
user = User.first
Current.session = user.sessions.last