Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.37 KB | None | 0 0
  1. class TabsController < ApplicationController
  2. before_action :set_tab, only: [:show, :edit, :update, :destroy]
  3.  
  4. before_filter :get_tab, :only => [:edit, :update, :destroy]
  5. before_filter :check_auth, :only => [:show, :edit, :update, :destroy]
  6. before_filter :check_admin, only: [:index]
  7.  
  8. # GET /tabs
  9. # GET /tabs.json
  10. def index
  11. if params[:search]
  12. @tabs = Tab.search(params[:search]).order("created_at DESC").paginate(:per_page => 5, :page => params[:page])
  13. else
  14. @tabs = Tab.all.order('created_at DESC').paginate(:per_page => 5, :page => params[:page])
  15. end
  16. end
  17.  
  18. def get_tab
  19. @tab = Tab.find(params[:id])
  20. end
  21.  
  22. def check_auth
  23. if @tab.users.where(user_id: 'session[:user_id]') == nil
  24. flash[:notice] = "Acceso denegado"
  25. redirect_to user_debts_path
  26. end
  27.  
  28. end
  29. # GET /tabs/1
  30. # GET /tabs/1.json
  31. def show
  32. @debts_cuenta = Debt.where(debt_type: 'A una cuenta', tab_id: @tab)
  33. @debts_usuarios = Debt.where(debt_type: 'A un amigo', tab_id: @tab)
  34. @tab_comment = TabComment.new
  35. end
  36.  
  37. # GET /tabs/new
  38. def new
  39. @users = User.all
  40. @tab = Tab.new
  41.  
  42. @amigos = Array.new
  43.  
  44. current_user.friendships.each do |f|
  45. @amigos.push(User.find(f.friend_id))
  46. end
  47.  
  48. end
  49.  
  50. # GET /tabs/1/edit
  51. def edit
  52. @users = User.all
  53. end
  54.  
  55. # POST /tabs
  56. # POST /tabs.json
  57. def create
  58. @tab = Tab.new(tab_params)
  59. @tab.completed=false
  60.  
  61. respond_to do |format|
  62. if @tab.save
  63. format.html { redirect_to @tab, notice: 'Tab was successfully created.' }
  64. format.json { render :show, status: :created, location: @tab }
  65.  
  66. # Agregamos Tap Participant
  67. @tab.users.each do |user|
  68. if(user.id!=current_user.id)
  69. tp = TabParticipant.new
  70. tp.tab_id = @tab.id
  71. tp.user_id = user.id
  72. tp.accepted = false
  73. tp.save
  74. else
  75. tp = TabParticipant.new
  76. tp.tab_id = @tab.id
  77. tp.user_id = user.id
  78. tp.accepted = true
  79. tp.save
  80. end
  81. end
  82.  
  83. #Asignación de la Deuda
  84.  
  85. #Asignación Simple
  86. if @tab.simple_distribution
  87. valor_uniforme_deuda = @tab.amount/@tab.users.count
  88.  
  89. @tab.users.each do |user|
  90. if(user.id!=current_user.id)
  91. deuda_temp = Debt.new
  92. deuda_temp.tab_id = @tab.id
  93. deuda_temp.user1_id = user.id
  94. deuda_temp.user2_id = current_user.id
  95. deuda_temp.amount = valor_uniforme_deuda
  96. deuda_temp.amount_fixed = valor_uniforme_deuda
  97. deuda_temp.debt_type = 'A un amigo'
  98. deuda_temp.accepted = false
  99. deuda_temp.payed = false
  100. deuda_temp.save
  101.  
  102. #Mandar email
  103. UserMailer.newtab_email(user).deliver
  104.  
  105. #Crear Notificacion
  106. notification = Notification.new
  107. notification.checked = false
  108. notification.tipo_notificacion = "tab_participant"
  109. notification.user_que_notifica = current_user.id
  110. notification.user_notificado = user.id
  111. notification.message = "te ha incluido en la creación de una cuenta: "
  112. notification.id_elemento = @tab.id
  113. notification.save
  114. end
  115. end
  116.  
  117. end
  118. else
  119. format.html { render :new }
  120. format.json { render json: @tab.errors, status: :unprocessable_entity }
  121. end
  122. end
  123. end
  124.  
  125. # PATCH/PUT /tabs/1
  126. # PATCH/PUT /tabs/1.json
  127. def update
  128. respond_to do |format|
  129. if @tab.update(tab_params)
  130. format.html { redirect_to @tab, notice: 'Tab was successfully updated.' }
  131. format.json { render :show, status: :ok, location: @tab }
  132. else
  133. format.html { render :edit }
  134. format.json { render json: @tab.errors, status: :unprocessable_entity }
  135. end
  136. end
  137. end
  138.  
  139. # DELETE /tabs/1
  140. # DELETE /tabs/1.json
  141. def destroy
  142. @tab.destroy
  143. respond_to do |format|
  144. format.html { redirect_to tabs_url, notice: 'Tab was successfully destroyed.' }
  145. format.json { head :no_content }
  146. end
  147. end
  148.  
  149. def completar
  150. la_tab = Tab.find(la_tp.tab_id)
  151. la_tab.completar
  152. redirect_to root_url, notice: "Cuenta Completa"
  153. end
  154.  
  155. private
  156. # Use callbacks to share common setup or constraints between actions.
  157. def set_tab
  158. @tab = Tab.find(params[:id])
  159. end
  160. # Never trust parameters from the scary internet, only allow the white list through.
  161. def tab_params
  162. params.require(:tab).permit(:title, :accepted, :amount, :simple_distribution, :description, :date, :confirmed, :id_creador, :user_ids => [], :tags => [])
  163. end
  164. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement