diff --git a/app/controllers/concerns/public_collection_scoped.rb b/app/controllers/concerns/public_collection_scoped.rb new file mode 100644 index 000000000..3f5edb35d --- /dev/null +++ b/app/controllers/concerns/public_collection_scoped.rb @@ -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 diff --git a/app/controllers/public/collections/card_previews_controller.rb b/app/controllers/public/collections/card_previews_controller.rb new file mode 100644 index 000000000..753422ab5 --- /dev/null +++ b/app/controllers/public/collections/card_previews_controller.rb @@ -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 diff --git a/app/controllers/public/collections_controller.rb b/app/controllers/public/collections_controller.rb index 73b4e20d0..2847773db 100644 --- a/app/controllers/public/collections_controller.rb +++ b/app/controllers/public/collections_controller.rb @@ -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 diff --git a/app/helpers/cards_helper.rb b/app/helpers/cards_helper.rb index 77912dea5..a3aad18a2 100644 --- a/app/helpers/cards_helper.rb +++ b/app/helpers/cards_helper.rb @@ -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), diff --git a/app/views/cards/previews/index.turbo_stream.erb b/app/views/cards/previews/index.turbo_stream.erb index a97924606..a7bc7099e 100644 --- a/app/views/cards/previews/index.turbo_stream.erb +++ b/app/views/cards/previews/index.turbo_stream.erb @@ -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? %> diff --git a/app/views/public/collections/card_previews/index.turbo_stream.erb b/app/views/public/collections/card_previews/index.turbo_stream.erb new file mode 100644 index 000000000..1877fd968 --- /dev/null +++ b/app/views/public/collections/card_previews/index.turbo_stream.erb @@ -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 %> diff --git a/app/views/public/collections/show.html.erb b/app/views/public/collections/show.html.erb index dfdf2ff4f..7b125948f 100644 --- a/app/views/public/collections/show.html.erb +++ b/app/views/public/collections/show.html.erb @@ -1,8 +1,8 @@ <% @page_title = @collection.name %>
Nothing here
diff --git a/app/views/public/collections/show/_considering.html.erb b/app/views/public/collections/show/_considering.html.erb index b4a302ede..c88e31a5f 100644 --- a/app/views/public/collections/show/_considering.html.erb +++ b/app/views/public/collections/show/_considering.html.erb @@ -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 %>Nothing here
diff --git a/app/views/public/collections/show/_doing.html.erb b/app/views/public/collections/show/_doing.html.erb index 45360bccf..7ed35376d 100644 --- a/app/views/public/collections/show/_doing.html.erb +++ b/app/views/public/collections/show/_doing.html.erb @@ -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 %>Nothing here
diff --git a/config/routes.rb b/config/routes.rb index 37682f33e..25e96b5ab 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/script/populate/cards_for_pagination.rb b/script/populate/cards_for_pagination.rb index 15cf693da..000ec7deb 100644 --- a/script/populate/cards_for_pagination.rb +++ b/script/populate/cards_for_pagination.rb @@ -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