lib/mongoid_follow/followee.rb in mongoid_follow-0.2.1 vs lib/mongoid_follow/followee.rb in mongoid_follow-0.2.2
- old
+ new
@@ -8,49 +8,80 @@
end
# know if self is followed by model
#
# Example:
- # >> @clyde.follower?(@bonnie)
+ # => @clyde.follower?(@bonnie)
# => true
def follower?(model)
0 < self.followers.find(:all, conditions: {ff_id: model.id}).limit(1).count
end
- # get followees count
+ # get followers count
+ # Note: this is a cache counter
#
# Example:
- # >> @bonnie.followees_count
+ # => @bonnie.followers_count
# => 1
def followers_count
self.fferc
end
+ # get followers count by model
+ #
+ # Example:
+ # => @bonnie.followers_count_by_model(User)
+ # => 1
+ def followers_count_by_model(model)
+ self.followers.where(:ff_type => model.to_s).count
+ end
+
# view all selfs followers
#
# Example:
- # >> @clyde.all_followers
+ # => @clyde.all_followers
# => [@bonnie, @alec]
def all_followers
get_followers_of(self)
end
+ # view all selfs followers by model
+ #
+ # Example:
+ # => @clyde.all_followers_by_model
+ # => [@bonnie]
+ def all_followers_by_model(model)
+ get_followers_of(self, model)
+ end
+
# view all common followers of self against model
#
# Example:
- # >> @clyde.common_followers_with(@gang)
+ # => @clyde.common_followers_with(@gang)
# => [@bonnie, @alec]
def common_followers_with(model)
model_followers = get_followers_of(model)
self_followers = get_followers_of(self)
self_followers & model_followers
end
private
- def get_followers_of(me)
- me.followers.collect do |f|
+ def get_followers_of(me, model = nil)
+ followers = !model ? me.followers : me.followers.where(:ff_type => model.to_s)
+
+ followers.collect do |f|
f.ff_type.constantize.find(f.ff_id)
+ end
+ end
+
+ def method_missing(missing_method, *args, &block)
+ if missing_method.to_s =~ /^(.+)_followers_count$/
+ followers_count_by_model($1.camelize)
+ elsif missing_method.to_s =~ /^all_(.+)_followers$/
+ all_followers_by_model($1.camelize)
+ else
+ super
end
end
end
end