docbrown/app/services/exporter/articles.rb
rhymes a0c9576bec Export comments (#1471)
* Use name attribute

* Export comments

* Update copy

* Fix time-dependent test

* Remove reactions_count from the export

* Only select needed attributes from the DB

* Add the path of the commentable to the export

Comments are attached to something but from the export that is not easily identifiable. We add the "commentable_path" to give some context.
2019-01-16 12:54:45 -05:00

66 lines
1.4 KiB
Ruby

module Exporter
class Articles
attr_reader :name
attr_reader :user
def initialize(user)
@name = :articles
@user = user
end
def export(slug: nil)
articles = user.articles
articles = articles.where(slug: slug) if slug.present?
{ "#{name}.json" => jsonify(articles) }
end
private
def allowed_attributes
%i[
body_markdown
cached_tag_list
cached_user_name
cached_user_username
canonical_url
comments_count
created_at
crossposted_at
description
edited_at
feed_source_url
language
last_comment_at
main_image
main_image_background_hex_color
path
positive_reactions_count
processed_html
published
published_at
published_from_feed
show_comments
slug
social_image
title
video
video_closed_caption_track_url
video_code
video_source_url
video_thumbnail_url
]
end
def jsonify(articles)
articles_to_jsonify = []
# load articles in batches and select only needed attributes
articles.select([:id] + allowed_attributes).find_each do |article|
articles_to_jsonify << article
end
articles_to_jsonify.to_json(only: allowed_attributes)
end
end
end