# -*- encoding : utf-8 -*-
class Admin::HomeBlocksController < ApplicationController
	before_filter :authenticate_admin!
	load_and_authorize_resource
	

  def show
  end

  def edit
	@home_block = HomeBlock.find(params[:id])
	params[:resource => "home_block"]
  end

  def update
	@home_block = HomeBlock.find(params[:id])
	params[:resource => "home_block"]
	
	params_content = params[:home_block].delete(:cible_attributes)
	content_type = params[:home_block][:cible_type]
	
	cible_id = params[:home_block][:cible_id].to_i if params[:home_block][:cible_id].to_i != 0
	
	puts cible_id.to_i
	
	if !cible_id
		content = content_type.constantize.new(params_content)
	else
		content = content_type.constantize.find(cible_id) 
		
		content.attributes = params_content
	end
	
	
	
	@home_block.cible = content
	
	

	if @home_block.update_attributes(params[:home_block])
		flash[:notice] = "Le bloc a bien été modifié."

		respond_to do |format|
			format.js
		end

	else
		respond_to do |format|
			format.js { render :action => :edit}
		end		
	end
  end

end
