<commit_before>class PadsController < ApplicationController
  include Etherpad
  include Mediawiki
  helper_method :sort_column, :sort_direction
  layout 'pad', only: [:show]
  before_filter :authenticate_user!, except: [:show, :index]
  before_action :set_pad, only: [:show, :edit, :update, :destroy]
  load_and_authorize_resource

  # GET /pads
  # GET /pads.json
  def index
    if params[:group_id].present?
    @group = Group.find(params[:group_id])
    else
    @group = Group.find_or_create_by_name('ungrouped')
    end

    if current_user.nil?
      @group = Group.find_by_name('ungrouped')
    end

    @pads = Pad.joins(:group, :creator).order(sort_column + ' ' + sort_direction)
    @pads = @pads.where("pads.group_id = ?", @group.id)
    @pads = @pads.where(is_public: true) if current_user.nil?
    @pads = @pads.all
  end

  # GET /p/1
  # GET /p/1.json
  def show
    cookies[:sessionID] = nil

    unless current_user.nil?
      @author = ether.author(current_user.name, name: current_user.name)
      @author.sessions.each do |sess|
        if sess.expired? || !@pad.group.users.include?(current_user)
          sess.delete
        end
      end
      if @pad.group.users.include?(current_user) || @pad.group.name == 'ungrouped'
        sess = @pad.group.ep_group.create_session(@author, 480)
        cookies[:sessionID] = {:value => sess.id}
      end
    end
  end

  # GET /pads/new
  def new
    @pad = Pad.new
    @group = Group.find(params[:group_id])
  end

  # GET /pads/1/edit
  def edit
  end

  # POST /pads
  # POST /pads.json
  def create
    @pad = Pad.new(pad_params)
    @group = Group.find(params[:group_id])

    @pad.creator_id = current_user.id
    @pad.group = @group

    respond_to do |format|
      if @pad.save
        logger.error { "FOOOOOO" }
        format.html {
          pad_url = '/p/'+@group.name+'/'+@pad.name
          pad_url = '/p/'+@pad.name if @group.name == 'ungrouped'
          redirect_to pad_url, notice: 'Pad was successfully created.'
        }
        format.json { render action: 'show', status: :created, location: @pad }
      else
        format.html { render action: 'new', error: 'Error' }
        format.json { render json: @pad.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /pads/1
  # PATCH/PUT /pads/1.json
  def update
    if params[:pad][:wiki_page].present?
      mw.edit(params[:pad][:wiki_page], @pad.ep_pad.text, :summary => 'vie Eplmgmt by '+current_user.name)
      @pad.wiki_page = params[:pad][:wiki_page]
      @pad.save
    end

    respond_to do |format|
      if @pad.update(pad_params)
        format.html { 
          if (params[:pad][:delete_ep_pad] == 'true') && params[:pad][:wiki_page].present?
            @pad.destroy
            redirect_to ENV['MW_URL']+'/wiki/'+params[:pad][:wiki_page], notice: 'Pad was successfully updated'
          elsif params[:pad][:wiki_page].present?
            redirect_to ENV['MW_URL']+'/wiki/'+params[:pad][:wiki_page], notice: 'Pad was successfully updated'
          else
            redirect_to edit_pad_path(@pad), notice: 'Pad was successfully updated'
          end
        }<commit_msg>fixed typo<commit_after>class PadsController < ApplicationController
  include Etherpad
  include Mediawiki
  helper_method :sort_column, :sort_direction
  layout 'pad', only: [:show]
  before_filter :authenticate_user!, except: [:show, :index]
  before_action :set_pad, only: [:show, :edit, :update, :destroy]
  load_and_authorize_resource

  # GET /pads
  # GET /pads.json
  def index
    if params[:group_id].present?
    @group = Group.find(params[:group_id])
    else
    @group = Group.find_or_create_by_name('ungrouped')
    end

    if current_user.nil?
      @group = Group.find_by_name('ungrouped')
    end

    @pads = Pad.joins(:group, :creator).order(sort_column + ' ' + sort_direction)
    @pads = @pads.where("pads.group_id = ?", @group.id)
    @pads = @pads.where(is_public: true) if current_user.nil?
    @pads = @pads.all
  end

  # GET /p/1
  # GET /p/1.json
  def show
    cookies[:sessionID] = nil

    unless current_user.nil?
      @author = ether.author(current_user.name, name: current_user.name)
      @author.sessions.each do |sess|
        if sess.expired? || !@pad.group.users.include?(current_user)
          sess.delete
        end
      end
      if @pad.group.users.include?(current_user) || @pad.group.name == 'ungrouped'
        sess = @pad.group.ep_group.create_session(@author, 480)
        cookies[:sessionID] = {:value => sess.id}
      end
    end
  end

  # GET /pads/new
  def new
    @pad = Pad.new
    @group = Group.find(params[:group_id])
  end

  # GET /pads/1/edit
  def edit
  end

  # POST /pads
  # POST /pads.json
  def create
    @pad = Pad.new(pad_params)
    @group = Group.find(params[:group_id])

    @pad.creator_id = current_user.id
    @pad.group = @group

    respond_to do |format|
      if @pad.save
        logger.error { "FOOOOOO" }
        format.html {
          pad_url = '/p/'+@group.name+'/'+@pad.name
          pad_url = '/p/'+@pad.name if @group.name == 'ungrouped'
          redirect_to pad_url, notice: 'Pad was successfully created.'
        }
        format.json { render action: 'show', status: :created, location: @pad }
      else
        format.html { render action: 'new', error: 'Error' }
        format.json { render json: @pad.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /pads/1
  # PATCH/PUT /pads/1.json
  def update
    if params[:pad][:wiki_page].present?
      mw.edit(params[:pad][:wiki_page], @pad.ep_pad.text, :summary => 'via Eplmgmt by '+current_user.name)
      @pad.wiki_page = params[:pad][:wiki_page]
      @pad.save
    end

    respond_to do |format|
      if @pad.update(pad_params)
        format.html { 
          if (params[:pad][:delete_ep_pad] == 'true') && params[:pad][:wiki_page].present?
            @pad.destroy
            redirect_to ENV['MW_URL']+'/wiki/'+params[:pad][:wiki_page], notice: 'Pad was successfully updated'
          elsif params[:pad][:wiki_page].present?
            redirect_to ENV['MW_URL']+'/wiki/'+params[:pad][:wiki_page], notice: 'Pad was successfully updated'
          else
            redirect_to edit_pad_path(@pad), notice: 'Pad was successfully updated'
          end
        }
{'fraction_non_alphanumeric': 0.14070670258279194, 'fraction_numerical': 0.0025352559023926477, 'mean_word_length': 2.712941176470588, 'pattern_counts': {'":': 0, '<': 5, '<?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': '953304', 'n_tokens_mistral': 2230, 'n_tokens_neox': 2177, 'n_words': 453}