From a2705a835efa22063586a677770b83d0264975f4 Mon Sep 17 00:00:00 2001 From: joshua stein Date: Tue, 3 Jul 2012 22:24:18 -0500 Subject: [PATCH] add tag filters --- app/controllers/application_controller.rb | 10 +++++-- app/controllers/filters_controller.rb | 36 +++++++++++++++++++++++ app/models/tag_filter.rb | 4 +++ app/models/user.rb | 2 ++ app/views/filters/index.html.erb | 32 ++++++++++++++++++++ app/views/global/_header.html.erb | 2 ++ config/routes.rb | 3 ++ db/migrate/20120704025956_user_filter.rb | 12 ++++++++ db/schema.rb | 9 +++++- 9 files changed, 107 insertions(+), 3 deletions(-) create mode 100644 app/controllers/filters_controller.rb create mode 100644 app/models/tag_filter.rb create mode 100644 app/views/filters/index.html.erb create mode 100644 db/migrate/20120704025956_user_filter.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index d9cab7f..9631116 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -44,11 +44,17 @@ class ApplicationController < ActionController::Base conds.push tag.id stories = Story.find(:all, :conditions => conds, :include => [ :user, { :taggings => :tag } ], :limit => 30, - :order => (newest ? "created_at DESC" : "hotness")) + :order => (newest ? "stories.created_at DESC" : "hotness")) else + if user + conds[0] += " AND taggings.tag_id NOT IN (SELECT tag_id FROM " << + "tag_filters WHERE user_id = ?)" + conds.push @user.id + end + stories = Story.find(:all, :conditions => conds, :include => [ :user, { :taggings => :tag } ], :limit => 30, - :order => (newest ? "created_at DESC" : "hotness")) + :order => (newest ? "stories.created_at DESC" : "hotness")) end # TODO: figure out a better sorting algorithm for newest, including some diff --git a/app/controllers/filters_controller.rb b/app/controllers/filters_controller.rb new file mode 100644 index 0000000..a0d46a1 --- /dev/null +++ b/app/controllers/filters_controller.rb @@ -0,0 +1,36 @@ +class FiltersController < ApplicationController + before_filter :require_logged_in_user + + def index + @filtered_tags = @user.tag_filters + render :action => "index" + end + + def update + new_filters = [] + + params.each do |k,v| + if (m = k.match(/^tag_(.+)$/)) && v.to_i == 1 && Tag.find_by_tag(m[1]) + new_filters.push m[1] + end + end + + @user.tag_filters(:include => :tag).each do |tf| + if new_filters.include?(tf.tag.tag) + new_filters.reject!{|t| t == tf.tag.tag } + else + tf.destroy + end + end + + new_filters.each do |t| + TagFilter.new({ + :user_id => @user.id, + :tag_id => Tag.find_by_tag(t).id, + }).save + end + + flash.now[:success] = "Your filters have been updated." + index + end +end diff --git a/app/models/tag_filter.rb b/app/models/tag_filter.rb new file mode 100644 index 0000000..b528c07 --- /dev/null +++ b/app/models/tag_filter.rb @@ -0,0 +1,4 @@ +class TagFilter < ActiveRecord::Base + belongs_to :tag + belongs_to :user +end diff --git a/app/models/user.rb b/app/models/user.rb index 2d40eeb..abb1b13 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -8,6 +8,8 @@ class User < ActiveRecord::Base has_many :received_messages, :class_name => "Message", :foreign_key => "recipient_user_id" + has_many :tag_filters + has_secure_password validates_format_of :username, :with => /\A[A-Za-z0-9][A-Za-z0-9_-]*\Z/ diff --git a/app/views/filters/index.html.erb b/app/views/filters/index.html.erb new file mode 100644 index 0000000..a7ed6ab --- /dev/null +++ b/app/views/filters/index.html.erb @@ -0,0 +1,32 @@ +
+
+ Filtered Tags +
+ +

+ To hide stories tagged with certain tags, check them below. +

+ + <%= form_tag "/filters", :method => :post do %> + + + + + + + <% Tag.all(:order => :tag).each do |tag| %> + + + + + + <% end %> +
HideTagDescription
><%= + tag.tag %><%= tag.description %>
+

+ <%= submit_tag "Save Filters" %> +

+ <% end %> +
diff --git a/app/views/global/_header.html.erb b/app/views/global/_header.html.erb index 2937f74..5efe656 100644 --- a/app/views/global/_header.html.erb +++ b/app/views/global/_header.html.erb @@ -1,6 +1,8 @@