diff --git a/app/controllers/tags_controller.rb b/app/controllers/tags_controller.rb index c5a67939332cbd7c5ae3287fbf0cd3d599ee6d51..091e14be856161a154d7184eeb7b69e140e04818 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].downcase}, :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 6fd07c0eff12b1a907bd0d6a290c61166b24d665..8705f185ff7b8e1e6908761b2622190934aebcb7 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/models/tag_following_spec.rb b/spec/models/tag_following_spec.rb index 71163c68014e081fb69bbb77d22406aed8200293..a5dada8ac5e59ef66571de617dab4cf604eb8450 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