Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions app/controllers/tag_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,35 @@ def related
render partial: 'tag/related', layout: false, locals: { tags: @tags }
end

def subtopics
@topic = (params[:subtopic] || params[:topic] || params[:id]).to_s.strip
if @topic.blank?
@subtopics = []
else
parent_tag = Tag.find_by(name: "parent:#{@topic}")
node_subtopics = parent_tag ? parent_tag.nodes.where(status: 1) : []
node_slugs = node_subtopics.collect(&:slug).compact.uniq

if node_slugs.present?
@subtopics = Tag.where(name: node_slugs).or(Tag.where(parent: @topic)).order(:name)
else
@subtopics = Tag.where(parent: @topic).order(:name)
end

existing_names = @subtopics.collect(&:name)
missing_slugs = node_slugs - existing_names
if missing_slugs.present?
missing_tags = missing_slugs.map { |slug| Tag.new(name: slug) }
@subtopics = @subtopics.to_a + missing_tags
@subtopics.sort_by!(&:name)
end
end

render partial: 'tag/subtopics', layout: false, locals: { subtopics: @subtopics, topic: @topic }
end

def topic_tree; end

def widget
num = params[:n] || 4
nids = Tag.find_nodes_by_type(params[:id], 'note', num).collect(&:nid)
Expand Down Expand Up @@ -566,6 +595,4 @@ def fetch_counts
@nodes.size
end
end

def topic_tree; end
end
1 change: 1 addition & 0 deletions app/views/tag/_subtopic.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= render partial: 'tag/subtopics', locals: { subtopics: local_assigns[:subtopics] || @subtopics, topic: local_assigns[:topic] || @topic } %>
19 changes: 19 additions & 0 deletions app/views/tag/_subtopics.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<% list = local_assigns[:subtopics] || @subtopics %>
<% if list.present? %>
<% list.each do |subtopic| %>
<% name = subtopic.respond_to?(:name) ? subtopic.name : (subtopic.respond_to?(:slug) ? subtopic.slug : subtopic.to_s) %>
<div class="topic-item" data-topic="<%= name %>">
<div class="list-group-item list-group-item-action d-flex align-items-center">
<span class="expand-btn" data-topic="<%= name %>" style="cursor: pointer; margin-right: 5px;" title="Expand subtopics">
&emsp;<i class="fa fa-chevron-right icon"></i>
</span>
<a href="/<%= name %>" style="color: inherit; text-decoration: none;" class="flex-grow-1">&nbsp;<%= name %></a>
</div>
<div class="subtopics-container list-group" style="display: none; margin-left: 20px;"></div>
</div>
<% end %>
<% else %>
<div class="list-group-item text-muted" style="font-style: italic;">
&emsp;No subtopics found
</div>
<% end %>
660 changes: 553 additions & 107 deletions app/views/tag/topic_tree.html.erb

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@
get 'blog2/:id' => 'tag#blog2'
get 'tags' => 'tag#index'
get 'tags/related(/:id)' => 'tag#related'
get 'tags/subtopics(/:subtopic)' => 'tag#subtopics'
get 'tags/subtopic(/:subtopic)' => 'tag#subtopics'
get 'tags/:id' => redirect('/tag/:id')
post 'tag/suggested/:id' => 'tag#suggested'
get 'tag/author/:id.json' => 'tag#author'
Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/node_tags.yml
Original file line number Diff line number Diff line change
Expand Up @@ -284,3 +284,10 @@ sub_tag:
tid: 37
nid: 1
uid: 2

subtopic_node_tag:
tid: 38
uid: 1
nid: 2
date: <%= DateTime.now.to_i %>

9 changes: 9 additions & 0 deletions test/fixtures/tags.yml
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,12 @@ hidden_response_tag:
subscribed_tag:
tid: 37
name: sub:tag

subtopic_parent_tag:
tid: 38
name: parent:water

subtopic_child_tag:
tid: 39
name: about

48 changes: 48 additions & 0 deletions test/functional/tag_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -694,4 +694,52 @@ def setup
assert_not_nil comments
assert_template 'comments/_comments'
end

test 'subtopics partial with parent column tag' do
get :subtopics, params: { subtopic: 'spectrometry' }
assert_response :success
assert_not_nil assigns(:subtopics)
assert_includes assigns(:subtopics).map(&:name), 'spectrometer'
assert_template partial: 'tag/_subtopics'
assert_select '.topic-item[data-topic="spectrometer"]'
end

test 'subtopics partial with parent:tagname on node' do
get :subtopics, params: { subtopic: 'water' }
assert_response :success
assert_not_nil assigns(:subtopics)
assert_includes assigns(:subtopics).map(&:name), 'about'
assert_template partial: 'tag/_subtopics'
assert_select '.topic-item[data-topic="about"]'
end

test 'subtopics accepts topic parameter alias' do
get :subtopics, params: { topic: 'water' }
assert_response :success
assert_not_nil assigns(:subtopics)
assert_includes assigns(:subtopics).map(&:name), 'about'
end

test 'subtopics with missing or empty parameter' do
get :subtopics, params: { subtopic: '' }
assert_response :success
assert_equal [], assigns(:subtopics)
assert_select '.text-muted'
end

test 'subtopics with nonexistent topic returns empty list message' do
get :subtopics, params: { subtopic: 'nonexistent-topic-xyz' }
assert_response :success
assert_equal [], assigns(:subtopics)
assert_select '.text-muted'
end

test 'topic_tree page renders correctly with expandable topics' do
get :topic_tree
assert_response :success
assert_select '#accordion'
assert_select '.topic-item'
assert_select '.expand-btn'
assert_select '.subtopics-container'
end
end
Loading