<commit_before>class UsersController < ApplicationController
  ROOT = "root"

  def show
    # First attempts to find articles posted by user with given screen name
    @user = User.find_by_screen(params[:screen])
    if @user.present?
      @articles = Article.find_all_by_posted_by(@user.username)
    else
      # If given screen name was not found, assume given param is username
      @articles = Article.find_all_by_posted_by(params[:screen].downcase)
    end
  end

  def new
    @user = User.new
  end

  def create
    @user = User.new
    @user.screen = params[:user][:screen]
    @user.username = params[:user][:username].downcase
    @user.hashed_password = <PASSWORD>::Password.create(params[:user][:raw_password])

    if @user.save
      redirect_to :root, notice: "Created user"
    else
      password = params[:user][:raw_password]
      password_confirmation = params[:user][:raw_password_confirmation]

      if password.empty? || password_confirmation.empty?
        flash.notice = "Enter both password and confirmation"
      elsif password != password_confirmation
        flash.notice = "Confirmation doesn't match"
      else
        flash.notice = "Try a different username"
      end

      render "new"
    end
  end

  def edit
    @user = User.find(params[:id])

    if !@logged_in_as.can_modify(@user)
      raise Forbidden
    end
  end

  def update
    # Use currently logged in user's username
    @user = User.find_by_username(@logged_in_as.username)

    # Update user information
    @user.username = params[:user][:username].downcase
    @user.screen = params[:user][:screen]

    password = params[:user][:raw_password]
    password_confirmation = params[:user][:raw_password_confirmation]
    if !password.empty?
      @user.hashed_password = <PASSWORD>::Password.create(params[:user][:raw_password])
    end

    if @user.save
      # Update currently
      @logged_in_as = @user
      redirect_to :root, notice: "Updated user information"
    else
      if password != password_confirmation
        flash.notice = "Confirmation doesn't match"
      else
        flash.notice = "Try a different username"
      end

      render "new"
    end
  end

  def destroy
    @user = find(params[:id])

    if !@logged_in_as.can_modify(@user)
      raise Forbidden
    end

    @user.destroy

    redirect_to "root", notice: "Deleted user"
  end
end<commit_msg>Update "posted_by" when changing username<commit_after>class UsersController < ApplicationController
  ROOT = "root"

  def show
    # First attempts to find articles posted by user with given screen name
    @user = User.find_by_screen(params[:screen])
    if @user.present?
      @articles = Article.find_all_by_posted_by(@user.username)
    else
      # If given screen name was not found, assume given param is username
      @articles = Article.find_all_by_posted_by(params[:screen].downcase)
    end
  end

  def new
    @user = User.new
  end

  def create
    @user = User.new
    @user.screen = params[:user][:screen]
    @user.username = params[:user][:username].downcase
    @user.hashed_password = <PASSWORD>(params[:user][:raw_password])

    if @user.save
      redirect_to :root, notice: "Created user"
    else
      password = params[:user][:raw_password]
      password_confirmation = params[:user][:raw_password_confirmation]

      if password.empty? || password_confirmation.empty?
        flash.notice = "Enter both password and confirmation"
      elsif password != password_confirmation
        flash.notice = "Confirmation doesn't match"
      else
        flash.notice = "Try a different username"
      end

      render "new"
    end
  end

  def edit
    @user = User.find(params[:id])

    if !@logged_in_as.can_modify(@user)
      raise Forbidden
    end
  end

  def update
    # Use currently logged in user's username
    @user = User.find_by_username(@logged_in_as.username)

    # Update "posted_by"s if username was changed
    old_username = @user.username
    new_username = params[:user][:username].downcase
    if new_username != old_username
      update_posted_by(old_username, new_username)
    end

    # Update user information
    @user.username = new_username
    @user.screen = params[:user][:screen]

    password = params[:user][:raw_password]
    password_confirmation = params[:user][:raw_password_confirmation]
    if !password.empty?
      @user.hashed_password = <PASSWORD>::Password.create(params[:user][:raw_password])
    end

    if @user.save
      # Update currently
      @logged_in_as = @user
      redirect_to :root, notice: "Updated user information"
    else
      if password != password_confirmation
        flash.notice = "Confirmation doesn't match"
      else
        flash.notice = "Try a different username"
      end

      render "new"
    end
  end

  def destroy
    @user = find(params[:id])

    if !@logged_in_as.can_modify(@user)
      raise Forbidden
    end

    @user.destroy

    redirect_to "root", notice: "Deleted user"
  end

  private
  # Updates the "posted_by" fields to the new username
  def update_posted_by(old_username, new_username)
    models = [Article, Product, Photo]

    models.each do |model|
      posts = model.where(posted_by: old_username)

      posts.each do |post|
        post.posted_by = new_username
        post.save
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.09746079163554892, 'fraction_numerical': 0.0, 'mean_word_length': 3.0430188679245282, 'pattern_counts': {'":': 0, '<': 9, '<?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': '346940', 'n_tokens_mistral': 1647, 'n_tokens_neox': 1590, 'n_words': 410}