add tag filters
This commit is contained in:
parent
7611e7d297
commit
a2705a835e
|
@ -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
|
||||
|
|
36
app/controllers/filters_controller.rb
Normal file
36
app/controllers/filters_controller.rb
Normal file
|
@ -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
|
4
app/models/tag_filter.rb
Normal file
4
app/models/tag_filter.rb
Normal file
|
@ -0,0 +1,4 @@
|
|||
class TagFilter < ActiveRecord::Base
|
||||
belongs_to :tag
|
||||
belongs_to :user
|
||||
end
|
|
@ -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/
|
||||
|
|
32
app/views/filters/index.html.erb
Normal file
32
app/views/filters/index.html.erb
Normal file
|
@ -0,0 +1,32 @@
|
|||
<div class="box wide">
|
||||
<div class="legend">
|
||||
Filtered Tags
|
||||
</div>
|
||||
|
||||
<p>
|
||||
To hide stories tagged with certain tags, check them below.
|
||||
</p>
|
||||
|
||||
<%= form_tag "/filters", :method => :post do %>
|
||||
<table class="data zebra" cellspacing=0 width="75%">
|
||||
<tr>
|
||||
<th width="10%">Hide</th>
|
||||
<th width="15%">Tag</th>
|
||||
<th width="75%">Description</th>
|
||||
</tr>
|
||||
<% Tag.all(:order => :tag).each do |tag| %>
|
||||
<tr>
|
||||
<td><input type="checkbox" name="tag_<%= tag.tag %>" value=1
|
||||
<%= @filtered_tags.map{|ft| ft.tag_id }.include?(tag.id) ?
|
||||
"checked" : "" %>></td>
|
||||
<td><a href="/t/<%= tag.tag %>" class="tag tag_<%= tag.tag %>"><%=
|
||||
tag.tag %></a></td>
|
||||
<td><%= tag.description %></td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</table>
|
||||
<p>
|
||||
<%= submit_tag "Save Filters" %>
|
||||
</p>
|
||||
<% end %>
|
||||
</div>
|
|
@ -1,6 +1,8 @@
|
|||
<div id="header">
|
||||
<div id="headerright" class="<%= @user ? "loggedin" : "" %>">
|
||||
<% if @user %>
|
||||
<a href="/filters">Filters</a>
|
||||
|
||||
<% if (count = @user.unread_message_count) > 0 %>
|
||||
<a href="/messages"><%= count %> New Message<%= count == 1 ? "" : "s"
|
||||
%></a>
|
||||
|
|
|
@ -51,6 +51,9 @@ Lobsters::Application.routes.draw do
|
|||
get "/settings" => "settings#index"
|
||||
post "/settings" => "settings#update"
|
||||
|
||||
get "/filters" => "filters#index"
|
||||
post "/filters" => "filters#update"
|
||||
|
||||
post "/invitations" => "invitations#create"
|
||||
get "/invitations/:invitation_code" => "signup#invited"
|
||||
end
|
||||
|
|
12
db/migrate/20120704025956_user_filter.rb
Normal file
12
db/migrate/20120704025956_user_filter.rb
Normal file
|
@ -0,0 +1,12 @@
|
|||
class UserFilter < ActiveRecord::Migration
|
||||
def up
|
||||
create_table :tag_filters do |t|
|
||||
t.timestamps
|
||||
t.integer :user_id
|
||||
t.integer :tag_id
|
||||
end
|
||||
end
|
||||
|
||||
def down
|
||||
end
|
||||
end
|
|
@ -11,7 +11,7 @@
|
|||
#
|
||||
# It's strongly recommended to check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(:version => 20120704013019) do
|
||||
ActiveRecord::Schema.define(:version => 20120704025956) do
|
||||
|
||||
create_table "comments", :force => true do |t|
|
||||
t.datetime "created_at", :null => false
|
||||
|
@ -79,6 +79,13 @@ ActiveRecord::Schema.define(:version => 20120704013019) do
|
|||
add_index "stories", ["hotness"], :name => "hotness_idx"
|
||||
add_index "stories", ["url"], :name => "url"
|
||||
|
||||
create_table "tag_filters", :force => true do |t|
|
||||
t.datetime "created_at", :null => false
|
||||
t.datetime "updated_at", :null => false
|
||||
t.integer "user_id"
|
||||
t.integer "tag_id"
|
||||
end
|
||||
|
||||
create_table "taggings", :force => true do |t|
|
||||
t.integer "story_id", :null => false
|
||||
t.integer "tag_id", :null => false
|
||||
|
|
Loading…
Reference in a new issue