class GekanatorPostsController < ApplicationController def index posts = Post .preload(tags: :tag_name) .with_attached_thumbnail .order(Arel.sql( 'COALESCE(posts.original_created_before - INTERVAL 1 MINUTE, ' \ 'posts.original_created_from, posts.created_at) DESC, posts.id DESC')) render json: { posts: posts.map { |post| post_json(post) } } end private def post_json post { id: post.id, url: post.url, title: post.title, thumbnail: thumbnail_url(post), thumbnail_base: post.thumbnail_base, original_created_from: post.original_created_from, original_created_before: post.original_created_before, tags: post.tags.map { |tag| tag_json(tag) } } end def tag_json tag { id: tag.id, name: tag.name, category: tag.category } end def thumbnail_url post return nil unless post.thumbnail.attached? rails_storage_proxy_url(post.thumbnail, only_path: false) rescue nil end end