docbrown/app/services/exporter/articles.rb
rhymes 915e2d77b6 Export articles/posts (#576)
* Add EditorConfig file for all editors

* Add article export service

* Add setting to request an export of all articles

* On the outside they are called posts

* Add articles exported email to mailer and service

* Refactor to one public method and test flags

* Trigger the export if it was requested and send the email

* Recreated migration file with generic export fields

* Rename fields and switch to a whitelist

* Refactor ArticleExportService into a more generic structure

* Rename articles_exported_email to export_email

* Fix notify mailer spec

* Rename Exporter::Exporter to Exporter::Service

* Remove commented out line

* Removed body_html, coordinates and updated_at from export

* Invert DJ config

* Update spec
2018-11-21 11:13:36 -05:00

67 lines
1.5 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?
json_articles = jsonify_articles(articles)
{ "articles.json" => json_articles }
end
private
def whitelisted_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
reactions_count
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)
articles_to_jsonify = []
# load articles in batches, we don't want to hog the DB
# if a user has lots and lots of articles
articles.find_each do |article|
articles_to_jsonify << article
end
articles_to_jsonify.to_json(only: whitelisted_attributes)
end
end
end