From 0396fd66f6cef21722ba9eb849f364f4945e4034 Mon Sep 17 00:00:00 2001 From: Sam Date: Mon, 5 Jan 2015 17:39:49 +1100 Subject: [PATCH] FEATURE: sorting by op likes shows the op likes count --- .../discourse/components/topic-list.js.es6 | 4 ++++ .../templates/components/topic-list-header.raw.hbs | 3 +++ .../discourse/templates/components/topic-list.hbs | 1 + .../discourse/templates/list/topic_list_item.raw.hbs | 9 +++++++++ .../discourse/views/topic-list-item.js.es6 | 4 ++++ app/models/topic.rb | 2 ++ app/serializers/topic_list_item_serializer.rb | 11 ++++++++++- lib/topic_query.rb | 2 +- 8 files changed, 34 insertions(+), 2 deletions(-) diff --git a/app/assets/javascripts/discourse/components/topic-list.js.es6 b/app/assets/javascripts/discourse/components/topic-list.js.es6 index 2ea6befbbe7..11dd9e42246 100644 --- a/app/assets/javascripts/discourse/components/topic-list.js.es6 +++ b/app/assets/javascripts/discourse/components/topic-list.js.es6 @@ -15,6 +15,10 @@ export default Ember.Component.extend({ return this.get('order') === "likes"; }.property(), + showOpLikes: function(){ + return this.get('order') === "op_likes"; + }.property(), + click: function(e){ var self = this; var on = function(sel, callback){ diff --git a/app/assets/javascripts/discourse/templates/components/topic-list-header.raw.hbs b/app/assets/javascripts/discourse/templates/components/topic-list-header.raw.hbs index 5111bb48cc0..b26ae030557 100644 --- a/app/assets/javascripts/discourse/templates/components/topic-list-header.raw.hbs +++ b/app/assets/javascripts/discourse/templates/components/topic-list-header.raw.hbs @@ -17,5 +17,8 @@ {{#if showLikes}} {{raw "components/topic-list-header-column" sortable=sortable number='true' order='likes' name='likes'}} {{/if}} +{{#if showOpLikes}} + {{raw "components/topic-list-header-column" sortable=sortable number='true' order='op_likes' name='likes'}} +{{/if}} {{raw "components/topic-list-header-column" sortable=sortable number='true' order='views' name='views'}} {{raw "components/topic-list-header-column" sortable=sortable number='true' order='activity' name='activity'}} diff --git a/app/assets/javascripts/discourse/templates/components/topic-list.hbs b/app/assets/javascripts/discourse/templates/components/topic-list.hbs index 5232c50c589..357b14c0792 100644 --- a/app/assets/javascripts/discourse/templates/components/topic-list.hbs +++ b/app/assets/javascripts/discourse/templates/components/topic-list.hbs @@ -7,6 +7,7 @@ hideCategory=hideCategory showPosters=showPosters showLikes=showLikes + showOpLikes=showOpLikes showParticipants=showParticipants order=order ascending=ascending diff --git a/app/assets/javascripts/discourse/templates/list/topic_list_item.raw.hbs b/app/assets/javascripts/discourse/templates/list/topic_list_item.raw.hbs index 2a404e077ed..83eedde3ac2 100644 --- a/app/assets/javascripts/discourse/templates/list/topic_list_item.raw.hbs +++ b/app/assets/javascripts/discourse/templates/list/topic_list_item.raw.hbs @@ -38,6 +38,15 @@ {{/if}} {{/if}} +{{#if controller.showOpLikes}} + + {{#if hasOpLikes}} + + {{number topic.op_like_count}} + + {{/if}} +{{/if}} + {{number topic.views numberKey="views_long"}} {{raw "list/activity-column" topic=topic class="num" tagName="td"}} diff --git a/app/assets/javascripts/discourse/views/topic-list-item.js.es6 b/app/assets/javascripts/discourse/views/topic-list-item.js.es6 index d1ea9fa8007..94191326473 100644 --- a/app/assets/javascripts/discourse/views/topic-list-item.js.es6 +++ b/app/assets/javascripts/discourse/views/topic-list-item.js.es6 @@ -18,6 +18,10 @@ export default Discourse.View.extend(StringBuffer, { return this.get('topic.like_count') > 0; }, + hasOpLikes: function(){ + return this.get('topic.op_like_count') > 0; + }, + click: function(e){ var target = $(e.target); diff --git a/app/models/topic.rb b/app/models/topic.rb index b1a49b22520..2eab5fe2d0b 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -101,6 +101,8 @@ class Topic < ActiveRecord::Base has_one :warning + has_one :first_post, -> {where post_number: 1}, class_name: Post + # When we want to temporarily attach some data to a forum topic (usually before serialization) attr_accessor :user_data attr_accessor :posters # TODO: can replace with posters_summary once we remove old list code diff --git a/app/serializers/topic_list_item_serializer.rb b/app/serializers/topic_list_item_serializer.rb index 7ff2d8eadac..e7c85031773 100644 --- a/app/serializers/topic_list_item_serializer.rb +++ b/app/serializers/topic_list_item_serializer.rb @@ -6,7 +6,8 @@ class TopicListItemSerializer < ListableTopicSerializer :has_summary, :archetype, :last_poster_username, - :category_id + :category_id, + :op_like_count has_many :posters, serializer: TopicPosterSerializer, embed: :objects has_many :participants, serializer: TopicPosterSerializer, embed: :objects @@ -21,6 +22,10 @@ class TopicListItemSerializer < ListableTopicSerializer object.posters || [] end + def op_like_count + object.first_post && object.first_post.like_count + end + def last_poster_username posters.find { |poster| poster.user.id == object.last_post_user_id }.try(:user).try(:username) end @@ -33,4 +38,8 @@ class TopicListItemSerializer < ListableTopicSerializer object.private_message? end + def include_op_like_count? + object.association(:first_post).loaded? + end + end diff --git a/lib/topic_query.rb b/lib/topic_query.rb index cbf36f52b08..78175bf0c25 100644 --- a/lib/topic_query.rb +++ b/lib/topic_query.rb @@ -220,7 +220,7 @@ class TopicQuery end if sort_column == 'op_likes' - return result.order("(SELECT like_count FROM posts p3 WHERE p3.topic_id = topics.id AND p3.post_number = 1) #{sort_dir}") + return result.includes(:first_post).order("(SELECT like_count FROM posts p3 WHERE p3.topic_id = topics.id AND p3.post_number = 1) #{sort_dir}") end result.order("topics.#{sort_column} #{sort_dir}")