Skip to content

Commit

Permalink
contributor count on tags page fixed (#2055)
Browse files Browse the repository at this point in the history
* contributor count on tags page fixed

fixes #2042

* minor tweaks

* changes

* added unit tests for Tag.contributors

* minor tweaks
  • Loading branch information
ViditChitkara authored and jywarren committed Jan 23, 2018
1 parent 893d9b6 commit ceb7d05
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
4 changes: 2 additions & 2 deletions app/controllers/tag_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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' }
Expand Down Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion app/models/tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand All @@ -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

Expand Down
7 changes: 4 additions & 3 deletions app/views/tag/contributors.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
<% end %>
<% if @notes.nil? || @notes.length == 0 %>
<% if @users.nil? || @users.length == 0 %>
<p><%= raw t('tag.contributors.no_contributors', :tag => params[:id]) %>:</p>
<% else %>
<table class="table">
Expand All @@ -78,9 +78,10 @@
</tr>
<% if @users %>
<% @users.each do |user| %>

<tr>
<td><a href='/profile/<%= user.name %>'><i class="fa fa-user"></i> <%= user.name %></a></td>
<td><a href='/notes/author/<%= user.name %>/<%= params[:id] %>'><%= t('tag.contributors.notes') %> &raquo;</a></td>
<td><a href='/profile/<%= user.username %>'><i class="fa fa-user"></i> <%= user.username %></a></td>
<td><a href='/notes/author/<%= user.username %>/<%= params[:id] %>'><%= t('tag.contributors.notes') %> &raquo;</a></td>
</tr>
<% end %>
<% end %>
Expand Down
8 changes: 7 additions & 1 deletion test/unit/tag_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit ceb7d05

Please sign in to comment.