<commit_before>class ChannelsController < ApplicationController

  layout "channels"

  before_filter :load_channel,
    :only => [
      :show,
      :edit,
      :destroy,
      :update,
      :facts,
      :create_fact,
      :activities,
      :remove_fact,
      :toggle_fact,
      :remove_fact,
      :follow,
      :last_fact_activity
    ]

  before_filter :get_user

  respond_to :json, :html

  before_filter :authenticate_user!


  def index
    authorize! :index, Channel

    respond_to do |format|
      format.json { render :json => channels_for_user(@user).map {|ch| Channels::Channel.for(channel: ch,view: view_context,channel_user: @user)} }
    end
  end

  def backbone_page
    render_backbone_page
  end

  def show
    authorize! :show, @channel
    respond_to do |format|
      format.json { render :json => Channels::Channel.for(channel: @channel,view: view_context,channel_user: @user)}
      format.js
      format.html do
        render_backbone_page
        mark_channel_as_read
      end
    end
  end

  def new
    authorize! :new, Channel
    @channel = Channel.new
  end

  def edit
    authorize! :edit, @channel
  end

  #TODO Move to topicscontroller, this searches for topics, not for channels
  def search
    @topics = interactor :search_channel, params[:s]
    render 'topics/index'
  end

  def create
    authorize! :update, @user

    @channel = Channel.new(params[:channel].andand.slice(:title) || params.slice(:title))
    @channel.created_by = current_user.graph_user

    # Check if object valid, then execute:
    if @channel.valid?
      @channel.save

      unless params[:for_fact].nil?
        @fact = Fact[params[:for_fact]]
        @channel.add_fact(@fact)
      end

      unless params[:for_channel].nil?
        @subchannel = Channel[params[:for_channel]]
        @channel.add_channel(@subchannel)
      end

      respond_to do |format|
        format.html { redirect_to(channel_path(@channel.created_by.user, @channel), :notice => 'Channel successfully created') }
        format.json { render :json => Channels::Channel.for(channel: @channel,view: view_context)}
      end

    else
      respond_to do |format|
        format.html { render :new }
        format.json { render :json => @channel.errors,
                      :status => :unprocessable_entity }<commit_msg>Created some sort of get_or_create functionality which should resolve @merijn481 his issues<commit_after>class ChannelsController < ApplicationController

  layout "channels"

  before_filter :load_channel,
    :only => [
      :show,
      :edit,
      :destroy,
      :update,
      :facts,
      :create_fact,
      :activities,
      :remove_fact,
      :toggle_fact,
      :remove_fact,
      :follow,
      :last_fact_activity
    ]

  before_filter :get_user

  respond_to :json, :html

  before_filter :authenticate_user!


  def index
    authorize! :index, Channel

    respond_to do |format|
      format.json { render :json => channels_for_user(@user).map {|ch| Channels::Channel.for(channel: ch,view: view_context,channel_user: @user)} }
    end
  end

  def backbone_page
    render_backbone_page
  end

  def show
    authorize! :show, @channel
    respond_to do |format|
      format.json { render :json => Channels::Channel.for(channel: @channel,view: view_context,channel_user: @user)}
      format.js
      format.html do
        render_backbone_page
        mark_channel_as_read
      end
    end
  end

  def new
    authorize! :new, Channel
    @channel = Channel.new
  end

  def edit
    authorize! :edit, @channel
  end

  #TODO Move to topicscontroller, this searches for topics, not for channels
  def search
    @topics = interactor :search_channel, params[:s]
    render 'topics/index'
  end

  def create
    authorize! :update, @user

    # Late night drunk hacking going on
    # Blame it on the a-a-a-a-a-alcohol
    title_hash = params[:channel].andand.slice(:title) || params.slice(:title)
    title = title_hash[:title]

    @channels = Channel.find(created_by_id: current_user.graph_user_id)
    @channels.each do |channel|
      @channel = channel if channel.lowercase_title == title.downcase
    end

    unless @channel
      @channel = Channel.new(title)
      @channel.created_by = current_user.graph_user
    end

    # Check if object valid, then execute:
    if @channel.valid?
      @channel.save

      unless params[:for_fact].nil?
        @fact = Fact[params[:for_fact]]
        @channel.add_fact(@fact)
      end

      unless params[:for_channel].nil?
        @subchannel = Channel[params[:for_channel]]
        @channel.add_channel(@subchannel)
      end

      respond_to do |format|
        format.html { redirect_to(channel_path(@channel.created_by.user, @channel), :notice => 'Channel successfully created') }
        format.json { render :json => Channels::Channel.for(channel: @channel,view: view_context)}
      end

    else
      respond_to do |format|
        format.html { render :new }
        format.json { render :json => @channel.errors,
                      :status => :unprocessable_entity }
{'fraction_non_alphanumeric': 0.10188457008244994, 'fraction_numerical': 0.0005889281507656066, 'mean_word_length': 2.816479400749064, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, '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': '3006563', 'n_tokens_mistral': 1597, 'n_tokens_neox': 1539, 'n_words': 377}