<commit_before>require 'acts-as-taggable-on/version'
raise "Wrong ActsAsTaggableOn version!" unless ActsAsTaggableOn::VERSION == "2.3.3" #Needed to ensure our joins_values hack doesn't fail silently in new versions of AATO

class ItemsController < ApplicationController
  def index
    @items = _search_results.order("#{params[:sort]} #{params[:sort_direction]}").paginate(:page => params[:page], :per_page => 60)
    
    @tags = Item.tag_counts_on(:tags)
  end

  def bulk_export
    _search_results.each { |i| i.export }
  end

  def more_info
    @item = Item.find(params[:id])
    render :layout => 'secondary'
  end

  def update
    @item = Item.find(params[:id])
    if @item.update_attributes(params[:item])
      render :action => 'update_fields'
    else
      #boom
    end
  end

  def destroy
    @item = Item.find(params[:id])

    #if params[:delete]
    @item.delete_original
    #end

    @item.destroy
  end

  def import_and_update
    #Thread.new do #Temporarily remopve threading as it seems to be causing import problems
      Video.import_and_update
      Book.import_and_update
    #end
  end

  def info
    if Item.count == 0
      render :text => 'No items yet', :layout => 'secondary'
    end

    @oldest_book = Book.order('published_on ASC').first
    @newest_book = Book.order('published_on DESC').first
    @longest_book = Book.order('pages DESC').first
    @shortest_book = Book.order('pages ASC').first
    @most_popular_book = Book.order('opens DESC').first
    @least_popular_book = Book.order('opens ASC').first

    @oldest_video = Video.order('published_on ASC').first
    @newest_video = Video.order('published_on DESC').first
    @longest_video = Video.order('pages DESC').first
    @shortest_video = Video.order('pages ASC').first
    @most_popular_video = Video.order('opens DESC').first
    @least_popular_video = Video.order('opens ASC').first
    render :layout => 'secondary'
  end


  #TODO: Move someplace better
  def quit
    Process.kill("TERM", $$)
  end

  def dynamic_stylesheet
    self.formats = [:css]
  end

  private
  def _search_results
    params[:sort] ||= 'created_at'
    params[:sort_direction] ||= 'DESC'
    if !params[:search].blank?
      included_tags, excluded_tags = ActsAsTaggableOn::TagList.from(params[:search]).partition { |t| t.gsub!(/^-/, ''); $& != '-' }

      c = Item.connection

      results = Item

      results = results.where("opens > 0") if included_tags.delete 's:read'
      results = results.where("opens = 0") if included_tags.delete 's:unread'
      #results = results.where("COUNT(taggings.id) > 0") if included_tags.delete 's:tagged'
     
      unless excluded_tags.empty?
        results = results.tagged_with(excluded_tags, :exclude => true)
        excluded_tags_sql = excluded_tags.map { |t| "NOT items.title LIKE #{c.quote "%#{t}%"}" }.join(" AND ")
        results = results.where(excluded_tags_sql)
      end

      unless included_tags.empty?
        results = results.tagged_with(included_tags)
        included_tags_sql = included_tags.map { |t| "items.title LIKE #{c.quote "%#{t}%"}" }.join(" AND ")
        results = results.where("tag_id IS NOT NULL OR (#{included_tags_sql})")
      end

      results.joins_values.first.insert(0, "LEFT ") unless included_tags.empty? && excluded_tags.empty?
<commit_msg>If there are no tags matched by the search, there won't be any joins either<commit_after>require 'acts-as-taggable-on/version'
raise "Wrong ActsAsTaggableOn version!" unless ActsAsTaggableOn::VERSION == "2.3.3" #Needed to ensure our joins_values hack doesn't fail silently in new versions of AATO

class ItemsController < ApplicationController
  def index
    @items = _search_results.order("#{params[:sort]} #{params[:sort_direction]}").paginate(:page => params[:page], :per_page => 60)
    
    @tags = Item.tag_counts_on(:tags)
  end

  def bulk_export
    _search_results.each { |i| i.export }
  end

  def more_info
    @item = Item.find(params[:id])
    render :layout => 'secondary'
  end

  def update
    @item = Item.find(params[:id])
    if @item.update_attributes(params[:item])
      render :action => 'update_fields'
    else
      #boom
    end
  end

  def destroy
    @item = Item.find(params[:id])

    #if params[:delete]
    @item.delete_original
    #end

    @item.destroy
  end

  def import_and_update
    #Thread.new do #Temporarily remopve threading as it seems to be causing import problems
      Video.import_and_update
      Book.import_and_update
    #end
  end

  def info
    if Item.count == 0
      render :text => 'No items yet', :layout => 'secondary'
    end

    @oldest_book = Book.order('published_on ASC').first
    @newest_book = Book.order('published_on DESC').first
    @longest_book = Book.order('pages DESC').first
    @shortest_book = Book.order('pages ASC').first
    @most_popular_book = Book.order('opens DESC').first
    @least_popular_book = Book.order('opens ASC').first

    @oldest_video = Video.order('published_on ASC').first
    @newest_video = Video.order('published_on DESC').first
    @longest_video = Video.order('pages DESC').first
    @shortest_video = Video.order('pages ASC').first
    @most_popular_video = Video.order('opens DESC').first
    @least_popular_video = Video.order('opens ASC').first
    render :layout => 'secondary'
  end


  #TODO: Move someplace better
  def quit
    Process.kill("TERM", $$)
  end

  def dynamic_stylesheet
    self.formats = [:css]
  end

  private
  def _search_results
    params[:sort] ||= 'created_at'
    params[:sort_direction] ||= 'DESC'
    if !params[:search].blank?
      included_tags, excluded_tags = ActsAsTaggableOn::TagList.from(params[:search]).partition { |t| t.gsub!(/^-/, ''); $& != '-' }

      c = Item.connection

      results = Item

      results = results.where("opens > 0") if included_tags.delete 's:read'
      results = results.where("opens = 0") if included_tags.delete 's:unread'
      #results = results.where("COUNT(taggings.id) > 0") if included_tags.delete 's:tagged'
     
      unless excluded_tags.empty?
        results = results.tagged_with(excluded_tags, :exclude => true)
        excluded_tags_sql = excluded_tags.map { |t| "NOT items.title LIKE #{c.quote "%#{t}%"}" }.join(" AND ")
        results = results.where(excluded_tags_sql)
      end

      unless included_tags.empty?
        results = results.tagged_with(included_tags)
        included_tags_sql = included_tags.map { |t| "items.title LIKE #{c.quote "%#{t}%"}" }.join(" AND ")
        results = results.where(results.joins_values.empty? ? included_tags_sql : "tag_id IS NOT NULL OR (#{included_tags_sql})")
      end

      results.joins_values.first.insert(0, "LEFT ") unless (included_tags.empty? && excluded_tags.empty?) || results.joins_values.empty?

{'fraction_non_alphanumeric': 0.13331367055006635, 'fraction_numerical': 0.002949417490045716, 'mean_word_length': 3.482485128883014, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 23, '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': '4607336', 'n_tokens_mistral': 2266, 'n_tokens_neox': 2162, 'n_words': 497}