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
The Series list is showing empty series (with 0 articles) #9658
Comments
|
Thanks for the issue! We'll take your request into consideration and follow up if we decide to tackle this issue. To our amazing contributors: issues labeled To claim an issue to work on, please leave a comment. If you've claimed the issue and need help, please ping @forem/oss and we will follow up within 3 business days. For full info on how to contribute, please check out our contributors guide. |
|
@beeman go ahead! I think we can start by only showing series that have at least one article in the public series page. You can find the code that selects series here: https://github.com/forem/forem/blob/master/app/controllers/collections_controller.rb and its tests here https://github.com/forem/forem/blob/master/spec/requests/collections_spec.rb (maybe you can add a test to make sure that series without articles don't appear in the result). I would deal with "deleting empty series" separately to keep the PR small |
|
@beeman are you still working on this? Let us know, thanks! |
|
@rhymes I started on it but got distracted. Happy for someone else to pick it up. If I decide to look at it again, I'll post a message here first. |
|
I unassigned you now @beeman, thanks for giving it a shot 😃 |
|
Hey, @citizen428 I would really like this issue if possible. Also, I would like to add edit and delete functionality to the series. Should I do this all in the same pr, or is there another issue I should tackle as well? Thanks! |
|
@cwray-tech I think they should be in different PRs are they are related to different functionalities. I'm assigning you this issue |
|
@rhymes thank you! I will tackle this first then. Also, thanks for assigning this to me. |
|
@cwray-tech for the "managing" part of a series you can refer to this issue here #9008 |
|
@rhymes why don’t the series have their own model and controller? |
|
@cwray-tech they do, unfortunately the domain name is different. They are called collections in the code: https://github.com/forem/forem/blob/master/app/models/collection.rb and https://github.com/forem/forem/blob/master/app/controllers/collections_controller.rb |
|
@rhymes ah!! This helps so much. Thank you 😊 |
|
Hi @citizen428 or @rhymes I am pretty inexperienced with Ruby on Rails syntax. What I am trying to do is add a method in the Collection model called has_articles? then use that method in the index controller method to only get collections with articles, but I just cannot figure this out! Maybe you can help? Here is my Collection model method has_articles? : def has_articles?
articles.empty ? false : true
endIn the index method of the CollectionsController, I want to filter the collection items by whether the collection has articles, and I have tried this a variety of ways. Here is one: def index
@user = User.find_by!(username: params[:username])
@collections = @user.collections.where.has_articles?.order(created_at: :desc)
endWhat am I doing wrong here? Would love your help if you have the time! |
|
Hi @cwray-tech, thanks for giving this a shot. Here's some feedback:
Now on to the You can not just chain a normal method onto a All that said, what you want to achieve can already be done with standard Rails, no need to add any new methods: @collections = @user.collections.joins(:articles).order(created_at: :desc)This will create SQL like this: SELECT "collections".* FROM "collections" INNER JOIN "articles" ON "articles"."collection_id" = "collections"."id" WHERE "collections"."user_id" = 12 ORDER BY "collections"."created_at" DESCSince [17] forem(main)> @user.collections.count
(2.2ms) SELECT COUNT(*) FROM "collections" WHERE "collections"."user_id" = $1 [["user_id", 12]]
1
[18] forem(main)> @user.collections.joins(:articles).count
(18.3ms) SELECT COUNT(*) FROM "collections" INNER JOIN "articles" ON "articles"."collection_id" = "collections"."id" WHERE "collections"."user_id" = $1 [["user_id", 12]]
0Hope this helps, please let me know if you have any more questions! |
|
@citizen428 Awesome!! That is exactly what I was looking for. I think this will help a ton! Thank you so much. I appreciate you guys letting me hack at this. And I really didn’t want to submit a pull request with crap so I appreciate your response greatly.
…________________________________
From: Michael Kohl <notifications@github.com>
Sent: Sunday, November 1, 2020 7:04:26 PM
To: forem/forem <forem@noreply.github.com>
Cc: Christopher Wray <chris@solmediaco.com>; Mention <mention@noreply.github.com>
Subject: Re: [forem/forem] The Series list is showing empty series (with 0 articles) (#9658)
Hi @cwray-tech<https://github.com/cwray-tech>, thanks for giving this a shot.
Here's some feedback:
1. In Ruby, predicate methods names generally end with ?, so the method name is empty?, not empty.
2. You don't need a ternary here, the predicate already returns true or false.
3. In Rails vs. pure Ruby it's often more common to use present? instead of empty?, but but you can also use articles.any? here, which is semantically clear and basically renders the has_articles? method a bit obsolete.
Now on to the index method:
You can now just chain a normal method onto a where, you need to use a Rails scope for this (that includes the where). You can find the relevant doucmentation here<https://guides.rubyonrails.org/active_record_querying.html#scopes>.
All that said, what you want to achieve can already be done with standard Rails, no need to add any extra methods:
@collections = @user.collections.joins(:articles).order(created_at: :desc)
This will create SQL like this:
SELECT "collections".* FROM "collections" INNER JOIN "articles" ON "articles"."collection_id" = "collections"."id" WHERE "collections"."user_id" = 12 ORDER BY "collections"."created_at" DESC
Since joins generates an inner join collections without articles won't be returned:
[17] forem(main)> @user.collections.count
(2.2ms) SELECT COUNT(*) FROM "collections" WHERE "collections"."user_id" = $1 [["user_id", 12]]
1
[18] forem(main)> @user.collections.joins(:articles).count
(18.3ms) SELECT COUNT(*) FROM "collections" INNER JOIN "articles" ON "articles"."collection_id" = "collections"."id" WHERE "collections"."user_id" = $1 [["user_id", 12]]
0
Hope this helps, please let me know if you have any more questions.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub<#9658 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AMZNQEUAZZHUUOVC33KVCSDSNYHSVANCNFSM4PXBAW6Q>.
|
Today I saw there is a page that shows the users' series. While this is awesome, I think it would be even more awesome if it would not show the empty articles.
In my case, as seen in the screenshot, I have a few series that are there as work-in-progress, I'd love those not to be visible until they have at least one post published. In another case, a series appears double. Probably because I recreated some of the posts.
To Reproduce
Expected behavior
I would love it if the empty series (0 articles) would not appear, and add an option for authors to delete series.
Screenshots
Desktop (please complete the following information):
Smartphone (please complete the following information):
Additional context
I'm happy to give a go at implementing this, it would be great if someone could give me some pointers on what changes are expected :-)
The text was updated successfully, but these errors were encountered: