class ReviewsController < ApplicationController
  
  before_filter :get_movie
  # GET /reviews
  # GET /reviews.xml
  def index
    @reviews = @movie.reviews
    # ENSURE NOT TOO MUCH SPACE IS TAKEN BY THE REVIEW_TEXT ON THE LIST SCREEN
    @reviews.each do |review| 
      review.review_text = short_text(review.review_text) 
    end
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @reviews }
    end
  end

  # GET /reviews/1
  # GET /reviews/1.xml
  def show
    @review = @movie.reviews.find(params[:id])
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @review }
    end
  end

  # GET /reviews/new
  # GET /reviews/new.xml
  def new
    @review = @movie.reviews.build
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @review }
    end
  end

  # GET /reviews/1/edit
  def edit
    @review = @movie.reviews.find(params[:id])
  end

  # POST /reviews
  # POST /reviews.xml
  def create
    @review = @movie.reviews.build(params[:review])
    respond_to do |format|
      if @review.save
        flash[:notice] = 'Review was successfully created.'
        # format.html { redirect_to(@review) }
        format.html { redirect_to([@movie, @review]) }
        format.xml  { render :xml => @review, :status => :created, :location => @review }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @review.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /reviews/1
  # PUT /reviews/1.xml
  def update
    @review = @movie.reviews.find(params[:id])
    respond_to do |format|
      if @review.update_attributes(params[:review])
        flash[:notice] = 'Review was successfully updated.'
        # format.html { redirect_to(@review) }
        format.html { redirect_to([@movie, @review]) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @review.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /reviews/1
  # DELETE /reviews/1.xml
  def destroy
    @review = @movie.reviews.find(params[:id])
    @review.destroy
    respond_to do |format|
      format.html { redirect_to(movie_reviews_path(@movie)) }
      format.xml  { head :ok }
    end
  end
  
  # THIS METHOD CONVERTS A MOVIE_ID SENT BY THE ROUTE, INTO @MOVIE OBJECT
  private
  def get_movie
    @movie = Movie.find(params[:movie_id])
  end  
  
  
end
