journalduhacker/app/controllers/filters_controller.rb
joshua stein 249dd85ec3 allow non-logged-in users to define tag filters
when not logged in, store the filters in a long-lasting cookie and
do not cache the home page

for that one guy on hacker news that complained about lobste.rs not
having this
2013-08-05 02:19:55 -05:00

54 lines
1.2 KiB
Ruby

class FiltersController < ApplicationController
before_filter :authenticate_user
def index
@cur_url = "/filters"
@title = "Filtered Tags"
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
render :action => "index"
end
def update
new_filters = []
params.each do |k,v|
if (m = k.match(/^tag_(.+)$/)) && v.to_i == 1 &&
(t = Tag.find_by_tag(m[1])) && t.valid_for?(@user)
new_filters.push m[1]
end
end
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
end
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(",")
end
flash[:success] = "Your filters have been updated."
return redirect_to "/filters"
end
end