Skip to content

Commit

Permalink
Maps and Question Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SidharthBansal committed Jan 31, 2018
1 parent f52a4f5 commit 5e1de29
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 17 deletions.
37 changes: 23 additions & 14 deletions app/controllers/tag_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,10 @@ def show
end

def show_for_author
# try for a matching /wiki/_TAGNAME_ or /_TAGNAME_
@wiki = Node.where(path: "/wiki/#{params[:id]}").try(:first) || Node.where(path: "/#{params[:id]}").try(:first)
@wiki = Node.find(@wiki.power_tag('redirect')) if @wiki&.has_power_tag('redirect')
if params[:id][-1..-1] == '*' # wildcard tags
@wildcard = true
@tags = Tag.where('name LIKE (?)', params[:id][0..-2] + '%')
else
@tags = Tag.where(name: params[:id])
end
@tagname = params[:id]

default_type = if params[:id].match('question:')
'questions'
else
Expand All @@ -156,21 +151,35 @@ def show_for_author
# params[:node_type] - this is an optional param
# if params[:node_type] is nil - use @default_type
@node_type = params[:node_type] || default_type
@user = User.find_by(name: params[:author])
@title = "'" + @tagname.to_s + "' by " + params[:author]

node_type = 'note' if @node_type == 'questions' || @node_type == 'note'
node_type = 'page' if @node_type == 'wiki'
node_type = 'map' if @node_type == 'maps'
qids = Node.questions.where(status: 1).collect(&:nid)

if params[:id][-1..-1] == '*' # wildcard tags
@wildcard = true
@tags = Tag.where('name LIKE (?)', params[:id][0..-2] + '%')
else
@tags = Tag.where(name: params[:id])
end
@tagname = params[:id]
@user = User.find_by(name: params[:author])

nodes = Tag.tagged_nodes_by_author(@tagname, @user)
.paginate(page: params[:page], per_page: 24)
.where(status: 1, type: node_type)
.paginate(page: params[:page], per_page: 24)

# breaks the parameter
# sets everything to an empty array
set_sidebar :tags, [params[:id]]

@notes = nodes.where('node.nid NOT IN (?)', qids) if @node_type == 'note'
@unpaginated = true
node_type = 'note' if @node_type == 'questions' || @node_type == 'note'
node_type = 'page' if @node_type == 'wiki'
node_type = 'map' if @node_type == 'maps'
@questions = nodes.where('node.nid IN (?)', qids) if @node_type == 'questions'
@wikis = nodes if @node_type == 'wiki'
@nodes = nodes if @node_type == 'maps'
@title = "'" + @tagname.to_s + "' by " + params[:author]
@unpaginated = true
# the following could be refactored into a Tag.contributor_count method:
notes = Node.where(status: 1, type: 'note')
.select('node.nid, node.type, node.uid, node.status, term_data.*, community_tags.*')
Expand Down
3 changes: 2 additions & 1 deletion app/views/tag/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,14 @@
<% unless @wildcard %><p style="clear:both;padding-top:8px;"><a style="margin-bottom:6px;" class="btn btn-primary" href="/post?tags=question:<%= params[:id].gsub('question:', '') %>&template=question&redirect=question"><i class="fa fa-white icon-question-circle"></i> <%= raw t('tag.show.ask_question', :tag => params[:id].gsub('question:', '')) %></a> <%= raw t('tag.show.or_subscribe_to_answer', :url1 => "/subscribe/tag/question:"+params[:id].gsub('question:', '')) %></p><% end %>

<ul class="nav nav-tabs" style="margin-top:10px;">
<% unless params[:id].match("question:") %><li<% if @node_type == "note" %> class="active"<% end %>><a href="/tag/<%= params[:id] %>"><i class="fa fa-file"></i> <%= raw t('tag.show.research_notes') %></a></li><% end %>
<% if params[:action] == "show" %>
<% unless params[:id].match("question:") %><li<% if @node_type == "note" %> class="active"<% end %>><a href="/tag/<%= params[:id] %>"><i class="fa fa-file"></i> <%= raw t('tag.show.research_notes') %></a></li><% end %>
<li<% if @node_type == "questions" %> class="active"<% end %>><a href="/questions/tag/<%= params[:id] %>"><i class="fa fa-question-circle"></i> <%= t('tag.show.questions') %></a></li>
<li<% if @node_type == "wiki" %> class="active"<% end %>><a href="/wiki/tag/<%= params[:id] %>"><i class="fa fa-book"></i> <%= raw t('tag.show.wiki_pages') %></a></li>
<li<% if @node_type == "maps" %> class="active"<% end %>><a href="/maps/tag/<%= params[:id] %>"><i class="fa fa-map-marker"></i> <%= t('tag.show.maps') %></a></li>
<li<% if @node_type == "contributors" %> class="active"<% end %>><a href="/contributors/tag/<%= params[:id] %>"><i class="fa fa-user"></i> <%= raw t('tag.show.contributors') %> <span class="badge"><%=@length %></span></a></li>
<% else %>
<% unless params[:id].match("question:") %><li<% if @node_type == "note" %> class="active"<% end %>><a href="/tag/<%= params[:id] %>/author/<%= params[:author] %>"><i class="fa fa-file"></i> <%= raw t('tag.show.research_notes') %></a></li><% end %>
<li<% if @node_type == "questions" %> class="active"<% end %>><a href="/questions/tag/<%= params[:id] %>/author/<%= params[:author] %>" ><i class="fa fa-question-circle"></i> <%= t('tag.show.questions') %></a></li>
<li<% if @node_type == "wiki" %> class="active"<% end %>><a href="/wiki/tag/<%= params[:id] %>/author/<%= params[:author] %>" ><i class="fa fa-book"></i> <%= raw t('tag.show.wiki_pages') %></a></li>
<li<% if @node_type == "maps" %> class="active"<% end %>><a href="/maps/tag/<%= params[:id] %>/author/<%= params[:author] %>" ><i class="fa fa-map-marker"></i> <%= t('tag.show.maps') %></a></li>
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/node_tags.yml
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,9 @@ test_lego:
uid: 2
nid: 13
date: <%= DateTime.now.to_i %>

activity-spectrometer2:
tid: 14
uid: 2
nid: 21
date: <%= DateTime.now.to_i %>
12 changes: 12 additions & 0 deletions test/fixtures/nodes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,15 @@ method:
type: "page"
cached_likes: 0
path: "/wiki/spectrometer"

about2:
nid: 21
uid: 2
title: "about2"
path: "/about2"
created: <%= Time.now.to_i %>
changed: <%= Time.now.to_i %>
status: 1
type: "page"
cached_likes: 0
slug: about2
59 changes: 57 additions & 2 deletions test/functional/tag_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ def setup
get :show_for_author, node_type: 'wiki', id: 'activity:*', author: 'jeff'
assert :success
assert_not_nil :tags
assert :wildcard
assert @wildcard
assert :wikis
assert assigns(:wikis).length > 0
assigns['wikis'].each do |node|
Expand All @@ -190,7 +190,6 @@ def setup
get :show_for_author, node_type: 'wiki', id: 'activity:spectrometer', author: 'jeff'
assert :success
assert_not_nil :tags
assert :wildcard
assert :wikis
assert assigns(:wikis).length > 0
assigns['wikis'].each do |node|
Expand All @@ -205,6 +204,22 @@ def setup
assert_equal true, assigns(:wikis).empty?
end

test "wildcard does not show wiki for show_for_author" do
get :show_for_author, id: 'question:*', node_type: 'wiki', author: 'jeff'
assert_equal true, assigns(:wikis).empty?
end


test "wildcard does not show map for show_for_author" do
get :show_for_author, id: 'question:*', node_type: 'maps', author: 'Bob'
assert_equal true, assigns(:nodes).empty?
end

test " does not show map for show_for_author" do
get :show_for_author, id: 'question', node_type: 'maps', author: 'Bob'
assert_equal true, assigns(:nodes).empty?
end

test "should show a featured wiki page at top, if it exists" do
tag = tags(:test)

Expand Down Expand Up @@ -308,6 +323,22 @@ def setup
assert_equal 'note', assigns(:node_type)
end

test 'should take node type as question if tag is a question tag for show_for_author' do
tag = tags(:question)

get :show_for_author, id: tag.name, author: 'jeff'

assert_equal 'questions', assigns(:node_type)
end

test 'should take node type as note if tag is not a question tag for show_for_author' do
tag = tags(:awesome)

get :show_for_author, id: tag.name, author: 'jeff'

assert_equal 'note', assigns(:node_type)
end

test 'should list only question in question view' do
tag = tags(:question)

Expand All @@ -319,6 +350,17 @@ def setup
assert (questions & expected).present?
end

test 'should list only question in question view for show_for_author' do
tag = tags(:question)

get :show_for_author, id: tag.name, author: 'jeff'

questions = assigns(:questions)
expected = [nodes(:question), nodes(:question2)]
assert_not_nil assigns(:questions)
assert (questions & expected).present?
end

test 'should list only notes in notes view' do
tag = tags(:test)

Expand Down Expand Up @@ -356,6 +398,19 @@ def setup
assert_select '#questions.active', 1
end

test 'should have active question tab for question for show_for_author' do
tag = tags(:question)

get :show_for_author, id: tag.name, author: 'jeff'

assert_select 'ul.nav-tabs' do
assert_select 'li.active' do
assert_select "a[href = '/questions/tag/question:spectrometer/author/jeff']", 1
end
end
assert_select '#questions.active', 1
end

test 'can create tag instance (community_tag) using a parent tag' do
UserSession.create(users(:bob))

Expand Down

0 comments on commit 5e1de29

Please sign in to comment.