<commit_before>class TodosController < ApplicationController

  before_action :logged_in_user  #, only: [:new, :create, :edit, :update, :destroy, :index, :today, :tomorrow]
  before_action :set_todo, only: [:show, :edit, :update, :destroy]


  def today
    session[:working_date] = Date.today.strftime("%m/%d/%Y")
    @todo = Todo.new
    @todos = Todo.where('user_id=?', current_user.id)
                 .where('(is_complete is null and due_date<?) or (due_date=?)', Date.today, Date.today)
                 .order('position')
    session[:path] = 'Today'
  end


  def new
    @todo = Todo.new
    @todo.due_date = session[:working_date] #Date.today.strftime("%m/%d/%Y")
  end


  def create
    @todo = Todo.new(todo_params)
    @todo.user_id=current_user.id

    respond_to do |format|
      if @todo.save!

        session[:working_date] = @todo.due_date.strftime("%m/%d/%Y")

        format.html {
          flash[:success] = "Todo was successfully created."
          @todos = determine_todos_as_determined_by_working_date
          redirect_to get_path_in_context  #today_path
        }
        format.json { render :show, status: :created, location: @todo }
      else
        format.html { render :new }
        format.json { render json: @todo.errors, status: :unprocessable_entity }
      end
    end

  end


  def edit
    @todo.due_date = @todo.due_date.strftime("%m/%d/%Y") unless @todo.due_date.nil?
  end


  def update
    @todo = Todo.find(params[:id])
    @todo.update_attributes(todo_params)
    if params[:todo]['is_complete'].nil?
      @todo.is_complete = nil
    else
      @todo.is_complete = true
    end
    @todo.user_id=current_user.id

    respond_to do |format|
      if @todo.save!

        session[:working_date] = @todo.due_date.strftime("%m/%d/%Y")

        format.html {
          flash[:success] = "Todo was successfully updated."
          @todos = determine_todos_as_determined_by_working_date
          redirect_to get_path_in_context  #today_path
        }
        format.json { render :show, status: :created, location: @todo }
      else
        format.html { render :new }
        format.json { render json: @todo.errors, status: :unprocessable_entity }
      end
    end

  end


  def mark_complete
    @item = Todo.find(params[:id])

    if @item.recurrence > 0
      new_item = Todo.new
      new_item.subject = @item.subject
      new_item.recurrence = @item.recurrence

      if new_item.recurrence == 1
        # --- find any item with this subject tomorrow, if exists and incomplete then do NOT create again
        if Todo.where('subject = ?', @item.subject).where('due_date = ?', @item.due_date.advance(:days=>1)).count == 0
          new_item.due_date = @item.due_date.advance(:days=>1)
          new_item.user_id = current_user.id
          new_item.save!
        end
      elsif new_item.recurrence == 2
        # --- find an item with this subject next week, if exists and incomplete then do NOT create again
        match = Todo.where('subject = ?', @item.subject).where('due_date = ?', @item.due_date.advance(:weeks=>1)).first
        if match.nil?<commit_msg>Fixing bug where recurring items get created multiple times<commit_after>class TodosController < ApplicationController

  before_action :logged_in_user  #, only: [:new, :create, :edit, :update, :destroy, :index, :today, :tomorrow]
  before_action :set_todo, only: [:show, :edit, :update, :destroy]


  def today
    session[:working_date] = Date.today.strftime("%m/%d/%Y")
    @todo = Todo.new
    @todos = Todo.where('user_id=?', current_user.id)
                 .where('(is_complete is null and due_date<?) or (due_date=?)', Date.today, Date.today)
                 .order('position')
    session[:path] = 'Today'
  end


  def new
    @todo = Todo.new
    @todo.due_date = session[:working_date] #Date.today.strftime("%m/%d/%Y")
  end


  def create
    @todo = Todo.new(todo_params)
    @todo.user_id=current_user.id

    respond_to do |format|
      if @todo.save!

        session[:working_date] = @todo.due_date.strftime("%m/%d/%Y")

        format.html {
          flash[:success] = "Todo was successfully created."
          @todos = determine_todos_as_determined_by_working_date
          redirect_to get_path_in_context  #today_path
        }
        format.json { render :show, status: :created, location: @todo }
      else
        format.html { render :new }
        format.json { render json: @todo.errors, status: :unprocessable_entity }
      end
    end

  end


  def edit
    @todo.due_date = @todo.due_date.strftime("%m/%d/%Y") unless @todo.due_date.nil?
  end


  def update
    @todo = Todo.find(params[:id])
    @todo.update_attributes(todo_params)
    if params[:todo]['is_complete'].nil?
      @todo.is_complete = nil
    else
      @todo.is_complete = true
    end
    @todo.user_id=current_user.id

    respond_to do |format|
      if @todo.save!

        session[:working_date] = @todo.due_date.strftime("%m/%d/%Y")

        format.html {
          flash[:success] = "Todo was successfully updated."
          @todos = determine_todos_as_determined_by_working_date
          redirect_to get_path_in_context  #today_path
        }
        format.json { render :show, status: :created, location: @todo }
      else
        format.html { render :new }
        format.json { render json: @todo.errors, status: :unprocessable_entity }
      end
    end

  end


  def mark_complete
    @item = Todo.find(params[:id])

    if @item.recurrence > 0
      new_item = Todo.new
      new_item.subject = @item.subject
      new_item.recurrence = @item.recurrence

      if new_item.recurrence == 1
        # --- find any item with this subject tomorrow, if exists and incomplete then do NOT create again
        if Todo.where('subject = ?', @item.subject).where('due_date = ?', @item.due_date.advance(:days=>1)).count == 0
          new_item.subject += ' a'
          new_item.due_date = @item.due_date.advance(:days=>1)
          new_item.user_id = current_user.id
          new_item.save!
        end
      elsif new_item.recurrence == 2
        # --- find an item with this subject next week, if exists and incomplete then do NOT create again
        match = Todo.where('subject = ?', @item.subject).where('due_date = ?', @item.due_date.advance(:weeks=>1)).first
        if match.nil?
{'fraction_non_alphanumeric': 0.12687628377310792, 'fraction_numerical': 0.002212039816716701, 'mean_word_length': 2.667439165701043, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, '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': '6326150', 'n_tokens_mistral': 2192, 'n_tokens_neox': 2094, 'n_words': 453}