Reviewed-on: #355 Co-authored-by: miteruzo <miteruzo@naver.com> Co-committed-by: miteruzo <miteruzo@naver.com>
このコミットはPull リクエスト #355 でマージされました.
このコミットが含まれているのは:
@@ -2,20 +2,65 @@
|
||||
|
||||
|
||||
module PostRepr
|
||||
BASE = { include: { tags: TagRepr::BASE, uploaded_user: UserRepr::BASE },
|
||||
methods: [:parent_posts, :child_posts, :sibling_posts] }.freeze
|
||||
BASE_FIELDS = [
|
||||
:id,
|
||||
:version_no,
|
||||
:url,
|
||||
:title,
|
||||
:thumbnail_base,
|
||||
:original_created_from,
|
||||
:original_created_before,
|
||||
:created_at,
|
||||
:updated_at
|
||||
].freeze
|
||||
|
||||
module_function
|
||||
|
||||
def base post, current_user = nil
|
||||
json = post.as_json(BASE)
|
||||
return json.merge(viewed: false) unless current_user
|
||||
json = common(post)
|
||||
json['tags'] = tag_json(post.tags)
|
||||
json['uploaded_user'] = post.uploaded_user && UserRepr.base(post.uploaded_user)
|
||||
json['viewed'] = current_user ? current_user.viewed?(post) : false
|
||||
json
|
||||
end
|
||||
|
||||
viewed = current_user.viewed?(post)
|
||||
json.merge(viewed:)
|
||||
def detail post, current_user = nil, parent_posts: [], child_posts: [],
|
||||
sibling_posts: { }, related: []
|
||||
base(post, current_user).merge(
|
||||
'parent_posts' => cards(parent_posts),
|
||||
'child_posts' => cards(child_posts),
|
||||
'sibling_posts' => sibling_posts.transform_keys(&:to_s).transform_values { |posts|
|
||||
cards(posts)
|
||||
},
|
||||
'related' => cards(related))
|
||||
end
|
||||
|
||||
def card post
|
||||
common(post).merge('parent_posts' => [], 'child_posts' => [])
|
||||
end
|
||||
|
||||
def cards posts
|
||||
posts.map { |post| card(post) }
|
||||
end
|
||||
|
||||
def many posts, current_user = nil
|
||||
posts.map { |p| base(p, current_user) }
|
||||
end
|
||||
|
||||
def common post
|
||||
BASE_FIELDS.to_h { |field| [field.to_s, post.public_send(field)] }
|
||||
.merge('thumbnail' => thumbnail_url(post))
|
||||
end
|
||||
|
||||
def tag_json tags
|
||||
tags.map { |tag| TagRepr.inline(tag) }
|
||||
end
|
||||
|
||||
def thumbnail_url post
|
||||
return nil unless post.thumbnail.attached?
|
||||
|
||||
Rails.application.routes.url_helpers.rails_blob_url(post.thumbnail, only_path: false)
|
||||
rescue
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
新しい課題から参照
ユーザをブロックする