|
- class PostVersionsController < ApplicationController
- def index
- post_id = params[:post].presence
- tag_id = params[:tag].presence
- page = (params[:page].presence || 1).to_i
- limit = (params[:limit].presence || 20).to_i
-
- page = 1 if page < 1
- limit = 1 if limit < 1
-
- offset = (page - 1) * limit
-
- tag_name = nil
- if tag_id
- tag_name = TagName.joins(:tag).find_by(tag: { id: tag_id })
- return render json: [] unless tag_name
- end
-
- q = PostVersion
- q = q.where(post_id:) if post_id
- q = q.where("CONCAT(' ', tags, ' ') LIKE ?", "% #{ tag_name } %") if tag_name
-
- versions = q.order(created_at: :desc, id: :desc)
- .limit(limit)
- .offset(offset)
-
- render json: { versions:, count: q.count }
- end
- end
|