diff --git a/acts_as_follower.gemspec b/acts_as_follower.gemspec index 7975494..3deae4e 100644 --- a/acts_as_follower.gemspec +++ b/acts_as_follower.gemspec @@ -19,7 +19,7 @@ Gem::Specification.new do |s| s.require_paths = ["lib"] s.add_development_dependency "sqlite3" - s.add_development_dependency "shoulda" - s.add_development_dependency "factory_girl" + s.add_development_dependency "shoulda", "2.11.3" + s.add_development_dependency "factory_girl", "2.0.5" s.add_development_dependency "rails", "~>3.0.10" end diff --git a/lib/acts_as_follower/followable.rb b/lib/acts_as_follower/followable.rb index 7582859..f183d73 100644 --- a/lib/acts_as_follower/followable.rb +++ b/lib/acts_as_follower/followable.rb @@ -10,6 +10,13 @@ def acts_as_followable has_many :followings, :as => :followable, :dependent => :destroy, :class_name => 'Follow' include ActsAsFollower::Followable::InstanceMethods include ActsAsFollower::FollowerLib + extend ActsAsFollower::Followable::SingletonMethods + end + end + + module SingletonMethods + def unfollowed + self.where("NOT EXISTS (SELECT 1 FROM follows WHERE followable_id = #{self.table_name}.id AND followable_type = '#{self.model_name}')") end end @@ -25,8 +32,8 @@ def followers_by_type(follower_type, options={}) follows = follower_type.constantize. joins(:follows). where('follows.blocked' => false, - 'follows.followable_id' => self.id, - 'follows.followable_type' => parent_class_name(self), + 'follows.followable_id' => self.id, + 'follows.followable_type' => parent_class_name(self), 'follows.follower_type' => follower_type) if options.has_key?(:limit) follows = follows.limit(options[:limit]) diff --git a/test/acts_as_followable_test.rb b/test/acts_as_followable_test.rb index 8b9364d..8f654b6 100644 --- a/test/acts_as_followable_test.rb +++ b/test/acts_as_followable_test.rb @@ -2,6 +2,21 @@ class ActsAsFollowableTest < ActiveSupport::TestCase + context "class methods" do + context "unfollowed" do + setup do + @sam = Factory(:sam) + @oasis = Factory(:oasis) + @metallica = Factory(:metallica) + @sam.follow(@oasis) + end + + should "return unfollowed bands" do + assert_equal 1, Band.unfollowed.count + end + end + end + context "instance methods" do setup do @sam = Factory(:sam)