Sha256: 0c339ba100350b8098ac01d46555f0ffd8881ba26e3efdd2827d29688bf9873c

Contents?: true

Size: 1.64 KB

Versions: 2

Compression:

Stored size: 1.64 KB

Contents

require_dependency "mongoid_forums/application_controller"

module MongoidForums
  class PostsController < ApplicationController
    def new
      @topic = Topic.find(params[:topic_id])
      @post = Post.new
      @post.topic = @topic.id
    end

    def create

      @post = Post.new
      @post.text = params[:post][:text]
      @post.topic = params[:topic_id]
      @post.user = mongoid_forums_user.id
      @post.reply_to_id = params[:post][:reply_to_id]

      if @post.reply_to_id && @post.reply_to_id == @post.topic.posts.first.id
        flash[:alert] = "You may not quote the original post"
        redirect_to @post.topic
        return
      end


      if @post.topic.locked
        flash[:alert] = "You may not post on a locked topic"
        redirect_to @post.topic
        return
      end

      if @post.save
        @post.topic.alert_subscribers(mongoid_forums_user.id)
        flash[:notice] = "Reply created successfully"
        redirect_to @post.topic
      else
        flash.now.alert = "Reply could not be created"
        render :action => "new"
      end
    end

    def show
    end

    def edit
    end

    def update
      @post = current_resource

      if @post.update_attributes(post_params)
        flash[:notice] = "Reply updated successfully"
        redirect_to current_resource.topic
      else
        flash[:notice] = "Reply could not be updated"
        render :action => "edit"
      end
    end

    def destroy
    end

    private

    def current_resource
      @current_resource ||= Post.find(params[:id]) if params[:id]
    end

    def post_params
      params.require(:post).permit(:text, :reply_to_id)
    end


  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mongoid-forums-0.0.2 app/controllers/mongoid_forums/posts_controller.rb
mongoid-forums-0.0.1 app/controllers/mongoid_forums/posts_controller.rb