class SuppliersController < ApplicationController before_filter :set_relation_options, only: [:new, :edit, :create, :update] # GET /suppliers # GET /suppliers.json def index @suppliers = Supplier.all respond_to do |format| format.html # index.html.erb format.json { render json: @suppliers } end end # GET /suppliers/1 # GET /suppliers/1.json def show @supplier = Supplier.find(params[:id]) respond_to do |format| format.html # show.html.erb format.json { render json: @supplier } end end # GET /suppliers/new # GET /suppliers/new.json def new @supplier = Supplier.new respond_to do |format| format.html # new.html.erb format.json { render json: @supplier } end end # GET /suppliers/1/edit def edit @supplier = Supplier.find(params[:id]) end # POST /suppliers # POST /suppliers.json def create @supplier = Supplier.new(params[:supplier]) respond_to do |format| if @supplier.save format.html { redirect_to @supplier, notice: t('action.create.successfull', model: Supplier.model_name.human) } format.json { render json: @supplier, status: :created, location: @supplier } else format.html { render action: "new" } format.json { render json: @supplier.errors, status: :unprocessable_entity } end end end # PUT /suppliers/1 # PUT /suppliers/1.json def update @supplier = Supplier.find(params[:id]) respond_to do |format| if @supplier.update_attributes(params[:supplier]) format.html { redirect_to @supplier, notice: t('action.update.successfull', model: Supplier.model_name.human) } format.json { head :no_content } else format.html { render action: "edit" } format.json { render json: @supplier.errors, status: :unprocessable_entity } end end end # DELETE /suppliers/1 # DELETE /suppliers/1.json def destroy @supplier = Supplier.find(params[:id]) @supplier.destroy respond_to do |format| format.html { redirect_to suppliers_url, notice: t('action.destroy.successfull', model: Supplier.model_name.human) } format.json { head :no_content } end end # GET /suppliers/1/product_list # GET /suppliers/1/product_list.json def product_list @supplier = Supplier.find(params[:id]) respond_to do |format| format.html # show.html.erb format.json do products = @supplier.products products.include_relation(:product_categories) products.sort_by!{|p| p.product_category.try(:position) || 90000} h = products.inject({}){|h, p| n = p.product_category.try(:name) || 'other'; h[n] ||= []; h[n] << p; h} render json: h end end end private def set_relation_options @suppliers = Supplier.all @lists = List.all end end