Commit 217961415f1645abbb2bbb2cd4e706aa3a7dc325
1 parent
482b4e4f
Exists in
master
and in
3 other branches
Methods follows_by_type, all_follows, all_following, following_by_type from acts…
…_as_follower now accept's AR options (cherry picking from commit 0301b921041b665340df72dc5eb862875223b9d3)
Showing
3 changed files
with
41 additions
and
8 deletions
Show diff stats
lib/acts_as_follower.rb
... | ... | @@ -53,23 +53,32 @@ module ActiveRecord #:nodoc: |
53 | 53 | end |
54 | 54 | |
55 | 55 | # Returns the follow records related to this instance by type. |
56 | - def follows_by_type(followable_type) | |
57 | - Follow.unblocked.find(:all, :include => [:followable], :conditions => ["follower_id = ? AND follower_type = ? AND followable_type = ?", self.id, parent_class_name(self), followable_type]) | |
56 | + def follows_by_type(followable_type, options={}) | |
57 | + options = { | |
58 | + :include => [:followable], | |
59 | + :conditions => ["follower_id = ? AND follower_type = ? AND followable_type = ?", self.id, parent_class_name(self), followable_type] | |
60 | + }.merge(options) | |
61 | + | |
62 | + Follow.unblocked.find(:all, options) | |
58 | 63 | end |
59 | 64 | |
60 | 65 | # Returns the follow records related to this instance with the followable included. |
61 | - def all_follows | |
62 | - self.follows.unblocked.all(:include => :followable) | |
66 | + def all_follows(options={}) | |
67 | + options = { | |
68 | + :include => :followable | |
69 | + }.merge(options) | |
70 | + | |
71 | + self.follows.unblocked.all(options) | |
63 | 72 | end |
64 | 73 | |
65 | 74 | # Returns the actual records which this instance is following. |
66 | - def all_following | |
67 | - all_follows.collect{ |f| f.followable } | |
75 | + def all_following(options={}) | |
76 | + all_follows(options).collect{ |f| f.followable } | |
68 | 77 | end |
69 | 78 | |
70 | 79 | # Returns the actual records of a particular type which this record is following. |
71 | - def following_by_type(followable_type) | |
72 | - follows_by_type(followable_type).collect{ |f| f.followable } | |
80 | + def following_by_type(followable_type, options={}) | |
81 | + follows_by_type(followable_type, options).collect{ |f| f.followable } | |
73 | 82 | end |
74 | 83 | |
75 | 84 | # Allows magic names on following_by_type | ... | ... |
test/acts_as_follower_test.rb
... | ... | @@ -92,6 +92,12 @@ class ActsAsFollowerTest < Test::Unit::TestCase |
92 | 92 | assert_equal [@band_follow], @sam.follows_by_type('Band') |
93 | 93 | assert_equal [@user_follow], @sam.follows_by_type('User') |
94 | 94 | end |
95 | + | |
96 | + should "accept AR options" do | |
97 | + @metallica = Factory(:metallica) | |
98 | + @sam.follow(@metallica) | |
99 | + assert_equal 1, @sam.follows_by_type('Band', :limit => 1).count | |
100 | + end | |
95 | 101 | end |
96 | 102 | |
97 | 103 | context "all_follows" do |
... | ... | @@ -101,6 +107,10 @@ class ActsAsFollowerTest < Test::Unit::TestCase |
101 | 107 | assert @sam.all_follows.include?(@user_follow) |
102 | 108 | assert_equal [], @jon.all_follows |
103 | 109 | end |
110 | + | |
111 | + should "accept AR options" do | |
112 | + assert_equal 1, @sam.all_follows(:limit => 1).count | |
113 | + end | |
104 | 114 | end |
105 | 115 | end |
106 | 116 | |
... | ... | @@ -111,6 +121,10 @@ class ActsAsFollowerTest < Test::Unit::TestCase |
111 | 121 | assert @sam.all_following.include?(@jon) |
112 | 122 | assert_equal [], @jon.all_following |
113 | 123 | end |
124 | + | |
125 | + should "accept AR options" do | |
126 | + assert_equal 1, @sam.all_following(:limit => 1).count | |
127 | + end | |
114 | 128 | end |
115 | 129 | |
116 | 130 | context "following_by_type" do |
... | ... | @@ -118,6 +132,12 @@ class ActsAsFollowerTest < Test::Unit::TestCase |
118 | 132 | assert_equal [@oasis], @sam.following_by_type('Band') |
119 | 133 | assert_equal [@jon], @sam.following_by_type('User') |
120 | 134 | end |
135 | + | |
136 | + should "accept AR options" do | |
137 | + @metallica = Factory(:metallica) | |
138 | + @sam.follow(@metallica) | |
139 | + assert_equal 1, @sam.following_by_type('Band', :limit => 1).count | |
140 | + end | |
121 | 141 | end |
122 | 142 | |
123 | 143 | context "method_missing" do | ... | ... |