Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #11379: Made the subtopics method #11380

Closed
wants to merge 12 commits into from
12 changes: 12 additions & 0 deletions app/controllers/tag_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,17 @@ def comments
render 'show'
end

def subtopics
# here, we look for this special tag marking a node as having a parent
# collect the "slugs" i.e. the unique part of the URL, which should correspond to a tagname
subtopic_names = Tag.find_by(name: "parent:#{params[:topic]}")
&.nodes
&.collect(&:slug)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I pushed a couple commits to fix minor issues. See how I moved this up out of the "private" section.

Also I added &. which fails gracefully if the value is nil, instead of erroring - it just doesn't continue trying.

I think it should work now, but I think it's worth writing a functional test for this. That will mean adding a tag to a wiki page with name parent:TAGNAME such that the code actually gets used.

The reason is that for most tags we might test this with, there won't be any subtopics. We need to create some test data to make that situation appear so that we can write a test around it. Make sense? I'm happy to help with the test data, let me know if you'd like some support on that!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And great work getting this far. It's complex!!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jywarren
I tried opening the page: /tags/subtopic/one, I am able to successfully render the page (blank although), and I can render topic name also on the page by extracting it from the URL. I created a wiki page with tag parent:one but I am still unable to fetch subtopics on the page. I doubt if @subtopics actually carries the subtopics in it.

Also, a mention, on line 531 here, we have

Tag.find_by(name: "parent:#{params[:topic]}")

Here, tag is compared with parent:#{params[:topic]}, right? So I also tried creating a wiki page with tag parent:#one but still can't fetch it on the /tags/subtopic/one page. Conversely, I also tried to change the line 531 to this -

Tag.find_by(name: "parent:{params[:topic]}")

and created a wiki page with tag parent:one, but still I can't obtain desired result.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, do you think I have this test data (the wiki pages I created), so as to write a test? Also, I am totally new to writing tests, could you please guide me with it? PS: Sharing resources also would do.

Thanks!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @KarishmaVanwari ok so you are doing really well then, if it rendered a blank page. We just need to debug the controller.

A controller test is known as a functional test -- there is documentation here, but it's pretty dense: https://guides.rubyonrails.org/testing.html#functional-tests-for-your-controllers

I might start by looking at other tests:

test 'tag show' do
get :show, params: { id: tags(:spectrometer).name }
assert :success
assert_not_nil :tags
# assert_equal assigns['tags'].length, 1
assert_select '#wiki-summary', 1
end

And at how we create test data -- by creating entries in https://github.com/publiclab/plots2/blob/main/test/fixtures/

For tags, we'll have to create not only a tag fixture entry in https://github.com/publiclab/plots2/blob/main/test/fixtures/tags.yml

but also a record to link that tag to a node: https://github.com/publiclab/plots2/blob/main/test/fixtures/node_tags.yml

Does that make sense? Let's start with that, can you add that to this PR? Once it's in place, I can help you write a simple test, then narrow the test assertions until we find what was wrong with our assumptions. How does that sound?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And, as far as I can tell, your parent:one tag should work! And here is hopefully a helpful tip - the "line of text with #{something} in the middle" syntax is a way to insert Ruby code into a string. It must use " double quotes, and the inserted Ruby code to be evaluated is surrounded by #{ }. It's a little weird looking but it is smoother than adding a bunch of separate strings together.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@subtopics = Tag.find_by(name: subtopic_names)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was trying this locally and this line returns nil,,, the subtopic_names variable above returns slugs of the nodes' title downcase & hyphenated -- from the comment seems we are expecting this to be tagname slugs that maybe the issue

render "tag/_subtopics", layout: false
end


Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra blank line detected.

private

def order_string
Expand Down Expand Up @@ -568,4 +579,5 @@ def fetch_counts
end

def topic_tree; end

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra empty line detected at class body end.

end
3 changes: 3 additions & 0 deletions app/views/tag/_subtopics.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<% @subtopics&.each do |subtopic| %>
<h1><%= subtopic %></h1>
<% end %>
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,7 @@
post 'comment/react/update/:id' => 'comment#react_update'

get 'topic-tree' => 'tag#topic_tree'
get 'tags/subtopic/:topic' => 'tag#subtopics'

# Sample resource route (maps HTTP verbs to controller actions automatically):
# resources :products
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 @@ -284,3 +284,9 @@ sub_tag:
tid: 37
nid: 1
uid: 2

subtopic_tag:
tid: 38
uid: 2
nid: 41
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good, but it's applied to this page, which is a note, not a wiki:

https://github.com/KarishmaVanwari/plots2/blob/subtopics/test/fixtures/nodes.yml#L497

Will that affect the logic, and possibly cause it not to return things?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, it should get the slug for this note, and try to use it as a subtopic tagname, according to this logic:

https://github.com/publiclab/plots2/pull/11380/files#diff-5d60373322d4baf080e5f3957fc30d0e8058337c202f5641e1775402776428b1R533

Aha - yes, so, then it takes the slug from that page, which is "jeff-12-20-2020-ouija-board" - and tries to find a tag with that name. It doesn't find one, so it returns null.

Let's change this to instead point at this node, which is a page, with slug about. Then let's add a new tag with the name about so that there is actually a tag that can be returned.

We could give these more realistic tag names so it makes more sense... but, maybe it's not worth it for now. Let's just get it working first.

Suggested change
nid: 41
nid: 2

So now you'll also have to add a new tags.yml entry, but it doesn't need a node_tags.yml entry, since we only look if the tag exists, not if we used it anywhere. Let's see how that works!

date: <%= DateTime.now.to_i %>
4 changes: 4 additions & 0 deletions test/fixtures/tags.yml
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,7 @@ hidden_response_tag:
subscribed_tag:
tid: 37
name: sub:tag

subtopic_tag:
tid: 38
name: parent:one
9 changes: 9 additions & 0 deletions test/functional/tag_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,15 @@ def setup
assert_not_nil assigns(:notes)
assert (notes & expected).present?
end

test 'subtopics partial' do
get :subtopics,
params: {
topic: 'test'
}
assert :success
assert_not_nil assigns(:subtopics)
end

test 'shows suggested tags' do
get :suggested, params: { id: 'spectr' }
Expand Down