# coding: utf-8
class FactorsController < ApplicationController
  before_filter :isAuthorization
  around_filter :isAccessSuper

  layout "constr_works"

  def new
    @factor = Factor.new(:active => true)    
  end

  def edit
    @factor = Factor.find params[:id]
  end

  def create
    @factor = Factor.new(params[:factor])
    respond_to do |format|
      if @factor.save
        flash[:notice] = 'Коэффициент создан.'
        format.html { redirect_to factors_path }
      else
        format.html { render :action => "new" }
      end
    end

  end

  def update
     @factor = Factor.find(params[:id])
    respond_to do |format|
      if @factor.update_attributes(params[:factor])
        flash[:notice] = 'Данные обновлены'
        format.html { redirect_to factors_path }
      else
        format.html { render :action => "edit" }
      end
    end
  end

  def index
    @factors = Factor.all
  end
  
  def destroy
        @factor = Factor.find params[:id]
        if @factor.del!
          if Factor.exists? @factor.id
            flash[:notice] = 'Коэффициент скрыт.'
          else
            flash[:notice] = 'Коэффициент удалён.'
          end
    else
      flash[:error] =  (@factor.errors.each_full { |msg| puts msg }).to_s
      end
    respond_to do |format|
      format.html { redirect_to(factors_path()) }
    end

  end
  

  def show_hide
    @factor = Factor.find params[:factor_id]
    @factor.update_attributes(:active => !@factor.active)
    answer = @template.show_name(@factor.active)
    render :update do |page|
      page.call "ChangeRow", @factor.id, answer
    end

  end

  
end
