2012-07-04 05:24:18 +02:00
|
|
|
class FiltersController < ApplicationController
|
2013-08-05 09:16:33 +02:00
|
|
|
before_filter :authenticate_user
|
2012-07-04 05:24:18 +02:00
|
|
|
|
|
|
|
def index
|
2012-09-07 16:18:15 +02:00
|
|
|
@cur_url = "/filters"
|
2012-09-20 17:56:54 +02:00
|
|
|
@title = "Filtered Tags"
|
2012-09-07 16:18:15 +02:00
|
|
|
|
2013-08-05 09:16:33 +02:00
|
|
|
if @user
|
|
|
|
@filtered_tags = @user.tag_filters.reload
|
|
|
|
else
|
|
|
|
@filtered_tags = tags_filtered_by_cookie.map{|t|
|
|
|
|
tf = TagFilter.new
|
|
|
|
tf.tag = t
|
|
|
|
tf
|
|
|
|
}
|
|
|
|
end
|
2012-09-07 16:18:15 +02:00
|
|
|
|
2012-07-04 05:24:18 +02:00
|
|
|
render :action => "index"
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
new_filters = []
|
|
|
|
|
|
|
|
params.each do |k,v|
|
2012-09-20 17:56:31 +02:00
|
|
|
if (m = k.match(/^tag_(.+)$/)) && v.to_i == 1 &&
|
|
|
|
(t = Tag.find_by_tag(m[1])) && t.valid_for?(@user)
|
2012-07-04 05:24:18 +02:00
|
|
|
new_filters.push m[1]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-08-05 09:16:33 +02:00
|
|
|
if @user
|
|
|
|
@user.tag_filters(:include => :tag).each do |tf|
|
|
|
|
if tf.tag && new_filters.include?(tf.tag.tag)
|
|
|
|
new_filters.reject!{|t| t == tf.tag.tag }
|
|
|
|
else
|
|
|
|
tf.destroy
|
|
|
|
end
|
2012-07-04 05:24:18 +02:00
|
|
|
end
|
|
|
|
|
2013-08-05 09:16:33 +02:00
|
|
|
new_filters.each do |t|
|
|
|
|
tf = TagFilter.new
|
|
|
|
tf.user_id = @user.id
|
|
|
|
tf.tag_id = Tag.find_by_tag(t).id
|
|
|
|
tf.save!
|
|
|
|
end
|
|
|
|
else
|
|
|
|
cookies.permanent[TAG_FILTER_COOKIE] = new_filters.join(",")
|
2012-07-04 05:24:18 +02:00
|
|
|
end
|
|
|
|
|
2013-08-05 09:16:33 +02:00
|
|
|
flash[:success] = "Your filters have been updated."
|
|
|
|
return redirect_to "/filters"
|
2012-07-04 05:24:18 +02:00
|
|
|
end
|
|
|
|
end
|