diff --git a/app/assets/javascripts/discourse/controllers/group_controller.js b/app/assets/javascripts/discourse/controllers/group_controller.js index 3fa523e0d..ca2af991e 100644 --- a/app/assets/javascripts/discourse/controllers/group_controller.js +++ b/app/assets/javascripts/discourse/controllers/group_controller.js @@ -7,6 +7,7 @@ @module Discourse **/ Discourse.GroupController = Discourse.ObjectController.extend({ + postsCount: null, // It would be nice if bootstrap marked action lists as selected when their links // were 'active' not the `li` tags. diff --git a/app/assets/javascripts/discourse/models/group.js b/app/assets/javascripts/discourse/models/group.js index 5d307de07..7448fecd0 100644 --- a/app/assets/javascripts/discourse/models/group.js +++ b/app/assets/javascripts/discourse/models/group.js @@ -154,6 +154,12 @@ Discourse.Group.reopenClass({ }); }, + findPostsCount: function(name) { + return Discourse.ajax("/groups/" + name + "/posts_count.json").then(function(g) { + return g.posts_count; + }); + }, + find: function(name) { return Discourse.ajax("/groups/" + name + ".json").then(function(g) { return Discourse.Group.create(g.basic_group); diff --git a/app/assets/javascripts/discourse/routes/group_route.js b/app/assets/javascripts/discourse/routes/group_route.js index acbbbe823..286130858 100644 --- a/app/assets/javascripts/discourse/routes/group_route.js +++ b/app/assets/javascripts/discourse/routes/group_route.js @@ -12,4 +12,17 @@ Discourse.GroupRoute = Discourse.Route.extend({ return Discourse.Group.find(params.name); }, + afterModel: function(model) { + var self = this; + return Discourse.Group.findPostsCount(model.get('name')).then(function (c) { + self.set('postsCount', c); + }); + }, + + setupController: function(controller, model) { + controller.setProperties({ + model: model, + postsCount: this.get('postsCount') + }); + } }); diff --git a/app/assets/javascripts/discourse/templates/group.js.handlebars b/app/assets/javascripts/discourse/templates/group.js.handlebars index 97e47554f..c4cff14cc 100644 --- a/app/assets/javascripts/discourse/templates/group.js.handlebars +++ b/app/assets/javascripts/discourse/templates/group.js.handlebars @@ -1,10 +1,17 @@
-
+
  • + {{#link-to 'group.index' model}}{{i18n groups.posts}} + ({{postsCount}}) + {{/link-to}} +
  • +
  • + {{#link-to 'group.members' model}}{{i18n groups.members}} + {{/link-to}} +
  • + +
    diff --git a/app/controllers/groups_controller.rb b/app/controllers/groups_controller.rb index c9fc37c50..a57a5f235 100644 --- a/app/controllers/groups_controller.rb +++ b/app/controllers/groups_controller.rb @@ -6,10 +6,16 @@ class GroupsController < ApplicationController render_serialized(group, BasicGroupSerializer) end + def posts_count + group = Group.where(name: params.require(:group_id)).first + guardian.ensure_can_see!(group) + render json: {posts_count: group.posts_for(guardian).count} + end + def posts group = Group.where(name: params.require(:group_id)).first guardian.ensure_can_see!(group) - posts = group.posts_for(guardian, params[:before_post_id]) + posts = group.posts_for(guardian, params[:before_post_id]).limit(20) render_serialized posts.to_a, GroupPostSerializer end diff --git a/app/models/group.rb b/app/models/group.rb index d2f6cfb0d..52309ac58 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -31,7 +31,7 @@ class Group < ActiveRecord::Base validate :alias_level, inclusion: { in: ALIAS_LEVELS.values} - def posts_for(guardian, before_post_id) + def posts_for(guardian, before_post_id=nil) user_ids = group_users.map {|gu| gu.user_id} result = Post.where(user_id: user_ids).includes(:user, :topic).references(:posts, :topics) result = result.where('topics.archetype <> ?', Archetype.private_message) @@ -46,7 +46,7 @@ class Group < ActiveRecord::Base end result = result.where('posts.id < ?', before_post_id) if before_post_id - result.order('posts.created_at desc').limit(20) + result.order('posts.created_at desc') end def self.trust_group_ids diff --git a/config/routes.rb b/config/routes.rb index 4235cfbb3..5e219b023 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -167,6 +167,7 @@ Discourse::Application.routes.draw do resources :groups do get 'members' get 'posts' + get 'posts_count' end resources :posts do diff --git a/spec/controllers/groups_controller_spec.rb b/spec/controllers/groups_controller_spec.rb index ec217fb71..d6c9c3704 100644 --- a/spec/controllers/groups_controller_spec.rb +++ b/spec/controllers/groups_controller_spec.rb @@ -18,6 +18,21 @@ describe GroupsController do end end + describe "posts_count" do + it "ensures the group can be seen" do + Guardian.any_instance.expects(:can_see?).with(group).returns(false) + xhr :get, :posts_count, group_id: group.name + response.should_not be_success + end + + it "performs the query and responds with JSON" do + Guardian.any_instance.expects(:can_see?).with(group).returns(true) + Group.any_instance.expects(:posts_for).returns(Group.none) + xhr :get, :posts_count, group_id: group.name + response.should be_success + end + end + describe "posts" do it "ensures the group can be seen" do Guardian.any_instance.expects(:can_see?).with(group).returns(false) @@ -27,7 +42,7 @@ describe GroupsController do it "calls `posts_for` and responds with JSON" do Guardian.any_instance.expects(:can_see?).with(group).returns(true) - Group.any_instance.expects(:posts_for).returns([]) + Group.any_instance.expects(:posts_for).returns(Group.none) xhr :get, :posts, group_id: group.name response.should be_success end