Files
growstuff/app/controllers/posts_controller.rb
T
Daniel O'Connorandgoogle-labs-jules[bot] 2b366213e8 Cap RSS feeds iterate unbounded associations to 50 records (#4809)
- Cap @member.posts in app/views/members/show.rss.haml with .limit(50)
- Cap @post.comments in app/views/posts/show.rss.haml with .limit(50) and eager load authors
- Remove eager loading of all comments+authors on Post in PostsController#show
- Add view specs to verify RSS feed limits

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
2026-09-20 16:41:28 +09:30

66 lines
1.5 KiB
Ruby

# frozen_string_literal: true
class PostsController < ApplicationController
before_action :authenticate_member!, except: %i(index show)
load_and_authorize_resource
responders :flash
respond_to :html, :json
respond_to :rss, only: %i(index show)
def index
@author = Member.find_by!(slug: params[:member_slug]) if params[:member_slug].present?
@posts = posts
respond_with(@posts)
end
def show
@post = Post.includes(:author).find(params[:id])
respond_with(@post)
end
def new
@post = Post.new
@forum = Forum.find_by(id: params[:forum_id])
if params[:crop_id]
@crop = Crop.friendly.find(params[:crop_id])
@post.body = "[#{@crop.name}](crop)"
end
respond_with(@post)
end
def edit; end
def create
params[:post][:author_id] = current_member.id
@post = Post.new(post_params)
flash[:notice] = t('posts.created') if @post.save
respond_with(@post)
end
def update
flash[:notice] = t('posts.updated') if @post.update(post_params)
respond_with(@post)
end
def destroy
flash[:notice] = t('posts.deleted') if @post.destroy
respond_with(@post)
end
private
def post_params
params.require(:post).permit(:body, :subject, :author_id, :forum_id)
end
def posts
if @author
@author.posts
else
Post
end.order(created_at: :desc)
.includes(:author, :crop_posts, :crops, comments: :author)
.paginate(page: params[:page], per_page: 12)
end
end