Skip to content

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
10 changes: 10 additions & 0 deletions app/controllers/export/comments_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module Export
class CommentsController < ApplicationController
def index
comments = Comment.joins(:post).where(status: "public", comments: {status: "public"})

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?

Suggested change
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.

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.

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.

Suggested change
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')

respond_to do |format|
format.csv { send_data comments.to_csv, filename: "comments-#{DateTime.now.strftime("%d%m%Y%H%M")}.csv"}
end
end
end
end
11 changes: 11 additions & 0 deletions app/models/comment.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
require 'csv'

class Comment < ApplicationRecord
include Visible
belongs_to :post

def self.to_csv

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

CSV.generate do |csv|
csv << ["Post title", "Comment author", "Comment body"]

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.

all.each do |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?

csv << [comment.post.title, comment.author, comment.body]
end
end
end
end
6 changes: 6 additions & 0 deletions app/views/posts/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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" %>

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?

</div>
</div>

<% @posts.each do |post| %>
<%unless post.archived?%>
<div class="row">
Expand Down
4 changes: 4 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@
resources :posts do
resources :comments
end

namespace :export do
resources :comments, only: :index
end
end
13 changes: 0 additions & 13 deletions test/fixtures/authors.yml

This file was deleted.

6 changes: 4 additions & 2 deletions test/fixtures/comments.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
author: MyString
body: MyText
author: Author
body: Body
post: one
status: public

two:
author: MyString
body: MyText
post: two
status: private
2 changes: 1 addition & 1 deletion test/fixtures/posts.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
title: MyString
title: Title1
body: MyText

two:
Expand Down
16 changes: 8 additions & 8 deletions test/fixtures/users.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
name: MyString
age: 1
occupation: MyString
# one:
# name: MyString
# age: 1
# occupation: MyString

two:
name: MyString
age: 1
occupation: MyString
# two:
# name: MyString
# age: 1
# occupation: MyString
9 changes: 6 additions & 3 deletions test/models/comment_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
require "test_helper"

class CommentTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
test "to csv" do
assert_equal(
"Post title,Comment author,Comment body\nTitle1,Author,Body\n",
posts(:one).comments.to_csv
)
end
end