Skip to content

Instantly share code, notes, and snippets.

@sealabcore
Created June 4, 2010 22:51
Show Gist options
  • Save sealabcore/426051 to your computer and use it in GitHub Desktop.
Save sealabcore/426051 to your computer and use it in GitHub Desktop.

Revisions

  1. sealabcore created this gist Jun 4, 2010.
    87 changes: 87 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,87 @@
    class CommentsController < ApplicationController

    include ApplicationHelper, StageHelper, CommentsHelper

    before_filter :login_required
    before_filter :setup_current_user

    def create
    case params[:type]
    when "Photo"
    @item = Photo.find_by_id( params[:id] )
    path = photo_path(@item)
    @user = User.find_by_id( @item.user_id, :include => :privacy )
    comment_auth = ( @current_user.id != @item.user_id ) && (@user.privacy.photo_comment == "Yes")
    when "Audio"
    @item = Audio.find_by_id( params[:id] )
    path = audio_path(@item)
    @user = User.find_by_id( @item.user_id, :include => :privacy )
    comment_auth = ( @current_user.id != @item.user_id ) && (@user.privacy.audio_comment == "Yes")
    when "User"
    @item = User.find_by_id( params[:id], :include => :privacy )
    path = user_comments_path(@item.login)
    comment_auth = (@current_user != @item) && (@item.privacy.stage_comment == "Yes")
    when "Adventure"
    @item = Adventure.find_by_id( params[:id] )
    path = adventure_path(@item)
    end
    @comment = @item.comments.create( params[:comment] )
    if @comment.save
    case params[:type]
    when "Photo", "Audio"
    if comment_auth
    Notifications.deliver_comment( @item, @comment, @user, @comment.user, path, stage_for(@comment.user) )
    end
    when "User"
    if comment_auth
    Notifications.deliver_user_comment( @item, @current_user, @comment, stage_for(@item), stage_for(@current_user))
    end
    end
    respond_to do |wants|
    flash[:success] = "Your comment was added successfully"
    wants.html { redirect_to path }
    wants.js { render }
    end
    else
    respond_to do |wants|
    if session[:comment_redirect] != true
    flash[:warning] = @comment.errors.full_messages
    end
    wants.html { redirect_to path }
    wants.js { render }
    end
    end
    end

    def destroy
    user = current_user
    comment = Comment.find( :first, :conditions => [ "id = ?", params[:c] ])
    case params[:type]
    when "Photo"
    @item = Photo.find_by_id( params[:id] )
    path = photo_path(params[:id])
    comment_auth = comment.user_id == user.id || user.id == @item.user.id || user.admin?
    when "Audio"
    @item = Audio.find_by_id( params[:id] )
    path = audio_path(params[:id])
    comment_auth = comment.user_id == user.id || user.id == @item.user.id || user.admin?
    when "User"
    @item = User.find_by_id(params[:id])
    path = user_comments_path(@item.login)
    comment_auth = (user == comment.user) || (user.admin?) || ( @item.id == user.id )
    when "Adventure"
    @item = Adventure.find_by_id(params[:id])
    path = adventure_path(@item)
    comment_auth = (user == comment.user) || (user.admin?)
    end
    if comment_auth
    comment.destroy
    end

    respond_to do |wants|
    wants.html { redirect_to path }
    wants.js { render }
    end
    end

    end