From ceb7d05404b4619c7fa8cd8cae5e8215798cb02c Mon Sep 17 00:00:00 2001 From: Vidit Date: Tue, 23 Jan 2018 20:34:30 +0530 Subject: [PATCH] contributor count on tags page fixed (#2055) * contributor count on tags page fixed fixes #2042 * minor tweaks * changes * added unit tests for Tag.contributors * minor tweaks --- app/controllers/tag_controller.rb | 4 ++-- app/models/tag.rb | 8 +++++++- app/views/tag/contributors.html.erb | 7 ++++--- test/unit/tag_test.rb | 8 +++++++- 4 files changed, 20 insertions(+), 7 deletions(-) diff --git a/app/controllers/tag_controller.rb b/app/controllers/tag_controller.rb index 1d1d24c3af..f97345ab46 100644 --- a/app/controllers/tag_controller.rb +++ b/app/controllers/tag_controller.rb @@ -112,7 +112,7 @@ def show .includes(:tag) .references(:term_data) .where('term_data.name = ?', params[:id]) - @length = notes.collect(&:uid).uniq.length || 0 + @length = Tag.contributor_count(params[:id]) || 0 respond_with(nodes) do |format| format.html { render 'tag/show' } @@ -322,7 +322,7 @@ def contributors .references(:term_data, :node_revisions) .where('term_data.name = ?', params[:id]) .order('node_revisions.timestamp DESC') - @users = @notes.collect(&:author).uniq + @users = Tag.contributors(@tagnames[0]) end # /contributors diff --git a/app/models/tag.rb b/app/models/tag.rb index b05433eedb..bc7c8a72dc 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -52,8 +52,9 @@ def belongs_to(current_user, nid) node_tag && node_tag.uid == current_user.uid || node_tag.node.uid == current_user.uid end - def self.contributor_count(tagname) + def self.contributors(tagname) tag = Tag.includes(:node).where(name: tagname).first + return [] if tag.nil? nodes = tag.node.includes(:revision, :comments,:answers).where(status: 1) uids = nodes.collect(&:uid) nodes.each do |n| @@ -62,6 +63,11 @@ def self.contributor_count(tagname) uids+=n.revision.collect(&:uid) end uids = uids.uniq + User.where(id: uids) + end + + def self.contributor_count(tagname) + uids = Tag.contributors(tagname) uids.length end diff --git a/app/views/tag/contributors.html.erb b/app/views/tag/contributors.html.erb index 1ac3bf83e8..cbc36dd546 100644 --- a/app/views/tag/contributors.html.erb +++ b/app/views/tag/contributors.html.erb @@ -68,7 +68,7 @@ <% end %> - <% if @notes.nil? || @notes.length == 0 %> + <% if @users.nil? || @users.length == 0 %>

<%= raw t('tag.contributors.no_contributors', :tag => params[:id]) %>:

<% else %> @@ -78,9 +78,10 @@ <% if @users %> <% @users.each do |user| %> + - - + + <% end %> <% end %> diff --git a/test/unit/tag_test.rb b/test/unit/tag_test.rb index a02d54b882..9019383382 100644 --- a/test/unit/tag_test.rb +++ b/test/unit/tag_test.rb @@ -146,10 +146,16 @@ class TagTest < ActiveSupport::TestCase assert_not_nil top_nodes end + test 'contributors with specific tag name' do + tag = tags(:test) + contributors = Tag.contributors(tag.name) + assert_equal [1,2,5,6],contributors.pluck(:id) + end + test 'contributor_count with specific tag name' do tag = tags(:test) contributor_count = Tag.contributor_count(tag.name) - assert_equal 5,contributor_count + assert_equal 4,contributor_count end end
<%= user.name %><%= t('tag.contributors.notes') %> » <%= user.username %><%= t('tag.contributors.notes') %> »