From c0934318988214615d104dece39f4e5c9e5eaed7 Mon Sep 17 00:00:00 2001 From: joshua stein Date: Thu, 6 Sep 2012 13:44:42 -0500 Subject: [PATCH] allow tags to be filtered by default for new and non-logged-in users --- app/controllers/home_controller.rb | 5 +++++ app/models/user.rb | 10 ++++++++++ app/views/filters/index.html.erb | 12 ++++++++---- db/migrate/20120906183346_default_filtered_tags.rb | 8 ++++++++ db/schema.rb | 7 ++++--- 5 files changed, 35 insertions(+), 7 deletions(-) create mode 100644 db/migrate/20120906183346_default_filtered_tags.rb diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index e997a5f..8d3651f 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -116,6 +116,11 @@ private conds[0] += " AND taggings.tag_id NOT IN (SELECT tag_id FROM " << "tag_filters WHERE user_id = ?)" conds.push @user.id + else + # for logged-out users, filter defaults + conds[0] += " AND taggings.tag_id NOT IN (SELECT id FROM " << + "tags WHERE filtered_by_default = ?)" + conds.push true end stories = Story.find( diff --git a/app/models/user.rb b/app/models/user.rb index 453a643..587742e 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -25,6 +25,7 @@ class User < ActiveRecord::Base :pushover_device, :email_messages, :pushover_messages before_save :check_session_token + after_create :create_default_tag_filters def check_session_token if self.session_token.blank? @@ -32,6 +33,15 @@ class User < ActiveRecord::Base end end + def create_default_tag_filters + Tag.where(:filtered_by_default => true).each do |t| + tf = TagFilter.new + tf.tag_id = t.id + tf.user_id = self.id + tf.save + end + end + def unread_message_count Keystore.value_for("user:#{self.id}:unread_messages").to_i end diff --git a/app/views/filters/index.html.erb b/app/views/filters/index.html.erb index a7ed6ab..44f2b74 100644 --- a/app/views/filters/index.html.erb +++ b/app/views/filters/index.html.erb @@ -4,15 +4,16 @@

- To hide stories tagged with certain tags, check them below. + To hide stories from the home page that have been tagged with certain tags, + check them below.

<%= form_tag "/filters", :method => :post do %> - + - + <% Tag.all(:order => :tag).each do |tag| %> @@ -21,7 +22,10 @@ "checked" : "" %>> - + <% end %>
HideHide TagDescriptionDescription
<%= tag.tag %><%= tag.description %><%= tag.description %> + <% if tag.filtered_by_default? %> + (Filtered by default) + <% end %>
diff --git a/db/migrate/20120906183346_default_filtered_tags.rb b/db/migrate/20120906183346_default_filtered_tags.rb new file mode 100644 index 0000000..827af4a --- /dev/null +++ b/db/migrate/20120906183346_default_filtered_tags.rb @@ -0,0 +1,8 @@ +class DefaultFilteredTags < ActiveRecord::Migration + def up + add_column :tags, :filtered_by_default, :boolean, :default => false + end + + def down + end +end diff --git a/db/schema.rb b/db/schema.rb index bae7fcd..018a07a 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20120902143549) do +ActiveRecord::Schema.define(:version => 20120906183346) do create_table "comments", :force => true do |t| t.datetime "created_at", :null => false @@ -112,8 +112,9 @@ ActiveRecord::Schema.define(:version => 20120902143549) do add_index "taggings", ["story_id", "tag_id"], :name => "story_id_tag_id", :unique => true create_table "tags", :force => true do |t| - t.string "tag", :limit => 25, :default => "", :null => false - t.string "description", :limit => 100 + t.string "tag", :limit => 25, :default => "", :null => false + t.string "description", :limit => 100 + t.boolean "filtered_by_default", :default => false end add_index "tags", ["tag"], :name => "tag", :unique => true