<commit_before>class FavouritesController < ApplicationController
  def add
    #TODO validate id, and only add if not a duplicate
    split_id=params[:id].split("_")
    f=Favourite.new
    f.user=current_user
    f.model_name=split_id[1]
    f.asset_id=split_id[2].to_i
    f.save
    render :update do |page|
        page.replace_html "favourite_list", :partial=>"favourites/gadget_list"
        page.visual_effect :highlight, "drop_favourites", :startcolor=>"#DDDDFF"
    end
  end
  
  def delete
    id=params[:id].split("_")[1].to_i
    Favourite.find(id).destroy
    render :update do |page|
        page.replace_html "favourite_list", :partial=>"favourites/gadget_list"
        page.visual_effect :highlight, "drop_favourites", :startcolor=>"#DDDDFF"
    end 
  end
end<commit_msg>prevent duplicate favourites and protect that the one being deleted is for the current user. before_filter :login_required too.<commit_after>class FavouritesController < ApplicationController
  
  before_filter :login_required
  
  def add
    #FIXME validate id with a regular expression
    split_id=params[:id].split("_")
    f=Favourite.new
    f.user=current_user
    f.model_name=split_id[1]
    f.asset_id=split_id[2].to_i
    if Favourite.find_by_user_id_and_model_name_and_asset_id(current_user,f.model_name,f.asset_id).nil?
      f.save
      render :update do |page|
          page.replace_html "favourite_list", :partial=>"favourites/gadget_list"
          page.visual_effect :highlight, "drop_favourites", :startcolor=>"#DDDDFF"
      end
    else
      render :update do |page|
        page.visual_effect :highlight, "drop_favourites", :startcolor=>"#FF0000"
      end
    end
    
  end
  
  def delete
    id=params[:id].split("_")[1].to_i
    f=Favourite.find(id)
    if !f.nil? and f.user==current_user
      f.destroy
      render :update do |page|
          page.replace_html "favourite_list", :partial=>"favourites/gadget_list"
          page.visual_effect :highlight, "drop_favourites", :startcolor=>"#DDDDFF"
      end 
    else
      render :update do |page|
        page.visual_effect :highlight, "drop_favourites", :startcolor=>"#FF0000"
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.10958276020174232, 'fraction_numerical': 0.006419073819348923, 'mean_word_length': 3.4804928131416837, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, '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': '3299253', 'n_tokens_mistral': 788, 'n_tokens_neox': 750, 'n_words': 148}