diff --git a/app/controllers/aspects_controller.rb b/app/controllers/aspects_controller.rb index 7264d87d0bec098ae2457be8550f91c44ee4a26c..c47257a1657f19a86cfd9cb8ad8b25971365b2e4 100644 --- a/app/controllers/aspects_controller.rb +++ b/app/controllers/aspects_controller.rb @@ -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 diff --git a/app/controllers/tag_followings_controller.rb b/app/controllers/tag_followings_controller.rb index d8584ce54a8479483fd0511c59f71a8e27411f7c..f26c064421996fa53925b6aecdb9bd4b7649e2a3 100644 --- a/app/controllers/tag_followings_controller.rb +++ b/app/controllers/tag_followings_controller.rb @@ -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 diff --git a/app/controllers/tags_controller.rb b/app/controllers/tags_controller.rb index d8b31df5f6eef2cb7d8be627eb2c09f46d95a96b..d4e6bdf04b30f2d6ad36324ed73a199f34198e22 100644 --- a/app/controllers/tags_controller.rb +++ b/app/controllers/tags_controller.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index 7450ee427e07a617d520d392d54c26d87d2536bb..d93087837189cde29c55f399b7ca3f43b779f6d9 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 diff --git a/app/views/tags/show.haml b/app/views/tags/show.haml index f164ba59fbb41a7a917b465eeef19d55448ed86b..da714f91bdfae83d7d5952bd34d446dbd164b5a5 100644 --- a/app/views/tags/show.haml +++ b/app/views/tags/show.haml @@ -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]}" diff --git a/config/routes.rb b/config/routes.rb index 8207f9b3d95f62afe7fc2accea6d18303f900289..1dd4938fad5baf4dd462b17690b1266625e843b1 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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' diff --git a/features/follows_tags.feature b/features/follows_tags.feature index de38def379894cc02c040fd0e24d7da2f7250627..f05339b5c4b582ce0a71f04d3468a41fbc6c8741 100644 --- a/features/follows_tags.feature +++ b/features/follows_tags.feature @@ -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" diff --git a/spec/controllers/aspects_controller_spec.rb b/spec/controllers/aspects_controller_spec.rb index 5b51d8880b2e5251feac84c549120033114ec448..8308737383cbacd090ad9a89c4a8cfc197632b9a 100644 --- a/spec/controllers/aspects_controller_spec.rb +++ b/spec/controllers/aspects_controller_spec.rb @@ -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 diff --git a/spec/controllers/tags_controller_spec.rb b/spec/controllers/tags_controller_spec.rb index 192d6f9bc40f7104692b453a38ead89475f05734..0754db63ce90ebcdc7c2c8ff6717ca5b3689768a 100644 --- a/spec/controllers/tags_controller_spec.rb +++ b/spec/controllers/tags_controller_spec.rb @@ -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