app/controllers/camps_controller.rb
# Handle the grant updates in their own controller action
Handle the grant updates in their own controller action
Handle the grant updates in their own controller action
def update_grants
    granted = params['grants'].to_i
    if(granted <= 0)
      flash[:alert] = "#{t:cant_send_less_then_one}"
      redirect_to camp_path(@camp) and return
    end
    if @camp.maxbudget.nil?
      flash[:alert] = "#{t:dream_need_to_have_max_budget}"
      redirect_to camp_path(@camp) and return
    end
    if @camp.grants_received + granted > @camp.maxbudget
        granted = @camp.maxbudget - @camp.grants_received
    end
    if current_user.grants < granted
      flash[:alert] = "#{t:security_more_grants, granted: granted, current_user_grants: current_user.grants}"
      redirect_to camp_path(@camp) and return
    end
    ActiveRecord::Base.transaction do
      current_user.grants -= granted
      @camp.grants.new({:user_id => current_user.id, :amount => granted})      
      if @camp.grants_received + granted >= @camp.minbudget
        @camp.minfunded = true
      else
        @camp.minfunded = false
      end
      if @camp.grants_received + granted >= @camp.maxbudget
        @camp.fullyfunded = true
      else
        @camp.fullyfunded = false
      end
      unless current_user.save
        flash[:notice] = "#{t:errors_str}: #{current_user.errors.full_messages.uniq.join(', ')}"
        redirect_to camp_path(@camp) and return
      end
      unless @camp.save
        flash[:notice] = "#{t:errors_str}: #{@camp.errors.full_messages.uniq.join(', ')}"
        redirect_to camp_path(@camp) and return
      end
    end
    @add_my_name = params[:add_my_name_cb]
    if(@add_my_name == '1')
      if not @camp.users.where(id: current_user.id).exists?
        @camp.users << current_user
      end
    end
    redirect_to camp_path(@camp)
    flash[:notice] = "#{t:thanks_for_sending, grants: granted}"
  end
def update_grants granted = params [ 'grants' ] . to_i if ( granted <= 0 ) flash [ :alert ] = "#{t:cant_send_less_then_one}" redirect_to camp_path ( @camp ) and return end if @camp . maxbudget . nil? flash [ :alert ] = "#{t:dream_need_to_have_max_budget}" redirect_to camp_path ( @camp ) and return end if @camp . grants_received + granted > @camp . maxbudget granted = @camp . maxbudget - @camp . grants_received end if current_user . grants < granted flash [ :alert ] = "#{t:security_more_grants, granted: granted, current_user_grants: current_user.grants}" redirect_to camp_path ( @camp ) and return end ActiveRecord :: Base . transaction do current_user . grants -= granted @camp . grants . new ( { :user_id => current_user . id , :amount => granted } ) if @camp . grants_received + granted >= @camp . minbudget @camp . minfunded = true else @camp . minfunded = false end if @camp . grants_received + granted >= @camp . maxbudget @camp . fullyfunded = true else @camp . fullyfunded = false end unless current_user . save flash [ :notice ] = "#{t:errors_str}: #{current_user.errors.full_messages.uniq.join(', ')}" redirect_to camp_path ( @camp ) and return end unless @camp . save flash [ :notice ] = "#{t:errors_str}: #{@camp.errors.full_messages.uniq.join(', ')}" redirect_to camp_path ( @camp ) and return end end @add_my_name = params [ :add_my_name_cb ] if ( @add_my_name == '1' ) if not @camp . users . where ( id : current_user . id ) . exists? @camp . users << current_user end end redirect_to camp_path ( @camp ) flash [ :notice ] = "#{t:thanks_for_sending, grants: granted}" end
Handle the grant updates in their own controller action
Handle the grant updates in their own controller action
# Reduce the number of grants assigned to the current user by the number # of grants given away. Increase the number of grants assigned to the # camp by the same number of grants. # Decrement user grants. Check first if granting more than needed. # Increase camp grants.