<commit_before># encoding: UTF-8

# Source image sets controller.
class SrcSetsController < ApplicationController
  def new
    @src_set = SrcSet.new
  end

  def create
    @src_set = SrcSet.new(src_set_params)
    @src_set.user = current_user

    if @src_set.save
      redirect_to(
        { action: :index },
        notice: 'Source set created.')
    else
      render :new
    end
  end

  def index
    @src_sets = SrcSet.active.not_empty.most_recent.page(params[:page])
  end

  def update
    # rubocop:disable Style/IfUnlessModifier
    unless current_user
      render status: :forbidden, text: 'Forbidden' && return
    end
    # rubocop:enable Style/IfUnlessModifier

    @src_set = SrcSet.active.where(
      name: params[:id]).first_or_create do |ss|
      ss.user = current_user
    end

    if @src_set.user == current_user
      add_src_images = params.delete(:add_src_images)
      @src_set.add_src_images(add_src_images) if add_src_images
      delete_src_images = params.delete(:delete_src_images)
      @src_set.delete_src_images(delete_src_images) if delete_src_images

      respond_to do |format|
        if @src_set.update_attributes(src_set_params)
          format.html do
            redirect_to(
              { action: :show, id: @src_set.name },
              notice: 'The set was successfully updated.')
          end
          format.json { render json: {} }
        else
          format.html { render :edit }
          format.json do
            render json: @src_set.errors, status: :unprocessable_entity
          end
        end
      end
    else
      render status: :forbidden, text: 'Forbidden'
    end
  end

  def show
    @src_set = SrcSet.active.find_by_name!(params[:id])
    @src_images = @src_set.src_images.without_image.active.most_used.page(
      params[:page])
  end

  def destroy
    @src_set = SrcSet.active.find_by_name_and_user_id!(
      params[:id], current_user.try(:id))

    @src_set.update_attribute(:is_deleted, true)
    redirect_to action: :index
  end

  private<commit_msg>Use find_by! instead of dynamic finders.<commit_after># encoding: UTF-8

# Source image sets controller.
class SrcSetsController < ApplicationController
  def new
    @src_set = SrcSet.new
  end

  def create
    @src_set = SrcSet.new(src_set_params)
    @src_set.user = current_user

    if @src_set.save
      redirect_to(
        { action: :index },
        notice: 'Source set created.')
    else
      render :new
    end
  end

  def index
    @src_sets = SrcSet.active.not_empty.most_recent.page(params[:page])
  end

  def update
    # rubocop:disable Style/IfUnlessModifier
    unless current_user
      render status: :forbidden, text: 'Forbidden' && return
    end
    # rubocop:enable Style/IfUnlessModifier

    @src_set = SrcSet.active.where(
      name: params[:id]).first_or_create do |ss|
      ss.user = current_user
    end

    if @src_set.user == current_user
      add_src_images = params.delete(:add_src_images)
      @src_set.add_src_images(add_src_images) if add_src_images
      delete_src_images = params.delete(:delete_src_images)
      @src_set.delete_src_images(delete_src_images) if delete_src_images

      respond_to do |format|
        if @src_set.update_attributes(src_set_params)
          format.html do
            redirect_to(
              { action: :show, id: @src_set.name },
              notice: 'The set was successfully updated.')
          end
          format.json { render json: {} }
        else
          format.html { render :edit }
          format.json do
            render json: @src_set.errors, status: :unprocessable_entity
          end
        end
      end
    else
      render status: :forbidden, text: 'Forbidden'
    end
  end

  def show
    @src_set = SrcSet.active.find_by!(name: params[:id])
    @src_images = @src_set.src_images.without_image.active.most_used.page(
      params[:page])
  end

  def destroy
    @src_set = SrcSet.active.find_by!(
      name: params[:id], user_id: current_user.try(:id))

    @src_set.update_attribute(:is_deleted, true)
    redirect_to action: :index
  end

  private
{'fraction_non_alphanumeric': 0.09207968901846453, 'fraction_numerical': 0.00048590864917395527, 'mean_word_length': 2.60507880910683, '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': '1994918', 'n_tokens_mistral': 1354, 'n_tokens_neox': 1337, 'n_words': 291}