Sha256: b3f34d9e35242f118186843379b708f30573d212e3efbea366a5ba22bd55bc3d

Contents?: true

Size: 1.95 KB

Versions: 3

Compression:

Stored size: 1.95 KB

Contents

class SimpleDiscussion::ForumPostsController < SimpleDiscussion::ApplicationController
  before_action :authenticate_user!
  before_action :set_forum_thread
  before_action :set_forum_post, only: [:edit, :update]
  before_action :require_moderator_or_author!, only: [:edit, :update, :solved, :unsolved]

  def create
    @forum_post = @forum_thread.forum_posts.new(forum_post_params)
    @forum_post.user_id = current_user.id

    if @forum_post.save
      #ForumNotificationJob.perform_later(@forum_post.id)
      redirect_to simple_discussion.forum_thread_path(@forum_thread, anchor: "forum_post_#{@forum_post.id}")
    else
      render template: "simple_discussion/forum_threads/show"
    end
  end

  def edit
  end

  def update
    if @forum_post.update(forum_post_params)
      redirect_to simple_discussion.forum_thread_path(@forum_thread)
    else
      render action: :edit
    end
  end

  def solved
    @forum_post = @forum_thread.forum_posts.find(params[:id])

    @forum_thread.forum_posts.update_all(solved: false)
    @forum_post.update_column(:solved, true)
    @forum_thread.update_column(:solved, true)

    redirect_to simple_discussion.forum_thread_path(@forum_thread, anchor: ActionView::RecordIdentifier.dom_id(@forum_post))
  end

  def unsolved
    @forum_post = @forum_thread.forum_posts.find(params[:id])

    @forum_thread.forum_posts.update_all(solved: false)
    @forum_thread.update_column(:solved, false)

    redirect_to simple_discussion.forum_thread_path(@forum_thread, anchor: ActionView::RecordIdentifier.dom_id(@forum_post))
  end

  private

    def set_forum_thread
      @forum_thread = ForumThread.friendly.find(params[:forum_thread_id])
    end

    def set_forum_post
      if current_user.moderator?
        @forum_post = @forum_thread.forum_posts.find(params[:id])
      else
        @forum_post = current_user.forum_posts.find(params[:id])
      end
    end

    def forum_post_params
      params.require(:forum_post).permit(:body)
    end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
simple_discussion-0.9.2 app/controllers/simple_discussion/forum_posts_controller.rb
simple_discussion-0.9.1 app/controllers/simple_discussion/forum_posts_controller.rb
simple_discussion-0.9.0 app/controllers/simple_discussion/forum_posts_controller.rb