Sha256: 42ceb8510c00d4b2d30925bf9a908eeb189506e50094b12c57726805936d0947

Contents?: true

Size: 1.22 KB

Versions: 1

Compression:

Stored size: 1.22 KB

Contents

require_dependency "my_forum/application_controller"

module MyForum
  class PrivateMessagesController < ApplicationController

    before_filter :only_for_registered_users

    def index
      @private_messages = PrivateMessage.inbox_for(current_user)
      render :inbox
    end

    def inbox

    end

    def outbox

    end

    def deleted

    end

    def new
      @pm = PrivateMessage.new
    end

    def create
      @pm = PrivateMessage.new
      recipient = begin
        User.find_by_login(params[:private_message].delete(:recipient))
      rescue
        #TODO send notification to admin
        nil
      end

      unless recipient
        @pm.errors.add(:base, t('.cant_find_recipient'))
        render :new
        return
      end

      @pm.recipient_id = recipient.id
      @pm.sender_id = current_user_id
      @pm.assign_attributes(pm_params)
      @pm.save

      redirect_to action: :index
    end

    def show
      @pm = PrivateMessage.inbox_for(current_user).find(params[:id])
      @pm.update!(unread: false)
    end

    private

    def pm_params
      params.require(:private_message).permit(:subject, :body)
    end

    def only_for_registered_users
      redirect_to root_path unless current_user
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
my_forum-0.0.1.beta1 app/controllers/my_forum/private_messages_controller.rb