Skip to content

Commit

Permalink
Merge branch 'tag-exclusions' into diaspora-pistos-1.0.6
Browse files Browse the repository at this point in the history
  • Loading branch information
Pistos committed Jan 13, 2012
2 parents 8af5ec7 + ca99ed1 commit 884be13
Show file tree
Hide file tree
Showing 14 changed files with 172 additions and 7 deletions.
43 changes: 43 additions & 0 deletions app/controllers/tag_exclusions_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class TagExclusionsController < ApplicationController
before_filter :authenticate_user!

# POST /tag_exclusions
def create
name_normalized = ActsAsTaggableOn::Tag.normalize(params['tag_exclusion']['name'])

if name_normalized.nil? || name_normalized.empty?
flash[:error] = I18n.t('tag_exclusions.create.none')
else
tag = ActsAsTaggableOn::Tag.find_or_create_by_name(name_normalized)
tag_exclusion = current_user.tag_exclusions.new(:tag_id => tag.id)

if tag_exclusion.save
flash[:notice] = I18n.t('tag_exclusions.create.success', :name => name_normalized)
else
flash[:error] = I18n.t('tag_exclusions.create.failure', :name => name_normalized)
end
end

redirect_to :back
end

# DELETE /tag_exclusions/1
def destroy
@tag_exclusion = current_user.tag_exclusions.where(:id => params['id']).first
success = @tag_exclusion && @tag_exclusion.destroy

if params[:remote]
respond_to do |format|
format.all {}
format.js { render 'tags/update' } # Hmm... this is probably not appropriate
end
else
if success
flash[:notice] = I18n.t('tag_exclusions.destroy.success', :name => @tag_exclusion.tag.name)
else
flash[:error] = I18n.t('tag_exclusions.destroy.failure', :name => @tag_exclusion.tag.name)
end
redirect_to filters_path
end
end
end
4 changes: 4 additions & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ def privacy_settings
@blocks = current_user.blocks.includes(:person)
end

def filters
@tag_exclusions = current_user.tag_exclusions
end

def update
password_changed = false
@user = current_user
Expand Down
6 changes: 3 additions & 3 deletions app/models/status_message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class StatusMessage < Post
scope :where_person_is_mentioned, lambda { |person|
joins(:mentions).where(:mentions => {:person_id => person.id})
}

scope :commented_by, lambda { |person|
select('DISTINCT posts.*').joins(:comments).where(:comments => {:author_id => person.id})
}
Expand Down Expand Up @@ -168,8 +168,8 @@ def text_and_photos_blank?

def queue_gather_oembed_data
Resque.enqueue(Jobs::GatherOEmbedData, self.id, self.oembed_url)
end
end

def contains_oembed_url_in_text?
require 'uri'
urls = URI.extract(self.raw_message, ['http', 'https'])
Expand Down
6 changes: 6 additions & 0 deletions app/models/tag_exclusion.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class TagExclusion < ActiveRecord::Base
belongs_to :user
belongs_to :tag, :class_name => "ActsAsTaggableOn::Tag"

validates_uniqueness_of :tag_id, :scope => :user_id
end
2 changes: 2 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class User < ActiveRecord::Base
has_many :user_preferences, :dependent => :destroy
has_many :tag_followings, :dependent => :destroy
has_many :followed_tags, :through => :tag_followings, :source => :tag, :order => 'tags.name'
has_many :tag_exclusions, :dependent => :destroy
has_many :tags_that_exclude, :through => :tag_exclusions, :source => :tag, :order => 'tags.name'
has_many :blocks

has_many :authorizations, :class_name => 'OAuth2::Provider::Models::ActiveRecord::Authorization', :foreign_key => :resource_owner_id
Expand Down
1 change: 1 addition & 0 deletions app/views/shared/_settings_nav.haml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
%li= link_to_unless_current t('profile'), edit_profile_path
%li= link_to_unless_current t('account'), edit_user_path
%li= link_to_unless_current t('privacy'), privacy_settings_path
%li= link_to_unless_current t('filters'), filters_path
%li= link_to_unless_current t('_services'), services_path
%li= link_to_unless_current t('_applications'), authorizations_path
24 changes: 24 additions & 0 deletions app/views/users/filters.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
- content_for :page_title do
= t('.title')

#section_header
%h2
= t('filters')
= render 'shared/settings_nav'

.span-12.prepend-5.last
%h3
= t('.tag_exclusions')

= form_for 'tag_exclusion', :url => tag_exclusions_path, :html => { :method => :post } do |f|
= f.error_messages
%p
= f.text_field 'name'
= f.submit t('.add'), :class => "button"

- @tag_exclusions.each do |tx|
= link_to "##{tx.tag.name}", tag_path(:name => tx.tag.name)
\-
= link_to t('.delete'), tag_exclusion_path(tx), :method => :delete
%br

15 changes: 15 additions & 0 deletions config/locales/diaspora/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ en:
account: "Account"
privacy: "Privacy"
privacy_policy: "Privacy Policy"
filters: 'Filters'
terms_and_conditions: "Terms and Conditions"
_services: "Services"
_applications: "Applications"
Expand Down Expand Up @@ -862,6 +863,14 @@ en:
destroy:
success: "Alas! You aren't following #%{name} anymore."
failure: "Failed to stop following #%{name}. Maybe you already stopped following it?"
tag_exclusions:
create:
success: "You will no longer see posts tagged #%{name}."
failure: "Failed to filter by #%{name}. Are you already filtering by it?"
none: "Please specify a hashtag to filter out."
destroy:
success: "Posts tagged #%{name} are no longer hidden from you."
failure: "Failed to remove filter on #%{name}. Maybe you already removed it?"

tokens:
show:
Expand Down Expand Up @@ -966,6 +975,12 @@ en:
ignored_users: "Ignored Users"
stop_ignoring: "Stop ignoring"

filters:
title: 'Filters'
tag_exclusions: 'Tags to Filter Out'
add: 'Add Filter'
delete: 'Delete'

destroy:
success: "Your account has been locked. It may take 20 minutes for us to finish closing your account. Thank you for trying Diaspora."
no_password: "Please enter your current password to close your account."
Expand Down
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
get "tag_followings" => "tag_followings#index", :as => 'tag_followings'
resources :mentions, :only => [:index]
resources "tag_followings", :only => [:create]
resources 'tag_exclusions', :only => [:create, :destroy]

get 'comment_stream' => 'comment_stream#index', :as => 'comment_stream'

Expand All @@ -85,6 +86,7 @@
get 'public/:username' => :public, :as => 'users_public'
match 'getting_started' => :getting_started, :as => 'getting_started'
match 'privacy' => :privacy_settings, :as => 'privacy_settings'
get 'filters' => :filters, :as => 'filters'
get 'getting_started_completed' => :getting_started_completed
get 'confirm_email/:token' => :confirm_email, :as => 'confirm_email'
end
Expand Down
23 changes: 23 additions & 0 deletions db/migrate/20111215041907_create_tag_exclusions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class CreateTagExclusions < ActiveRecord::Migration

def self.up
create_table :tag_exclusions do |t|
t.integer :user_id, :null => false
t.integer :tag_id, :null => false
t.timestamps
end

add_index :tag_exclusions, :tag_id
add_index :tag_exclusions, :user_id
add_index :tag_exclusions, [:tag_id, :user_id], :unique => true
end

def self.down
remove_index :tag_exclusions, :column => [:tag_id, :user_id]
remove_index :tag_exclusions, :column => :user_id
remove_index :tag_exclusions, :column => :tag_id

drop_table :tag_exclusions
end

end
13 changes: 12 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20111112033021) do
ActiveRecord::Schema.define(:version => 20111215041907) do

create_table "aspect_memberships", :force => true do |t|
t.integer "aspect_id", :null => false
Expand Down Expand Up @@ -392,6 +392,17 @@
t.datetime "updated_at"
end

create_table "tag_exclusions", :force => true do |t|
t.integer "user_id", :null => false
t.integer "tag_id", :null => false
t.datetime "created_at"
t.datetime "updated_at"
end

add_index "tag_exclusions", ["tag_id", "user_id"], :name => "index_tag_exclusions_on_tag_id_and_user_id", :unique => true
add_index "tag_exclusions", ["tag_id"], :name => "index_tag_exclusions_on_tag_id"
add_index "tag_exclusions", ["user_id"], :name => "index_tag_exclusions_on_user_id"

create_table "tag_followings", :force => true do |t|
t.integer "tag_id", :null => false
t.integer "user_id", :null => false
Expand Down
16 changes: 13 additions & 3 deletions lib/stream/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,17 @@ def posts

# @return [ActiveRecord::Relation<Post>]
def stream_posts
self.posts.for_a_stream(max_time, order, self.user)
self.posts.for_a_stream(
max_time, order, self.user
).reject { |p|
# No need to check tag filters if there is no user signed in
# Reshares and Photos don't have tags
if user && p.respond_to?(:tag_strings)
(
user.tags_that_exclude.map { |t| t.name } & p.tag_strings
).any?
end
}
end

# @return [ActiveRecord::Association<Person>] AR association of people within stream's given aspects
Expand Down Expand Up @@ -84,7 +94,7 @@ def for_all_aspects?
true
end

#NOTE: MBS bad bad methods the fact we need these means our views are foobared. please kill them and make them
#NOTE: MBS bad bad methods the fact we need these means our views are foobared. please kill them and make them
#private methods on the streams that need them
def aspects
user.aspects
Expand All @@ -96,7 +106,7 @@ def aspect
end

def aspect_ids
aspects.map{|x| x.id}
aspects.map{|x| x.id}
end

def max_time=(time_string)
Expand Down
8 changes: 8 additions & 0 deletions public/stylesheets/sass/application.sass
Original file line number Diff line number Diff line change
Expand Up @@ -3531,3 +3531,11 @@ a.toggle_selector
#view_profile
:margin
:bottom 1em

#tag_exclusion_name
:width 150px
:display inline
:margin-right 5px

#tag_exclusion_submit
:margin-bottom 15px
16 changes: 16 additions & 0 deletions spec/models/tag_exclusion_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require 'spec_helper'

describe TagExclusion do
before do
@tag = ActsAsTaggableOn::Tag.create(:name => "partytimeexcellent")
TagExclusion.create!(:tag => @tag, :user => alice)
end

it 'validates uniqueness of tag_exclusion scoped through user' do
TagExclusion.new(:tag => @tag, :user => alice).valid?.should be_false
end

it 'allows multiple tag exclusions for different users' do
TagExclusion.new(:tag => @tag, :user => bob).valid?.should be_true
end
end

0 comments on commit 884be13

Please sign in to comment.