a user can now follow and unfollow a tag
This commit is contained in:
parent
632eacc51f
commit
f0d9d318e8
9 changed files with 35 additions and 77 deletions
|
|
@ -175,11 +175,7 @@ class AspectsController < ApplicationController
|
|||
end
|
||||
|
||||
def tags
|
||||
if tag_followings != [] && @tags != []
|
||||
@tags ||= ActsAsTaggableOn::Tag.where(:id => tag_followings.map(&:id)).all
|
||||
else
|
||||
@tags ||= []
|
||||
end
|
||||
@tags ||= current_user.followed_tags
|
||||
end
|
||||
|
||||
private
|
||||
|
|
|
|||
|
|
@ -5,15 +5,12 @@ class TagFollowingsController < ApplicationController
|
|||
# POST /tag_followings.xml
|
||||
def create
|
||||
@tag = ActsAsTaggableOn::Tag.find_or_create_by_name(params[:name])
|
||||
@tag_following = current_user.tag_followings.new(:tag_id => @tag_id)
|
||||
@tag_following = current_user.tag_followings.new(:tag_id => @tag.id)
|
||||
|
||||
respond_to do |format|
|
||||
if @tag_following.save
|
||||
format.html { redirect_to(tag_path(:name => params[:name]), :notice => "Successfully following: #{params[:name]}" ) }
|
||||
format.xml { render :xml => @tag_following, :status => :created, :location => @tag_following }
|
||||
else
|
||||
render :nothing => true, :status => :unprocessable_entity
|
||||
end
|
||||
if @tag_following.save
|
||||
redirect_to(tag_path(:name => params[:name]), :notice => "Successfully following: #{params[:name]}" )
|
||||
else
|
||||
render :nothing => true, :status => 406
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -23,7 +20,7 @@ class TagFollowingsController < ApplicationController
|
|||
@tag = ActsAsTaggableOn::Tag.find_by_name(params[:name])
|
||||
@tag_following = current_user.tag_followings.where(:tag_id => @tag.id).first
|
||||
if @tag_following && @tag_following.destroy
|
||||
render :nothing => true, :status => 200
|
||||
redirect_to(tag_path(:name => params[:name]), :notice => "Successfully stopped following: #{params[:name]}" )
|
||||
else
|
||||
render :nothing => true, :status => 410
|
||||
end
|
||||
|
|
|
|||
|
|
@ -8,6 +8,8 @@ class TagsController < ApplicationController
|
|||
skip_before_filter :set_grammatical_gender
|
||||
before_filter :ensure_page, :only => :show
|
||||
|
||||
helper_method :tag_followed?
|
||||
|
||||
respond_to :html, :only => [:show]
|
||||
respond_to :json, :only => [:index]
|
||||
|
||||
|
|
@ -55,7 +57,6 @@ class TagsController < ApplicationController
|
|||
|
||||
max_time = params[:max_time] ? Time.at(params[:max_time].to_i) : Time.now
|
||||
@posts = @posts.where(StatusMessage.arel_table[:created_at].lt(max_time))
|
||||
|
||||
@posts = @posts.includes(:comments, :photos).order('posts.created_at DESC').limit(15)
|
||||
|
||||
@posts = PostsFake.new(@posts)
|
||||
|
|
@ -70,7 +71,10 @@ class TagsController < ApplicationController
|
|||
end
|
||||
end
|
||||
|
||||
# def tag_following?
|
||||
# TagFollowings.join(:tags)
|
||||
# end
|
||||
def tag_followed?
|
||||
if @tf.nil?
|
||||
@tf = TagFollowing.joins(:tag).where(:tags => {:name => params[:name]}, :user_id => current_user.id).exists? #,
|
||||
end
|
||||
@tf
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -40,6 +40,7 @@ class User < ActiveRecord::Base
|
|||
has_many :services, :dependent => :destroy
|
||||
has_many :user_preferences, :dependent => :destroy
|
||||
has_many :tag_followings, :dependent => :destroy
|
||||
has_many :followed_tags, :through => :tag_followings, :source => :tag
|
||||
|
||||
has_many :authorizations, :class_name => 'OAuth2::Provider::Models::ActiveRecord::Authorization', :foreign_key => :resource_owner_id
|
||||
has_many :applications, :through => :authorizations, :source => :client
|
||||
|
|
|
|||
|
|
@ -30,10 +30,10 @@
|
|||
#author_info
|
||||
- if user_signed_in? && current_user.person != @person
|
||||
.right
|
||||
- unless true #tag_followed?
|
||||
- unless tag_followed?
|
||||
= button_to t('.follow', :tag => params[:name]), tag_tag_followings_path(:name => params[:name]), :method => :post, :class => 'button take_action'
|
||||
- else
|
||||
= button_to t('.stop_following', :tag => params[:name]), tag_tag_followings_path(:name => params[:name]), :method => :post, :class => 'button take_action'
|
||||
= button_to t('.stop_following', :tag => params[:name]), tag_tag_followings_path(:name => params[:name]), :method => :delete, :class => 'button take_action'
|
||||
%h2
|
||||
= "##{params[:name]}"
|
||||
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@ Diaspora::Application.routes.draw do
|
|||
|
||||
resources :tags, :only => [:index]
|
||||
post "/tags/:name/tag_followings" => "tag_followings#create", :as => 'tag_tag_followings'
|
||||
delete " /tags/:name/tag_followings" => "tag_followings#destroy"
|
||||
delete "/tags/:name/tag_followings" => "tag_followings#destroy"
|
||||
|
||||
get 'tags/:name' => 'tags#show', :as => 'tag'
|
||||
|
||||
|
|
|
|||
|
|
@ -17,13 +17,13 @@ Feature: posting
|
|||
And I wait for the ajax to finish
|
||||
And I wait for the ajax to finish
|
||||
|
||||
#the following (3 lines) verbosity should not be needed
|
||||
When I go to the home page
|
||||
Then I should see "I am da #boss"
|
||||
And I wait for 5 seconds
|
||||
|
||||
And I follow "#boss"
|
||||
And I wait for the ajax to finish
|
||||
And I debug
|
||||
Then I should see "I am da #boss"
|
||||
|
||||
|
||||
|
|
@ -39,8 +39,10 @@ Feature: posting
|
|||
And I follow "#boss"
|
||||
Then I should see "I am da #boss"
|
||||
|
||||
Scenario: see that I'm following a particular tag
|
||||
Then I should see "Following #boss"
|
||||
Scenario: can stop following a particular tag
|
||||
Then I should see "Stop Following #boss"
|
||||
When I go to the home page
|
||||
And I should not see "#boss" in ".left_nav"
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -335,61 +335,18 @@ describe AspectsController do
|
|||
context 'helper methods' do
|
||||
before do
|
||||
@tag = ActsAsTaggableOn::Tag.create!(:name => "partytimeexcellent")
|
||||
TagFollowing.create!(:tag => @tag, :user => bob )
|
||||
end
|
||||
|
||||
describe 'tag_followings' do
|
||||
it 'does nothing if no-one is signed in' do
|
||||
@controller.stub!(:current_user).and_return(nil)
|
||||
@controller.tag_followings.should be_nil
|
||||
end
|
||||
|
||||
it 'queries current_users tag_followings' do
|
||||
alice.should_receive(:tag_followings).once.and_return([42])
|
||||
|
||||
@controller.stub(:current_user).and_return(alice)
|
||||
@controller.tag_followings.should == [42]
|
||||
end
|
||||
|
||||
it 'does not query twice' do
|
||||
alice.should_receive(:tag_followings).once.and_return([42])
|
||||
@controller.stub(:current_user).and_return(alice)
|
||||
|
||||
@controller.tag_followings.should == [42]
|
||||
@controller.tag_followings.should == [42]
|
||||
end
|
||||
TagFollowing.create!(:tag => @tag, :user => alice )
|
||||
alice.should_receive(:followed_tags).once.and_return([42])
|
||||
end
|
||||
|
||||
describe 'tags' do
|
||||
it 'does nothing there are no tag_followings' do
|
||||
@controller.stub!(:tag_followings).and_return([])
|
||||
@controller.tags.should == []
|
||||
it 'queries current_users tag if there are tag_followings' do
|
||||
@controller.tags.should == [42]
|
||||
end
|
||||
|
||||
context "querying" do
|
||||
before do
|
||||
@ids = [1,2,3]
|
||||
@tag_followings = @ids.map do |n|
|
||||
tf = mock()
|
||||
tf.should_receive(:id).and_return(n)
|
||||
tf
|
||||
end
|
||||
|
||||
query = mock
|
||||
query.should_receive(:all).and_return([42])
|
||||
|
||||
ActsAsTaggableOn::Tag.should_receive(:where).with(:id => @ids).once.and_return(query)
|
||||
@controller.stub(:tag_followings).and_return(@tag_followings)
|
||||
end
|
||||
|
||||
it 'queries current_users tag if there are tag_followings' do
|
||||
@controller.tags.should == [42]
|
||||
end
|
||||
|
||||
it 'does not query twice' do
|
||||
@controller.tags.should == [42]
|
||||
@controller.tags.should == [42]
|
||||
end
|
||||
it 'does not query twice' do
|
||||
@controller.tags.should == [42]
|
||||
@controller.tags.should == [42]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -110,18 +110,19 @@ describe TagsController do
|
|||
context 'helper methods' do
|
||||
describe 'tag_followed?' do
|
||||
before do
|
||||
sign_in alice
|
||||
sign_in bob
|
||||
@tag = ActsAsTaggableOn::Tag.create!(:name => "partytimeexcellent")
|
||||
@controller.stub(:current_user).and_return(bob)
|
||||
@controller.stub(:params).and_return({:name => "partytimeexcellent"})
|
||||
end
|
||||
|
||||
it 'returns true if the following already exists' do
|
||||
TagFollowing.create!(:tag => @tag, :user => bob )
|
||||
@controller.tag_following?.should be_true
|
||||
@controller.tag_followed?.should be_true
|
||||
end
|
||||
|
||||
it 'returns false if the following does not already exist' do
|
||||
@controller.tag_following?.should be_false
|
||||
@controller.tag_followed?.should be_false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Reference in a new issue