From cfa7898c2d540aa5948c3c65d43e8a02add25711 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Wed, 27 Jun 2018 12:33:57 +0800 Subject: [PATCH] Rename `TopicView#last_read_post_id` to `TopicView#filtered_post_id`. --- app/serializers/topic_view_serializer.rb | 2 +- lib/topic_view.rb | 12 ++++++------ spec/components/topic_view_spec.rb | 6 +++--- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/app/serializers/topic_view_serializer.rb b/app/serializers/topic_view_serializer.rb index a34a22fb505..a14dd9ff301 100644 --- a/app/serializers/topic_view_serializer.rb +++ b/app/serializers/topic_view_serializer.rb @@ -187,7 +187,7 @@ class TopicViewSerializer < ApplicationSerializer def last_read_post_id return nil unless last_read_post_number - object.last_read_post_id(last_read_post_number) + object.filtered_post_id(last_read_post_number) end alias_method :include_last_read_post_id?, :has_topic_user? diff --git a/lib/topic_view.rb b/lib/topic_view.rb index 8289ccbdf4a..be2819d8e1e 100644 --- a/lib/topic_view.rb +++ b/lib/topic_view.rb @@ -412,7 +412,7 @@ class TopicView end end - def last_read_post_id(post_number) + def filtered_post_id(post_number) @filtered_posts.where(post_number: post_number).pluck(:id).first end @@ -566,16 +566,16 @@ class TopicView def closest_post_to(post_number) # happy path - closest_post = @filtered_posts.where("post_number = ?", post_number).limit(1).pluck(:id) + closest_post_id = filtered_post_id(post_number) - if closest_post.empty? + if closest_post_id.blank? # less happy path, missing post - closest_post = @filtered_posts.order("@(post_number - #{post_number})").limit(1).pluck(:id) + closest_post_id = @filtered_posts.order("@(post_number - #{post_number})").limit(1).pluck(:id).first end - return nil if closest_post.empty? + return nil if closest_post_id.blank? - filtered_post_ids.index(closest_post.first) || filtered_post_ids[0] + filtered_post_ids.index(closest_post_id) || filtered_post_ids[0] end MEGA_TOPIC_POSTS_COUNT = 10000 diff --git a/spec/components/topic_view_spec.rb b/spec/components/topic_view_spec.rb index 78f7a32585e..2f54bc6cf0f 100644 --- a/spec/components/topic_view_spec.rb +++ b/spec/components/topic_view_spec.rb @@ -583,12 +583,12 @@ describe TopicView do end end - describe '#last_read_post_id' do + describe '#filtered_post_id' do it 'should return the right id' do post = Fabricate(:post, topic: topic) - expect(topic_view.last_read_post_id(nil)).to eq(nil) - expect(topic_view.last_read_post_id(post.post_number)).to eq(post.id) + expect(topic_view.filtered_post_id(nil)).to eq(nil) + expect(topic_view.filtered_post_id(post.post_number)).to eq(post.id) end end end