<commit_before>class UserLeavesController < ApplicationController
  unloadable

  def new
    @user_leave = UserLeave.new    
  end
  
  def create  
    errors = []
    selected_users = []
    if params['create_user_leave']['selected_users']
      selected_users = params['create_user_leave']['selected_users']
    end
    selected_group_users = User.active.joins(:groups).
      where("#{User.table_name_prefix}groups_users#{User.table_name_suffix}.id" => params['create_user_leave']['selected_groups'])
    selected_date_from = params['create_user_leave']['selected_date_from'].map{|k,v| v}.join("-").to_date
    selected_date_to   = params['create_user_leave']['selected_date_to'].map{|k,v| v}.join("-").to_date    
    selected_group_users.each do |group_user|
      selected_users << User.find(group_user).id.to_s
    end
    unless selected_users.empty?
      selected_users = selected_users.uniq
      selected_users.each do |user|
        leave_date = selected_date_from        
        while leave_date <= selected_date_to
          @user_leave = UserLeave.new(user_id: user.to_i, leave_type: params['create_user_leave']['selected_leave'],
            leave_date: leave_date, comments: params['create_user_leave']['comments'], 
            fractional_leave: params['create_user_leave']['fractional_leave'])
          leave_date += 1
          unless @user_leave.save
            errors << "Leave for #{@user_leave.user.name} already exists as #{
            (@user_leave.errors[:leave_type]).first} for #{@user_leave.leave_date}"
          end 
        end       
      end
      errors=errors.flatten.uniq
      unless errors.blank?
        flash[:error]="#{errors.join('<br/>')}"
        redirect_to new_user_leafe_path
      else
        flash[:notice] = 'Leave(s) Added!'
        redirect_to user_leave_reports_path
      end
    else
      flash[:error] = "No User/Group Selected!"
      redirect_to new_user_leafe_path
    end
  end
  
  def edit
    @user_leave = UserLeave.find(params[:id])
  end
  
  def update    
    @user_leave = UserLeave.find(params[:id])
    if @user_leave.update_attributes(params[:user_leave])
      redirect_to user_leave_reports_path
    else
      render 'edit'
    end    
  end
    
  def destroy
    @user_leave = UserLeave.find(params[:id])
    @user_leave.destroy
    redirect_to user_leave_reports_path
  end
end<commit_msg>edit/update notification added and redirection modified<commit_after>class UserLeavesController < ApplicationController
  unloadable

  def new
    @user_leave = UserLeave.new    
  end
  
  def create  
    errors = []
    selected_users = []
    if params['create_user_leave']['selected_users']
      selected_users = params['create_user_leave']['selected_users']
    end
    selected_group_users = User.active.joins(:groups).
      where("#{User.table_name_prefix}groups_users#{User.table_name_suffix}.id" => params['create_user_leave']['selected_groups'])
    selected_date_from = params['create_user_leave']['selected_date_from'].map{|k,v| v}.join("-").to_date
    selected_date_to   = params['create_user_leave']['selected_date_to'].map{|k,v| v}.join("-").to_date    
    selected_group_users.each do |group_user|
      selected_users << User.find(group_user).id.to_s
    end
    unless selected_users.empty?
      selected_users = selected_users.uniq
      selected_users.each do |user|
        leave_date = selected_date_from        
        while leave_date <= selected_date_to
          @user_leave = UserLeave.new(user_id: user.to_i, leave_type: params['create_user_leave']['selected_leave'],
            leave_date: leave_date, comments: params['create_user_leave']['comments'], 
            fractional_leave: params['create_user_leave']['fractional_leave'])
          leave_date += 1
          unless @user_leave.save
            errors << "Leave for #{@user_leave.user.name} already exists as #{
            (@user_leave.errors[:leave_type]).first} for #{@user_leave.leave_date}"
          end 
        end       
      end
      errors=errors.flatten.uniq
      unless errors.blank?
        flash[:error]="#{errors.join('<br/>')}"
        redirect_to new_user_leafe_path
      else
        flash[:notice] = 'Leave(s) Added!'
        redirect_to user_leave_reports_path
      end
    else
      flash[:error] = "No User/Group Selected!"
      redirect_to new_user_leafe_path
    end
  end
  
  def edit
    @user_leave = UserLeave.find(params[:id])
  end
  
  def update    
    @user_leave = UserLeave.find(params[:id])
    if @user_leave.update_attributes(params[:user_leave])
      redirect_to edit_user_leafe_path(@user_leave), notice: 'User Leave Updated!'
    else
      redirect_to edit_user_leafe_path(@user_leave), error: 'User Leave not Updated!'
    end    
  end
    
  def destroy
    @user_leave = UserLeave.find(params[:id])
    @user_leave.destroy
    redirect_to user_leave_reports_path
  end
end
{'fraction_non_alphanumeric': 0.11192660550458716, 'fraction_numerical': 0.00040774719673802244, 'mean_word_length': 3.2220309810671255, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5513768', 'n_tokens_mistral': 1529, 'n_tokens_neox': 1505, 'n_words': 259}