<commit_before>class VideosController < ApplicationController
  before_action :must_be_logged_in, only: [:create, :edit, :update, :destroy, :your_videos]

  def index
    @videos = Video.where(private: false).order('created_at DESC')
  end

  def new
    @video = Video.new
     
    if current_user
      @users_subjects = Subject.where(user_id: current_user.id).order("default_subject DESC")
      # This will pre-select subject when user is coming from subject playlist
      @video.subject = Subject.find(params[:subject_id]) if params[:subject_id]
    end
  end

  def create
     @video = Video.new(video_params)
     @video.user_id = current_user.id

    if @video.valid?
      flash[:success] = "Video Created!"

      if !params[:subject][:subject].empty?
        @subject = Subject.new(subject: params[:subject][:subject], description: "", user_id: @video.user_id)
        @subject.private = true if !params[:subject][:private].nil?
        @subject.save
        @video.subject = @subject
      end

      @video.private = true if @video.subject.private?
      @video.save
      redirect_to video_path(@video)
    else
      render :new
    end
  end

  def show
    @video = Video.find(params[:id])

    if !is_resource_owner?(@video) && @video.private?
      redirect_to videos_path
    end

    @video_user = User.find(@video.user_id) if !current_user || @video.user_id != current_user.id
    @comment = Comment.new
    @comments = Comment.where(video_id: params[:id]).order('created_at DESC')
  end

  def edit
    @video = Video.find(params[:id])

    if !is_resource_owner?(@video)
      redirect_to video_path(@video)
    end

    @users_subjects = Subject.where(user_id: current_user.id).order("default_subject DESC")
  end

   def update
    @video = Video.find(params[:id])
    @video.assign_attributes(video_params)
    redirect_to video_path(@video) if !is_resource_owner?(@video)
    if @video.valid?
      @video.private = true if @video.subject.private?
      @video.save
      redirect_to video_path(@video)
    else
      render :edit <commit_msg>Allow users to create subject on edit video page<commit_after>class VideosController < ApplicationController
  before_action :must_be_logged_in, only: [:create, :edit, :update, :destroy, :your_videos]

  def index
    @videos = Video.where(private: false).order('created_at DESC')
  end

  def new
    @video = Video.new
     
    if current_user
      @users_subjects = Subject.where(user_id: current_user.id).order("default_subject DESC")
      # This will pre-select subject when user is coming from subject playlist
      @video.subject = Subject.find(params[:subject_id]) if params[:subject_id]
    end
  end

  def create
     @video = Video.new(video_params)
     @video.user_id = current_user.id

    if @video.valid?
      flash[:success] = "Video Created!"

      if !params[:subject][:subject].empty?
        @subject = Subject.new(subject: params[:subject][:subject], description: "", user_id: @video.user_id)
        @subject.private = true if !params[:subject][:private].nil?
        @subject.save
        @video.subject = @subject
      end

      @video.private = true if @video.subject.private?
      @video.save
      redirect_to video_path(@video)
    else
      render :new
    end
  end

  def show
    @video = Video.find(params[:id])

    if !is_resource_owner?(@video) && @video.private?
      redirect_to videos_path
    end

    @video_user = User.find(@video.user_id) if !current_user || @video.user_id != current_user.id
    @comment = Comment.new
    @comments = Comment.where(video_id: params[:id]).order('created_at DESC')
  end

  def edit
    @video = Video.find(params[:id])

    if !is_resource_owner?(@video)
      redirect_to video_path(@video)
    end

    @users_subjects = Subject.where(user_id: current_user.id).order("default_subject DESC")
  end

   def update
    @video = Video.find(params[:id])
    @video.assign_attributes(video_params)
    redirect_to video_path(@video) if !is_resource_owner?(@video)
    if @video.valid?

      if !params[:subject][:subject].empty?
        @subject = Subject.new(subject: params[:subject][:subject], description: "", user_id: @video.user_id)
        @subject.private = true if !params[:subject][:private].nil?
        @subject.save
        @video.subject = @subject
      end
      
      @video.private = true if @video.subject.private?
      @video.save
      redirect_to video_path(@video)
    else
      render :edit 
{'fraction_non_alphanumeric': 0.12257200267916946, 'fraction_numerical': 0.0, 'mean_word_length': 3.2666666666666666, 'pattern_counts': {'":': 0, '<': 5, '<?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': '6184079', 'n_tokens_mistral': 1431, 'n_tokens_neox': 1395, 'n_words': 308}