Skip to content

Commit c093431

Browse files
committed
allow tags to be filtered by default for new and non-logged-in users
1 parent d986952 commit c093431

File tree

5 files changed

+35
-7
lines changed

5 files changed

+35
-7
lines changed

app/controllers/home_controller.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ def _find_stories_for_user_and_tag_and_newest_and_by_user(user, tag = nil,
116116
conds[0] += " AND taggings.tag_id NOT IN (SELECT tag_id FROM " <<
117117
"tag_filters WHERE user_id = ?)"
118118
conds.push @user.id
119+
else
120+
# for logged-out users, filter defaults
121+
conds[0] += " AND taggings.tag_id NOT IN (SELECT id FROM " <<
122+
"tags WHERE filtered_by_default = ?)"
123+
conds.push true
119124
end
120125

121126
stories = Story.find(

app/models/user.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,23 @@ class User < ActiveRecord::Base
2525
:pushover_device, :email_messages, :pushover_messages
2626

2727
before_save :check_session_token
28+
after_create :create_default_tag_filters
2829

2930
def check_session_token
3031
if self.session_token.blank?
3132
self.session_token = Utils.random_str(60)
3233
end
3334
end
3435

36+
def create_default_tag_filters
37+
Tag.where(:filtered_by_default => true).each do |t|
38+
tf = TagFilter.new
39+
tf.tag_id = t.id
40+
tf.user_id = self.id
41+
tf.save
42+
end
43+
end
44+
3545
def unread_message_count
3646
Keystore.value_for("user:#{self.id}:unread_messages").to_i
3747
end

app/views/filters/index.html.erb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@
44
</div>
55

66
<p>
7-
To hide stories tagged with certain tags, check them below.
7+
To hide stories from the home page that have been tagged with certain tags,
8+
check them below.
89
</p>
910

1011
<%= form_tag "/filters", :method => :post do %>
1112
<table class="data zebra" cellspacing=0 width="75%">
1213
<tr>
13-
<th width="10%">Hide</th>
14+
<th width="7%">Hide</th>
1415
<th width="15%">Tag</th>
15-
<th width="75%">Description</th>
16+
<th width="78%">Description</th>
1617
</tr>
1718
<% Tag.all(:order => :tag).each do |tag| %>
1819
<tr>
@@ -21,7 +22,10 @@
2122
"checked" : "" %>></td>
2223
<td><a href="/t/<%= tag.tag %>" class="tag tag_<%= tag.tag %>"><%=
2324
tag.tag %></a></td>
24-
<td><%= tag.description %></td>
25+
<td><%= tag.description %>
26+
<% if tag.filtered_by_default? %>
27+
<em>(Filtered by default)</em>
28+
<% end %></td>
2529
</tr>
2630
<% end %>
2731
</table>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class DefaultFilteredTags < ActiveRecord::Migration
2+
def up
3+
add_column :tags, :filtered_by_default, :boolean, :default => false
4+
end
5+
6+
def down
7+
end
8+
end

db/schema.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#
1212
# It's strongly recommended to check this file into your version control system.
1313

14-
ActiveRecord::Schema.define(:version => 20120902143549) do
14+
ActiveRecord::Schema.define(:version => 20120906183346) do
1515

1616
create_table "comments", :force => true do |t|
1717
t.datetime "created_at", :null => false
@@ -112,8 +112,9 @@
112112
add_index "taggings", ["story_id", "tag_id"], :name => "story_id_tag_id", :unique => true
113113

114114
create_table "tags", :force => true do |t|
115-
t.string "tag", :limit => 25, :default => "", :null => false
116-
t.string "description", :limit => 100
115+
t.string "tag", :limit => 25, :default => "", :null => false
116+
t.string "description", :limit => 100
117+
t.boolean "filtered_by_default", :default => false
117118
end
118119

119120
add_index "tags", ["tag"], :name => "tag", :unique => true

0 commit comments

Comments
 (0)