From 92b29d31dbc2fa3b91da10d45093dfa7cadf0d9c Mon Sep 17 00:00:00 2001 From: Diaspora Europe <info@diasp.eu> Date: Thu, 9 Feb 2012 14:16:57 +0100 Subject: [PATCH] added user_is_following method, added 2 model specs --- app/controllers/tags_controller.rb | 5 +---- app/models/tag_following.rb | 5 +++++ spec/models/tag_following_spec.rb | 9 +++++++++ 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/app/controllers/tags_controller.rb b/app/controllers/tags_controller.rb index c5a6793933..091e14be85 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 6fd07c0eff..8705f185ff 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 71163c6801..a5dada8ac5 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 -- GitLab