diff --git a/app/controllers/events_controller.rb b/app/controllers/events_controller.rb index 119e83b..4e4e094 100644 --- a/app/controllers/events_controller.rb +++ b/app/controllers/events_controller.rb @@ -1,10 +1,18 @@ class EventsController < ApplicationController include AircraftScoped + EVENTS_PER_PAGE = 50 + + # A page at a time; the rest load as you scroll (each page ends in a lazy + # frame that fetches the next). def index @category = params[:category].presence_in(AircraftEvent::CATEGORIES.keys) - scope = @aircraft.events.includes(:user) + @page = [ params[:page].to_i, 1 ].max + scope = @aircraft.events.includes(:user).reorder(created_at: :desc, id: :desc) scope = scope.where(category: @category) if @category - @events = scope.limit(200) + rows = scope.offset((@page - 1) * EVENTS_PER_PAGE).limit(EVENTS_PER_PAGE + 1).to_a + @more = rows.size > EVENTS_PER_PAGE + @events = rows.first(EVENTS_PER_PAGE) + render(partial: "events/page", locals: { events: @events, page: @page, more: @more, category: @category }) if turbo_frame_request? end end diff --git a/app/views/events/_page.html.erb b/app/views/events/_page.html.erb new file mode 100644 index 0000000..b439e41 --- /dev/null +++ b/app/views/events/_page.html.erb @@ -0,0 +1,13 @@ +<%# One page of the activity log, in its own frame; a lazy frame after it + loads the next page when it scrolls into view. Links open in the whole page. %> +<%= turbo_frame_tag "events-#{page}", target: "_top", class: ["block", ("border-t border-slate-100" if page > 1)] do %> + <%= render "events/list", events: events, full: true %> + <% if more %> + <% next_page = aircraft_events_path(@aircraft, category: category, page: page + 1) %> + <%= turbo_frame_tag "events-#{page + 1}", src: next_page, loading: :lazy, target: "_top", class: "block border-t border-slate-100" do %> +

+ <%= t(".loading_earlier_activity_html", link: (link_to t(".load_more"), next_page, class: "link", data: { turbo_frame: "events-#{page + 1}" })) %> +

+ <% end %> + <% end %> +<% end %> diff --git a/app/views/events/index.html.erb b/app/views/events/index.html.erb index 528a34e..692f4fc 100644 --- a/app/views/events/index.html.erb +++ b/app/views/events/index.html.erb @@ -10,5 +10,5 @@ <% end %> - <%= render "events/list", events: @events, full: true %> + <%= render "events/page", events: @events, page: @page, more: @more, category: @category %> diff --git a/config/locales/en.yml b/config/locales/en.yml index 2f61630..074e287 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -2271,6 +2271,9 @@ en: ago_html: "%{who} · %{created_at} ago" no_activity_yet: No activity yet. system: System + page: + load_more: Load more + loading_earlier_activity_html: Loading earlier activity… %{link} flight_imports: new: import_flights: Import Flights diff --git a/test/integration/events_paging_test.rb b/test/integration/events_paging_test.rb new file mode 100644 index 0000000..61f365c --- /dev/null +++ b/test/integration/events_paging_test.rb @@ -0,0 +1,33 @@ +require "test_helper" + +# The activity log loads a page at a time (no more hidden events past 200). +class EventsPagingTest < ActionDispatch::IntegrationTest + setup do + @owner = create_user("owner@example.com") + @a = create_aircraft(owner: @owner, tail_number: "N12345") + per = EventsController::EVENTS_PER_PAGE + (per + 7).times do |i| + @a.events.create!(category: i.even? ? "note" : "oil", event_name: "Event #{i}", created_at: Time.zone.local(2026, 1, 1) + i.hours) + end + end + + test "the first page has a page of events; the next page comes in a lazy frame" do + sign_in @owner + get aircraft_events_path(@a) + assert_select "turbo-frame#events-1[target=_top] li", EventsController::EVENTS_PER_PAGE + assert_select "turbo-frame#events-2[loading=lazy][src=?]", aircraft_events_path(@a, page: 2) + + get aircraft_events_path(@a, page: 2), headers: { "Turbo-Frame" => "events-2" } + assert_select "turbo-frame#events-2 li", 7 + assert_select "turbo-frame#events-3", 0 + assert_select "h2", 0 + assert_includes response.body, "Event 0" # the oldest event is reachable + end + + test "the next page keeps the category filter" do + sign_in @owner + get aircraft_events_path(@a, category: "note") + assert_select "turbo-frame#events-1 li", 29 + assert_select "turbo-frame#events-2", 0 + end +end