diff --git a/app/controllers/tags_controller.rb b/app/controllers/tags_controller.rb index 8128c03d2..091e14be8 100644 --- a/app/controllers/tags_controller.rb +++ b/app/controllers/tags_controller.rb @@ -40,10 +40,7 @@ class TagsController < ApplicationController end def tag_followed? - if @tag_followed.nil? - @tag_followed = TagFollowing.joins(:tag).where(:tags => {:name => params[:name]}, :user_id => current_user.id).exists? - end - @tag_followed + TagFollowing.user_is_following?(current_user, params[:name]) end def prep_tags_for_javascript diff --git a/app/models/tag_following.rb b/app/models/tag_following.rb index 6fd07c0ef..8705f185f 100644 --- a/app/models/tag_following.rb +++ b/app/models/tag_following.rb @@ -3,4 +3,9 @@ class TagFollowing < ActiveRecord::Base belongs_to :tag, :class_name => "ActsAsTaggableOn::Tag" validates_uniqueness_of :tag_id, :scope => :user_id + + def self.user_is_following?(user, tagname) + tagname.nil? ? false : joins(:tag).where(:tags => {:name => tagname.downcase}, :user_id => user.id).exists? + end + end diff --git a/spec/controllers/tags_controller_spec.rb b/spec/controllers/tags_controller_spec.rb index fcce6b312..b08a8602d 100644 --- a/spec/controllers/tags_controller_spec.rb +++ b/spec/controllers/tags_controller_spec.rb @@ -76,10 +76,10 @@ describe TagsController do sign_in bob @tag = ActsAsTaggableOn::Tag.create!(:name => "partytimeexcellent") @controller.stub(:current_user).and_return(bob) - @controller.stub(:params).and_return({:name => "partytimeexcellent"}) + @controller.stub(:params).and_return({:name => "PARTYTIMEexcellent"}) end - it 'returns true if the following already exists' do + it 'returns true if the following already exists and should be case insensitive' do TagFollowing.create!(:tag => @tag, :user => bob ) @controller.tag_followed?.should be_true end diff --git a/spec/models/tag_following_spec.rb b/spec/models/tag_following_spec.rb index 71163c680..a5dada8ac 100644 --- a/spec/models/tag_following_spec.rb +++ b/spec/models/tag_following_spec.rb @@ -13,4 +13,13 @@ describe TagFollowing do it 'allows multiple tag followings for different users' do TagFollowing.new(:tag => @tag, :user => bob).valid?.should be_true end + + it 'user is following a tag' do + TagFollowing.user_is_following?(alice, @tag.name).should be_true + end + + it 'user not following a tag' do + TagFollowing.user_is_following?(bob, @tag.name).should be_false + end + end