app/controllers/thredded/posts_controller.rb in thredded-0.11.1 vs app/controllers/thredded/posts_controller.rb in thredded-0.12.0
- old
+ new
@@ -1,33 +1,43 @@
# frozen_string_literal: true
module Thredded
# A controller for managing {Post}s.
class PostsController < Thredded::ApplicationController
include ActionView::RecordIdentifier
+ include Thredded::NewPostParams
helper_method :topic
after_action :update_user_activity
after_action :verify_authorized
+ def new
+ @post_form = PostForm.new(user: thredded_current_user, topic: parent_topic, post_params: new_post_params)
+ authorize_creating @post_form.post
+ end
+
def create
- post = parent_topic.posts.build(post_params)
- authorize_creating post
- post.save!
+ @post_form = PostForm.new(user: thredded_current_user, topic: parent_topic, post_params: new_post_params)
+ authorize_creating @post_form.post
- redirect_to post_path(post, user: thredded_current_user)
+ if @post_form.save
+ redirect_to post_path(@post_form.post, user: thredded_current_user)
+ else
+ render :new
+ end
end
def edit
- authorize post, :update?
+ @post_form = PostForm.for_persisted(post)
+ authorize @post_form.post, :update?
return redirect_to(canonical_topic_params) unless params_match?(canonical_topic_params)
render
end
def update
authorize post, :update?
- post.update_attributes(post_params.except(:user, :ip))
+ post.update_attributes(new_post_params)
redirect_to post_path(post, user: thredded_current_user)
end
def destroy
@@ -43,10 +53,15 @@
page = post.page(user: thredded_current_user)
post.mark_as_unread(thredded_current_user, page)
after_mark_as_unread # customization hook
end
+ def quote
+ authorize_reading post
+ render plain: Thredded::ContentFormatter.quote_content(post.content)
+ end
+
private
def canonical_topic_params
{ messageboard_id: messageboard.slug, topic_id: topic.slug }
end
@@ -55,15 +70,9 @@
redirect_to messageboard_topics_path(messageboard)
end
def topic
post.postable
- end
-
- def post_params
- params.require(:post)
- .permit(:content)
- .merge(user: thredded_current_user, ip: request.remote_ip, messageboard: messageboard)
end
def parent_topic
Topic
.where(messageboard: messageboard)