<commit_before>class CategoriesController < ApplicationController
  include SocialStream::Controllers::Objects

  #before_filter :add_item_to_categories, :only => [:create, :update]
  skip_load_and_authorize_resource :only => [:add_items]
  
  def show_favorites
    render "favorites"
  end



  def create
    create! do |success, failure|
      success.json { render :json => {"title"=>@category.title, "id"=>@category.id}, :status => 200 }
      #failure.json { render :json => {"errors" => @category.errors}, :status => 400}
      failure.json { render :json => {"errors" => @category.errors.full_messages.to_sentence}, :status => 400}
    end
  end

  def update
    update! do |format|
      format.json {render :json => { :success => true }}
    end
  end

  def add_items
    if params[:item_type].present? && params["categories_array"].present?
      cat_array = JSON.parse(params["categories_array"])
      #2 actions, remove categories that are no longer assinged and apply new ones
      item = params[:item_type].constantize.find(params[:item_id]).activity_object
      item.holder_categories.each do |cat|
        if cat_array.include?(cat.id)
          cat_array.delete(cat.id) #remove item because the category is already applied
          next
        end
        the_cat = Category.find(cat.id)
        the_cat.property_objects.delete(item)
      end
      #now apply the rest of categories, the new ones
      cat_array.each do |new_cat_id|
        new_cat = Category.find(new_cat_id)
        new_cat.property_objects << item
      end
      render :json => { :success => true }
    end
  end


  def destroy
    super do |format|
      format.html {
        redirect_to user_path(current_user)
       }

      format.js
    end
   end

  private

  def add_item_to_categories
    if params[:item_type].present?
      included = @category.property_objects.include? params[:item_type].constantize.find(params[:item_id]).activity_object<commit_msg>categories bug fix<commit_after>class CategoriesController < ApplicationController
  include SocialStream::Controllers::Objects

  #before_filter :add_item_to_categories, :only => [:create, :update]
  skip_load_and_authorize_resource :only => [:add_items]
  
  def show_favorites
    render "favorites"
  end



  def create
    create! do |success, failure|
      success.json { render :json => {"title"=>@category.title, "id"=>@category.id}, :status => 200 }
      #failure.json { render :json => {"errors" => @category.errors}, :status => 400}
      failure.json { render :json => {"errors" => @category.errors.full_messages.to_sentence}, :status => 400}
    end
  end

  def update
    update! do |format|
      format.json {render :json => { :success => true }}
    end
  end

  def add_items
    if params[:item_type].present? && params["categories_array"].present?
      cat_array = JSON.parse(params["categories_array"])
      #2 actions, remove categories that are no longer assinged and apply new ones
      item = params[:item_type].constantize.find(params[:item_id]).activity_object
      item.holder_categories.each do |cat|
        if cat_array.include?(cat.id)
          cat_array.delete(cat.id) #remove item because the category is already applied
          next
        end
        #the_cat = Category.find(cat.id)
        #the_cat.property_objects.delete(item)
      end
      #now apply the rest of categories, the new ones
      cat_array.each do |new_cat_id|
        new_cat = Category.find(new_cat_id)
        new_cat.property_objects << item
      end
      render :json => { :success => true }
    end
  end


  def destroy
    super do |format|
      format.html {
        redirect_to user_path(current_user)
       }

      format.js
    end
   end

  private

  def add_item_to_categories
    if params[:item_type].present?
      included = @category.property_objects.include? params[:item_type].constantize.find(params[:item_id]).activity_object
{'fraction_non_alphanumeric': 0.11018024879411017, 'fraction_numerical': 0.005077430820005078, 'mean_word_length': 3.134312696747114, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 35, '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': '1354717', 'n_tokens_mistral': 1211, 'n_tokens_neox': 1172, 'n_words': 287}