class BooksController < ApplicationController
  def index
    list
    render :action => 'list'
  end

  def list
    @book_pages, @books = paginate :books, :per_page => 10
  end

  def show
    @book = Book.find(params[:id])
  end

  def new
    @book = Book.new
  end

  def create
    @book = Book.new(params[:book])
    if @book.save
      flash[:notice] = 'Książka została utworzona pomyślnie.'
      redirect_to :action => 'list'
    else
      render :action => 'new'
    end
  end

  def edit
    @book = Book.find(params[:id])
    @inserts = Insert.find(:all, :order => "name desc")
  end

  def update
    @book = Book.find(params[:id])
    insert = Insert.find(params["insert"].to_i)
    unless @book.inserts.include?(insert)
      @book.inserts << insert
    end
    if @book.update_attributes(params[:book])
      flash[:notice] = 'Książka została pomyślnie zaktualizowana.'
      redirect_to :action => 'show', :id => @book
    else
      render :action => 'edit'
    end
  end

  def destroy
    Book.find(params[:id]).destroy
    redirect_to :action => 'list'
  end
end