From 6e61e0cabeba293a46a8c4df3bd417d959be8225 Mon Sep 17 00:00:00 2001 From: Karina Kwiatek Date: Tue, 19 Jul 2022 17:28:38 +0200 Subject: [PATCH] Deduplicate shared logic in `TimelineController` --- app/controllers/timeline_controller.rb | 34 ++++++++----------- app/views/timeline/list.haml | 13 ------- app/views/timeline/list.js.erb | 8 ----- app/views/timeline/public.haml | 13 ------- app/views/timeline/public.js.erb | 8 ----- .../timeline/{index.haml => timeline.haml} | 6 ++-- .../{index.js.erb => timeline.js.erb} | 0 7 files changed, 17 insertions(+), 65 deletions(-) delete mode 100644 app/views/timeline/list.haml delete mode 100644 app/views/timeline/list.js.erb delete mode 100644 app/views/timeline/public.haml delete mode 100644 app/views/timeline/public.js.erb rename app/views/timeline/{index.haml => timeline.haml} (68%) rename app/views/timeline/{index.js.erb => timeline.js.erb} (100%) diff --git a/app/controllers/timeline_controller.rb b/app/controllers/timeline_controller.rb index a7339120..a53c514e 100644 --- a/app/controllers/timeline_controller.rb +++ b/app/controllers/timeline_controller.rb @@ -4,36 +4,30 @@ class TimelineController < ApplicationController before_action :authenticate_user! def index - @timeline = current_user.cursored_timeline(last_id: params[:last_id]) - @timeline_last_id = @timeline.map(&:id).min - @more_data_available = !current_user.cursored_timeline(last_id: @timeline_last_id, size: 1).count.zero? - - respond_to do |format| - format.html - format.js { render layout: false } - end + paginate_timeline { |args| current_user.cursored_timeline(**args) } end def list @list = current_user.lists.find_by!(name: params[:list_name]) - @timeline = @list.cursored_timeline(last_id: params[:last_id]) - @timeline_last_id = @timeline.map(&:id).min - @more_data_available = !@list.cursored_timeline(last_id: @timeline_last_id, size: 1).count.zero? - - respond_to do |format| - format.html - format.js { render layout: false } - end + @title = list_title(current_user.lists.find_by!(name: params[:list_name])) + paginate_timeline { |args| @list.cursored_timeline(**args) } end def public - @timeline = Answer.cursored_public_timeline(last_id: params[:last_id]) + @title = generate_title("Public Timeline") + paginate_timeline { |args| Answer.cursored_public_timeline(**args) } + end + + private + + def paginate_timeline + @timeline = yield(last_id: params[:last_id]) @timeline_last_id = @timeline.map(&:id).min - @more_data_available = !Answer.cursored_public_timeline(last_id: @timeline_last_id, size: 1).count.zero? + @more_data_available = !yield(last_id: @timeline_last_id, size: 1).count.zero? respond_to do |format| - format.html - format.js { render layout: false } + format.html { render "timeline/timeline" } + format.js { render "timeline/timeline", layout: false } end end end diff --git a/app/views/timeline/list.haml b/app/views/timeline/list.haml deleted file mode 100644 index f7c97c3a..00000000 --- a/app/views/timeline/list.haml +++ /dev/null @@ -1,13 +0,0 @@ -#timeline - - @timeline.each do |answer| - = render 'answerbox', a: answer - -= render 'shared/cursored_pagination_dummy', more_data_available: @more_data_available, last_id: @timeline_last_id - -- if @more_data_available - .d-flex.justify-content-center.justify-content-sm-start - %button.btn.btn-light#load-more-btn{ type: :button, data: { last_id: @timeline_last_id } } - = t 'views.actions.load' - -- provide(:title, list_title(@list)) -- parent_layout 'feed' diff --git a/app/views/timeline/list.js.erb b/app/views/timeline/list.js.erb deleted file mode 100644 index 59591f8d..00000000 --- a/app/views/timeline/list.js.erb +++ /dev/null @@ -1,8 +0,0 @@ -$('#timeline').append('<% @timeline.each do |answer| - %><%= j render 'answerbox', a: answer -%><% end %>'); -<% if @more_data_available %> - $('#pagination').html('<%= j render 'shared/cursored_pagination_dummy', more_data_available: @more_data_available, last_id: @timeline_last_id %>'); -<% else %> - $('#pagination, #load-more-btn').remove(); -<% end %> diff --git a/app/views/timeline/public.haml b/app/views/timeline/public.haml deleted file mode 100644 index 3d027afe..00000000 --- a/app/views/timeline/public.haml +++ /dev/null @@ -1,13 +0,0 @@ -#timeline - - @timeline.each do |answer| - = render 'answerbox', a: answer - -= render 'shared/cursored_pagination_dummy', more_data_available: @more_data_available, last_id: @timeline_last_id - -- if @more_data_available - .d-flex.justify-content-center.justify-content-sm-start - %button.btn.btn-light#load-more-btn{ type: :button, data: { last_id: @timeline_last_id } } - = t 'views.actions.load' - -- provide(:title, generate_title('Public Timeline')) -- parent_layout 'feed' diff --git a/app/views/timeline/public.js.erb b/app/views/timeline/public.js.erb deleted file mode 100644 index 59591f8d..00000000 --- a/app/views/timeline/public.js.erb +++ /dev/null @@ -1,8 +0,0 @@ -$('#timeline').append('<% @timeline.each do |answer| - %><%= j render 'answerbox', a: answer -%><% end %>'); -<% if @more_data_available %> - $('#pagination').html('<%= j render 'shared/cursored_pagination_dummy', more_data_available: @more_data_available, last_id: @timeline_last_id %>'); -<% else %> - $('#pagination, #load-more-btn').remove(); -<% end %> diff --git a/app/views/timeline/index.haml b/app/views/timeline/timeline.haml similarity index 68% rename from app/views/timeline/index.haml rename to app/views/timeline/timeline.haml index 1e13c289..1548c944 100644 --- a/app/views/timeline/index.haml +++ b/app/views/timeline/timeline.haml @@ -1,13 +1,13 @@ #timeline - @timeline.each do |answer| - = render 'answerbox', a: answer + = render "answerbox", a: answer -= render 'shared/cursored_pagination_dummy', more_data_available: @more_data_available, last_id: @timeline_last_id += render "shared/cursored_pagination_dummy", more_data_available: @more_data_available, last_id: @timeline_last_id - if @more_data_available .d-flex.justify-content-center.justify-content-sm-start %button.btn.btn-light#load-more-btn{ type: :button, data: { last_id: @timeline_last_id } } = t 'views.actions.load' -- provide(:title, APP_CONFIG['site_name']) +- provide(:title, @title ||= APP_CONFIG["site_name"]) - parent_layout 'feed' diff --git a/app/views/timeline/index.js.erb b/app/views/timeline/timeline.js.erb similarity index 100% rename from app/views/timeline/index.js.erb rename to app/views/timeline/timeline.js.erb