<commit_before>class TasksController < ApplicationController
  before_action :set_task, only: %i[show edit update destroy]
  before_action :set_project, except: %i[assign index unassign]
  before_action :check_if_team_member, except: %i[assign index unassign]

  def index
    @tasks = current_user.tasks
  end

  def new
    @task = Task.new
  end

  def edit() end

  def create
    return render :new unless @project.tasks.build(task_params).save
    redirect_to @project, notice: 'Task was successfully created.'
  end

  def update
    @task.assign_attributes(task_params)
    notice = 'Task was successfully updated.'
    @task.save ? (redirect_to request.env['HTTP_REFERER'], notice: notice) : (render :edit)
  end

  def destroy
    @task.destroy
    notice = 'Task was successfully destroyed.'
    redirect_to request.env['HTTP_REFERER'], notice: notice
  end

  # add task to current_user.tasks
  def assign
    task = Task.find(params[:task_id])
    current_user.tasks << task
    notice = "You have successfully assigned yourself to #{task.title}"
    redirect_to project_path(task.project), notice: notice
  end

  # remove task from current_user.tasks
  def unassign
    current_user.tasks.where('id = ?', params[:task_id]).update(user_id: nil)
  end


  private

  def set_task
    @task = Task.find(params[:id])
  end

  def set_project
    @project = Project.find(params[:project_id])
  end

  def task_params<commit_msg>set redirect for task#unassign<commit_after>class TasksController < ApplicationController
  before_action :set_task, only: %i[show edit update destroy]
  before_action :set_project, except: %i[assign index unassign]
  before_action :check_if_team_member, except: %i[assign index unassign]

  def index
    @tasks = current_user.tasks
  end

  def new
    @task = Task.new
  end

  def edit() end

  def create
    return render :new unless @project.tasks.build(task_params).save
    redirect_to @project, notice: 'Task was successfully created.'
  end

  def update
    @task.assign_attributes(task_params)
    notice = 'Task was successfully updated.'
    @task.save ? (redirect_to request.env['HTTP_REFERER'], notice: notice) : (render :edit)
  end

  def destroy
    @task.destroy
    notice = 'Task was successfully destroyed.'
    redirect_to request.env['HTTP_REFERER'], notice: notice
  end

  # add task to current_user.tasks
  def assign
    task = Task.find(params[:task_id])
    current_user.tasks << task
    notice = "You have successfully assigned yourself to #{task.title}"
    redirect_to project_path(task.project), notice: notice
  end

  # remove task from current_user.tasks
  def unassign
    current_user.tasks.where('id = ?', params[:task_id]).update(user_id: nil)
    notice = 'Task successfully unassigned'
    redirect_to request.env['HTTP_REFERER'], notice: notice
  end


  private

  def set_task
    @task = Task.find(params[:id])
  end

  def set_project
    @project = Project.find(params[:project_id])
  end

  def task_params
{'fraction_non_alphanumeric': 0.09576242909576244, 'fraction_numerical': 0.0, 'mean_word_length': 3.866883116883117, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, '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': '4720616', 'n_tokens_mistral': 934, 'n_tokens_neox': 907, 'n_words': 260}