# -*- encoding : utf-8 -*-

class Admin::CommentsController < ApplicationController

	before_filter :auth_admin

	layout "admin"
	


	before_filter :find_comments


	def index 
    @comments = Comment.order("created_at DESC")
    if params[:moderated] and params[:moderated].to_i == 1
      @comments = @comments.where(:enabled => true)
      
    else
      @comments = @comments.where("enabled = 0 or enabled is null")
    end
    @comments = @comments.page(params[:page]).per(15)
    
	end


	def edit
	
		@comment = Comment.find(params[:id])
	end


	def update
	
		@comment = Comment.find(params[:id])
	
		if @comment.update_attributes(params.require(:comment).permit!)
      respond_to do |format|
        format.html{redirect_to admin_comments_path}
        format.js{}
      end

		else
		
			render :action => "edit" 
		
		end
	end


	def destroy
	
		@comment = Comment.find(params[:id])
		@comment.destroy

	end
	
	
	def find_comments
		@comments = Comment.order("created_at DESC").all
	end
	
end
