Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class TabsController < ApplicationController
- before_action :set_tab, only: [:show, :edit, :update, :destroy]
- before_filter :get_tab, :only => [:edit, :update, :destroy]
- before_filter :check_auth, :only => [:show, :edit, :update, :destroy]
- before_filter :check_admin, only: [:index]
- # GET /tabs
- # GET /tabs.json
- def index
- if params[:search]
- @tabs = Tab.search(params[:search]).order("created_at DESC").paginate(:per_page => 5, :page => params[:page])
- else
- @tabs = Tab.all.order('created_at DESC').paginate(:per_page => 5, :page => params[:page])
- end
- end
- def get_tab
- @tab = Tab.find(params[:id])
- end
- def check_auth
- if @tab.users.where(user_id: 'session[:user_id]') == nil
- flash[:notice] = "Acceso denegado"
- redirect_to user_debts_path
- end
- end
- # GET /tabs/1
- # GET /tabs/1.json
- def show
- @debts_cuenta = Debt.where(debt_type: 'A una cuenta', tab_id: @tab)
- @debts_usuarios = Debt.where(debt_type: 'A un amigo', tab_id: @tab)
- @tab_comment = TabComment.new
- end
- # GET /tabs/new
- def new
- @users = User.all
- @tab = Tab.new
- @amigos = Array.new
- current_user.friendships.each do |f|
- @amigos.push(User.find(f.friend_id))
- end
- end
- # GET /tabs/1/edit
- def edit
- @users = User.all
- end
- # POST /tabs
- # POST /tabs.json
- def create
- @tab = Tab.new(tab_params)
- @tab.completed=false
- respond_to do |format|
- if @tab.save
- format.html { redirect_to @tab, notice: 'Tab was successfully created.' }
- format.json { render :show, status: :created, location: @tab }
- # Agregamos Tap Participant
- @tab.users.each do |user|
- if(user.id!=current_user.id)
- tp = TabParticipant.new
- tp.tab_id = @tab.id
- tp.user_id = user.id
- tp.accepted = false
- tp.save
- else
- tp = TabParticipant.new
- tp.tab_id = @tab.id
- tp.user_id = user.id
- tp.accepted = true
- tp.save
- end
- end
- #Asignación de la Deuda
- #Asignación Simple
- if @tab.simple_distribution
- valor_uniforme_deuda = @tab.amount/@tab.users.count
- @tab.users.each do |user|
- if(user.id!=current_user.id)
- deuda_temp = Debt.new
- deuda_temp.tab_id = @tab.id
- deuda_temp.user1_id = user.id
- deuda_temp.user2_id = current_user.id
- deuda_temp.amount = valor_uniforme_deuda
- deuda_temp.amount_fixed = valor_uniforme_deuda
- deuda_temp.debt_type = 'A un amigo'
- deuda_temp.accepted = false
- deuda_temp.payed = false
- deuda_temp.save
- #Mandar email
- UserMailer.newtab_email(user).deliver
- #Crear Notificacion
- notification = Notification.new
- notification.checked = false
- notification.tipo_notificacion = "tab_participant"
- notification.user_que_notifica = current_user.id
- notification.user_notificado = user.id
- notification.message = "te ha incluido en la creación de una cuenta: "
- notification.id_elemento = @tab.id
- notification.save
- end
- end
- end
- else
- format.html { render :new }
- format.json { render json: @tab.errors, status: :unprocessable_entity }
- end
- end
- end
- # PATCH/PUT /tabs/1
- # PATCH/PUT /tabs/1.json
- def update
- respond_to do |format|
- if @tab.update(tab_params)
- format.html { redirect_to @tab, notice: 'Tab was successfully updated.' }
- format.json { render :show, status: :ok, location: @tab }
- else
- format.html { render :edit }
- format.json { render json: @tab.errors, status: :unprocessable_entity }
- end
- end
- end
- # DELETE /tabs/1
- # DELETE /tabs/1.json
- def destroy
- @tab.destroy
- respond_to do |format|
- format.html { redirect_to tabs_url, notice: 'Tab was successfully destroyed.' }
- format.json { head :no_content }
- end
- end
- def completar
- la_tab = Tab.find(la_tp.tab_id)
- la_tab.completar
- redirect_to root_url, notice: "Cuenta Completa"
- end
- private
- # Use callbacks to share common setup or constraints between actions.
- def set_tab
- @tab = Tab.find(params[:id])
- end
- # Never trust parameters from the scary internet, only allow the white list through.
- def tab_params
- params.require(:tab).permit(:title, :accepted, :amount, :simple_distribution, :description, :date, :confirmed, :id_creador, :user_ids => [], :tags => [])
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement