From 9401afd1ea33499a8f2db3b8eab11a69620a48d3 Mon Sep 17 00:00:00 2001 From: Tom Cocca Date: Sun, 16 Oct 2011 18:07:39 -0400 Subject: [PATCH] default is to use joins for the followers/following by_type methods, optionally have includes, attribution in the readme --- README.rdoc | 18 ++++++++++-------- lib/acts_as_follower/followable.rb | 5 ++++- lib/acts_as_follower/follower.rb | 5 ++++- test/acts_as_follower_test.rb | 2 +- 4 files changed, 19 insertions(+), 11 deletions(-) diff --git a/README.rdoc b/README.rdoc index 9791622..95c2109 100644 --- a/README.rdoc +++ b/README.rdoc @@ -191,14 +191,16 @@ If you have updates or patches or want to contribute I would love to see what yo Thanks to everyone for their interest and time in committing to making this plugin better. -* dougal (Douglas F Shearer) - http://github.com/dougal -* jdg (Jonathan George) - http://github.com/jdg -* m3talsmith (Michael Christenson II) - http://github.com/m3talsmith -* joergbattermann (Jörg Battermann) - http://github.com/joergbattermann -* TomK32 (Thomas R. Koll) - http://github.com/TomK32 -* drcapulet (Alex Coomans) - http://github.com/drcapulet -* jhchabran (Jean Hadrien Chabran) - http://github.com/jhchabran -* arthurgeek (Arthur Zapparoli) - http://github.com/arthurgeek +* dougal (Douglas F Shearer) - https://github.com/dougal +* jdg (Jonathan George) - https://github.com/jdg +* m3talsmith (Michael Christenson II) - https://github.com/m3talsmith +* joergbattermann (Jörg Battermann) - https://github.com/joergbattermann +* TomK32 (Thomas R. Koll) - https://github.com/TomK32 +* drcapulet (Alex Coomans) - https://github.com/drcapulet +* jhchabran (Jean Hadrien Chabran) - https://github.com/jhchabran +* arthurgeek (Arthur Zapparoli) - https://github.com/arthurgeek +* james2m (James McCarthy) - https://github.com/james2m +* peterjm (Peter McCracken) - https://github.com/peterjm Please let me know if I missed you. diff --git a/lib/acts_as_follower/followable.rb b/lib/acts_as_follower/followable.rb index e7e02ba..7582859 100644 --- a/lib/acts_as_follower/followable.rb +++ b/lib/acts_as_follower/followable.rb @@ -23,7 +23,7 @@ def followers_count # Returns the followers by a given type def followers_by_type(follower_type, options={}) follows = follower_type.constantize. - includes(:follows). + joins(:follows). where('follows.blocked' => false, 'follows.followable_id' => self.id, 'follows.followable_type' => parent_class_name(self), @@ -31,6 +31,9 @@ def followers_by_type(follower_type, options={}) if options.has_key?(:limit) follows = follows.limit(options[:limit]) end + if options.has_key?(:includes) + follows = follows.includes(options[:includes]) + end follows end diff --git a/lib/acts_as_follower/follower.rb b/lib/acts_as_follower/follower.rb index 4106ee0..2bef3bb 100644 --- a/lib/acts_as_follower/follower.rb +++ b/lib/acts_as_follower/follower.rb @@ -58,7 +58,7 @@ def all_following(options={}) # Returns the actual records of a particular type which this record is following. def following_by_type(followable_type, options={}) followables = followable_type.constantize. - includes(:followings). + joins(:followings). where('follows.blocked' => false, 'follows.follower_id' => self.id, 'follows.follower_type' => parent_class_name(self), @@ -66,6 +66,9 @@ def following_by_type(followable_type, options={}) if options.has_key?(:limit) followables = followables.limit(options[:limit]) end + if options.has_key?(:includes) + followables = followables.includes(options[:includes]) + end followables end diff --git a/test/acts_as_follower_test.rb b/test/acts_as_follower_test.rb index d3d4a23..01eed6d 100644 --- a/test/acts_as_follower_test.rb +++ b/test/acts_as_follower_test.rb @@ -148,7 +148,7 @@ class ActsAsFollowerTest < ActiveSupport::TestCase should "accept AR options" do @metallica = Factory(:metallica) @sam.follow(@metallica) - assert_equal 1, @sam.following_by_type('Band', :limit => 1).count + assert_equal 1, @sam.following_by_type('Band', :limit => 1).to_a.size end end