-
Notifications
You must be signed in to change notification settings - Fork 0
Add comments export #2
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good all in all already! I don't think it's working quite right, yet. I'm getting (public) comments on archived posts in the export. Could you look into that again?
Also, maybe you can add an (integration) controller test as well?
module Export | ||
class CommentsController < ApplicationController | ||
def index | ||
comments = Comment.joins(:post).where(status: "public", comments: {status: "public"}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
status: "public"
also checks the comment's status:
_ = Comment.joins(:post).where(status: "public", comments: {status: "public"})
Comment Load (0.4ms) SELECT "comments".* FROM "comments" INNER JOIN "posts" ON "posts"."id" = "comments"."post_id" WHERE "comments"."status" = ? AND "comments"."status" = ? [["status", "public"], ["status", "public"]]
Perhaps you meant to write this?
comments = Comment.joins(:post).where(status: "public", comments: {status: "public"}) | |
comments = Comment.joins(:post).where(status: "public", posts: {status: "public"}) |
This should explain why I'm getting comments of archived posts in the export.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's talk about what "visible" means. Private comments on private posts are also visible on the website.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a select/pluck to make sure that we're only loading data we actually need, to reduce database-application traffic.
comments = Comment.joins(:post).where(status: "public", comments: {status: "public"}) | |
comments = Comment.joins(:post).where(status: "public", posts: {status: "public"}).pluck(:'posts.title', :'comments.author', :'comments.body') |
class Comment < ApplicationRecord | ||
include Visible | ||
belongs_to :post | ||
|
||
def self.to_csv |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't that define a class method? How does calling comments.to_csv
work then? Perhaps some Rails magic? :D
def self.to_csv | ||
CSV.generate do |csv| | ||
csv << ["Post title", "Comment author", "Comment body"] | ||
all.each do |comment| |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does all
reference what this method was called on or does it call a method getting everything? It seems to be the latter, but can you please explain to me how it works?
|
||
def self.to_csv | ||
CSV.generate do |csv| | ||
csv << ["Post title", "Comment author", "Comment body"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a nitpick, but the case gave the header in lowercase. Relevant if it's supposed to be machine-processed at some point.
@@ -5,6 +5,12 @@ | |||
|
|||
<h1 align="center">Listing all articles</h1> | |||
|
|||
<div class="row" style="margin-bottom: 12px;"> | |||
<div class="col-md-4 col-xs-8 col-xs-offset-2"> | |||
<%= link_to "Export", export_comments_path(format: 'csv'), class: "btn btn-primary" %> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
format: 'csv'
doesn't have any effect at the moment, right?
No description provided.