Commit 6c03d40f6716e39a90f8461a5504951c198ed55e

Authored by Tom Cocca
1 parent fe0b6335

minor cleanup

Showing 2 changed files with 140 additions and 145 deletions   Show diff stats
lib/acts_as_followable.rb
1 -require File.dirname(__FILE__) + '/follower_lib'  
2 -  
3 -module ActiveRecord #:nodoc:  
4 - module Acts #:nodoc:  
5 - module Followable  
6 -  
7 - def self.included(base)  
8 - base.extend ClassMethods  
9 - base.class_eval do  
10 - include FollowerLib  
11 - end  
12 - end  
13 -  
14 - module ClassMethods  
15 - def acts_as_followable  
16 - has_many :follows, :as => :followable, :dependent => :destroy  
17 - include ActiveRecord::Acts::Followable::InstanceMethods  
18 - end  
19 - end  
20 -  
21 -  
22 - # This module contains instance methods  
23 - module InstanceMethods  
24 -  
25 - # Returns the number of followers a record has.  
26 - def followers_count  
27 - self.follows.size  
28 - end  
29 -  
30 - # Returns the following records.  
31 - def followers  
32 - Follow.find(:all, :include => [:follower], :conditions => ["followable_id = ? AND followable_type = ?",  
33 - self.id, parent_class_name(self)]).collect {|f| f.follower }  
34 - end  
35 -  
36 - # Returns true if the current instance is followed by the passed record.  
37 - def followed_by?(follower)  
38 - Follow.find(:first, :conditions => ["followable_id = ? AND followable_type = ? AND follower_id = ? AND follower_type = ?", self.id, parent_class_name(self), follower.id, parent_class_name(follower)]) ? true : false  
39 - end  
40 -  
41 - end  
42 -  
43 - end  
44 - end  
45 -end 1 +require File.dirname(__FILE__) + '/follower_lib'
  2 +
  3 +module ActiveRecord #:nodoc:
  4 + module Acts #:nodoc:
  5 + module Followable
  6 +
  7 + def self.included(base)
  8 + base.extend ClassMethods
  9 + base.class_eval do
  10 + include FollowerLib
  11 + end
  12 + end
  13 +
  14 + module ClassMethods
  15 + def acts_as_followable
  16 + has_many :follows, :as => :followable, :dependent => :destroy
  17 + include ActiveRecord::Acts::Followable::InstanceMethods
  18 + end
  19 + end
  20 +
  21 +
  22 + module InstanceMethods
  23 +
  24 + # Returns the number of followers a record has.
  25 + def followers_count
  26 + self.follows.size
  27 + end
  28 +
  29 + # Returns the following records.
  30 + def followers
  31 + Follow.find(:all, :include => [:follower], :conditions => ["followable_id = ? AND followable_type = ?",
  32 + self.id, parent_class_name(self)]).collect {|f| f.follower }
  33 + end
  34 +
  35 + # Returns true if the current instance is followed by the passed record.
  36 + def followed_by?(follower)
  37 + Follow.find(:first, :conditions => ["followable_id = ? AND followable_type = ? AND follower_id = ? AND follower_type = ?", self.id, parent_class_name(self), follower.id, parent_class_name(follower)]) ? true : false
  38 + end
  39 +
  40 + end
  41 +
  42 + end
  43 + end
  44 +end
lib/acts_as_follower.rb
1 -require File.dirname(__FILE__) + '/follower_lib'  
2 -  
3 -module ActiveRecord #:nodoc:  
4 - module Acts #:nodoc:  
5 - module Follower  
6 -  
7 - def self.included(base)  
8 - base.extend ClassMethods  
9 - base.class_eval do  
10 - include FollowerLib  
11 - end  
12 - end  
13 -  
14 - module ClassMethods  
15 - def acts_as_follower  
16 - has_many :follows, :as => :follower, :dependent => :destroy  
17 - include ActiveRecord::Acts::Follower::InstanceMethods  
18 - end  
19 - end  
20 -  
21 - # This module contains instance methods  
22 - module InstanceMethods  
23 -  
24 - # Returns true if this instance is following the object passed as an argument.  
25 - def following?(followable)  
26 - 0 < Follow.count(:all, :conditions => [  
27 - "follower_id = ? AND follower_type = ? AND followable_id = ? AND followable_type = ?",  
28 - self.id, parent_class_name(self), followable.id, parent_class_name(followable)  
29 - ])  
30 - end  
31 -  
32 - # Returns the number of objects this instance is following.  
33 - def follow_count  
34 - Follow.count(:all, :conditions => ["follower_id = ? AND follower_type = ?", self.id, parent_class_name(self)])  
35 - end  
36 -  
37 - # Creates a new follow record for this instance to follow the passed object.  
38 - # Does not allow duplicate records to be created.  
39 - def follow(followable)  
40 - follow = get_follow(followable)  
41 - unless follow  
42 - Follow.create(:followable => followable, :follower => self)  
43 - end  
44 - end  
45 -  
46 - # Deletes the follow record if it exists.  
47 - def stop_following(followable)  
48 - follow = get_follow(followable)  
49 - if follow  
50 - follow.destroy  
51 - end  
52 - end  
53 -  
54 - # TODO: Remove from public API.  
55 - # Returns the follow records related to this instance by type.  
56 - def follows_by_type(followable_type)  
57 - Follow.find(:all, :conditions => ["follower_id = ? AND follower_type = ? AND followable_type = ?", self.id, parent_class_name(self), followable_type])  
58 - end  
59 -  
60 - # TODO: Remove from public API.  
61 - # Returns the follow records related to this instance by type.  
62 - def all_follows  
63 - Follow.find(:all, :include => [:followable], :conditions => ["follower_id = ? AND follower_type = ?", self.id, parent_class_name(self)])  
64 - end  
65 -  
66 - # Returns the actual records which this instance is following.  
67 - def all_following  
68 - all_follows.map { |f| f.followable }  
69 - end  
70 -  
71 - # Returns the actual records of a particular type which this record is following.  
72 - def following_by_type(followable_type)  
73 - #klass = eval(followable_type) # be careful with this.  
74 - #klass.find(:all, :joins => :follows, :conditions => ['follower_id = ? AND follower_type = ?', self.id, parent_class_name(self)])  
75 - follows_by_type(followable_type).map { |f| f.followable }  
76 - end  
77 -  
78 - # Allows magic names on following_by_type  
79 - # e.g. following_users == following_by_type('User')  
80 - def method_missing(m, *args)  
81 - if m.to_s[/following_(.+)/]  
82 - #following_by_type(parent_class_name($1).classify)  
83 - following_by_type($1.singularize.classify)  
84 - else  
85 - super  
86 - end  
87 - end  
88 -  
89 - private  
90 -  
91 - # Returns a follow record for the current instance and followable object.  
92 - def get_follow(followable)  
93 - Follow.find(:first, :conditions => ["follower_id = ? AND follower_type = ? AND followable_id = ? AND followable_type = ?", self.id, parent_class_name(self), followable.id, parent_class_name(followable)])  
94 - end  
95 -  
96 - end  
97 -  
98 - end  
99 - end  
100 -end 1 +require File.dirname(__FILE__) + '/follower_lib'
  2 +
  3 +module ActiveRecord #:nodoc:
  4 + module Acts #:nodoc:
  5 + module Follower
  6 +
  7 + def self.included(base)
  8 + base.extend ClassMethods
  9 + base.class_eval do
  10 + include FollowerLib
  11 + end
  12 + end
  13 +
  14 + module ClassMethods
  15 + def acts_as_follower
  16 + has_many :follows, :as => :follower, :dependent => :destroy
  17 + include ActiveRecord::Acts::Follower::InstanceMethods
  18 + end
  19 + end
  20 +
  21 + module InstanceMethods
  22 +
  23 + # Returns true if this instance is following the object passed as an argument.
  24 + def following?(followable)
  25 + 0 < Follow.count(:all, :conditions => [
  26 + "follower_id = ? AND follower_type = ? AND followable_id = ? AND followable_type = ?",
  27 + self.id, parent_class_name(self), followable.id, parent_class_name(followable)
  28 + ])
  29 + end
  30 +
  31 + # Returns the number of objects this instance is following.
  32 + def follow_count
  33 + Follow.count(:all, :conditions => ["follower_id = ? AND follower_type = ?", self.id, parent_class_name(self)])
  34 + end
  35 +
  36 + # Creates a new follow record for this instance to follow the passed object.
  37 + # Does not allow duplicate records to be created.
  38 + def follow(followable)
  39 + follow = get_follow(followable)
  40 + unless follow
  41 + Follow.create(:followable => followable, :follower => self)
  42 + end
  43 + end
  44 +
  45 + # Deletes the follow record if it exists.
  46 + def stop_following(followable)
  47 + follow = get_follow(followable)
  48 + if follow
  49 + follow.destroy
  50 + end
  51 + end
  52 +
  53 + # TODO: Remove from public API.
  54 + # Returns the follow records related to this instance by type.
  55 + def follows_by_type(followable_type)
  56 + Follow.find(:all, :conditions => ["follower_id = ? AND follower_type = ? AND followable_type = ?", self.id, parent_class_name(self), followable_type])
  57 + end
  58 +
  59 + # TODO: Remove from public API.
  60 + # Returns the follow records related to this instance by type.
  61 + def all_follows
  62 + Follow.find(:all, :include => [:followable], :conditions => ["follower_id = ? AND follower_type = ?", self.id, parent_class_name(self)])
  63 + end
  64 +
  65 + # Returns the actual records which this instance is following.
  66 + def all_following
  67 + all_follows.map { |f| f.followable }
  68 + end
  69 +
  70 + # 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).map { |f| f.followable }
  73 + end
  74 +
  75 + # Allows magic names on following_by_type
  76 + # e.g. following_users == following_by_type('User')
  77 + def method_missing(m, *args)
  78 + if m.to_s[/following_(.+)/]
  79 + following_by_type($1.singularize.classify)
  80 + else
  81 + super
  82 + end
  83 + end
  84 +
  85 + private
  86 +
  87 + # Returns a follow record for the current instance and followable object.
  88 + def get_follow(followable)
  89 + Follow.find(:first, :conditions => ["follower_id = ? AND follower_type = ? AND followable_id = ? AND followable_type = ?", self.id, parent_class_name(self), followable.id, parent_class_name(followable)])
  90 + end
  91 +
  92 + end
  93 +
  94 + end
  95 + end
  96 +end