Commit 94c073282873d4403b070e2157685c1f979069b0

Authored by Tom Cocca
2 parents 165137a2 2954a768
Exists in master and in 1 other branch rails_3

Merge pull request #15 from james2m/master

A few minor tweaks to Follower and Followable.
lib/acts_as_follower.rb
... ... @@ -4,6 +4,7 @@ module ActsAsFollower
4 4 autoload :Follower, 'acts_as_follower/follower'
5 5 autoload :Followable, 'acts_as_follower/followable'
6 6 autoload :FollowerLib, 'acts_as_follower/follower_lib'
  7 + autoload :FollowScopes, 'acts_as_follower/follow_scopes'
7 8  
8 9 require 'acts_as_follower/railtie' if defined?(Rails) && Rails::VERSION::MAJOR >= 3
9 10 end
... ...
lib/acts_as_follower/follow_scopes.rb 0 → 100644
... ... @@ -0,0 +1,39 @@
  1 +module ActsAsFollower #:nodoc:
  2 + module FollowScopes
  3 +
  4 + # Scopes
  5 + def for_follower(follower)
  6 + where(:follower_id => follower.id, :follower_type => parent_class_name(follower))
  7 + end
  8 +
  9 + def for_followable(followable)
  10 + where(:followable_id => followable.id, :followable_type => parent_class_name(followable))
  11 + end
  12 +
  13 + def for_follower_type(follower_type)
  14 + where(:follower_type => follower_type)
  15 + end
  16 +
  17 + def for_followable_type(followable_type)
  18 + where(:followable_type => followable_type)
  19 + end
  20 +
  21 + def recent(from)
  22 + where(["created_at > ?", (from || 2.weeks.ago).to_s(:db)])
  23 + end
  24 +
  25 + def descending
  26 + order("follows.created_at DESC")
  27 + end
  28 +
  29 + def unblocked
  30 + where(:blocked => false)
  31 + end
  32 +
  33 + def blocked
  34 + where(:blocked => true)
  35 + end
  36 + # end Scopes
  37 +
  38 + end
  39 +end
... ...
lib/acts_as_follower/followable.rb
... ... @@ -70,8 +70,7 @@ module ActsAsFollower #:nodoc:
70 70 # Returns true if the current instance is followed by the passed record
71 71 # Returns false if the current instance is blocked by the passed record or no follow is found
72 72 def followed_by?(follower)
73   - f = get_follow_for(follower)
74   - (f && !f.blocked?) ? true : false
  73 + self.followings.unblocked.for_follower(follower).exists?
75 74 end
76 75  
77 76 def block(follower)
... ... @@ -85,7 +84,7 @@ module ActsAsFollower #:nodoc:
85 84 private
86 85  
87 86 def get_follow_for(follower)
88   - Follow.for_followable(self).for_follower(follower).first
  87 + self.followings.for_follower(follower).first
89 88 end
90 89  
91 90 def block_future_follow(follower)
... ...
lib/acts_as_follower/follower.rb
... ... @@ -28,9 +28,8 @@ module ActsAsFollower #:nodoc:
28 28 # Creates a new follow record for this instance to follow the passed object.
29 29 # Does not allow duplicate records to be created.
30 30 def follow(followable)
31   - follow = get_follow(followable)
32   - if follow.blank? && self != followable
33   - Follow.create(:followable => followable, :follower => self)
  31 + if self != followable
  32 + self.follows.find_or_create_by_followable_id_and_followable_type(followable.id, parent_class_name(followable))
34 33 end
35 34 end
36 35  
... ... @@ -43,7 +42,7 @@ module ActsAsFollower #:nodoc:
43 42  
44 43 # Returns the follow records related to this instance by type.
45 44 def follows_by_type(followable_type, options={})
46   - Follow.unblocked.includes(:followable).for_follower(self).for_followable_type(followable_type).find(:all, options)
  45 + self.follows.unblocked.includes(:followable).for_followable_type(followable_type).all(options)
47 46 end
48 47  
49 48 # Returns the follow records related to this instance with the followable included.
... ... @@ -58,21 +57,20 @@ module ActsAsFollower #:nodoc:
58 57  
59 58 # Returns the actual records of a particular type which this record is following.
60 59 def following_by_type(followable_type, options={})
61   - follows = followable_type.constantize.
  60 + followables = followable_type.constantize.
62 61 includes(:followings).
63   - where('blocked = ?', false).
64   - where(
65   - "follows.follower_id = ? AND follows.follower_type = ? AND follows.followable_type = ?",
66   - self.id, parent_class_name(self), followable_type
67   - )
  62 + where('follows.blocked' => false,
  63 + 'follows.follower_id' => self.id,
  64 + 'follows.follower_type' => parent_class_name(self),
  65 + 'follows.followable_type' => followable_type)
68 66 if options.has_key?(:limit)
69   - follows = follows.limit(options[:limit])
  67 + followables = followables.limit(options[:limit])
70 68 end
71   - follows
  69 + followables
72 70 end
73 71  
74 72 def following_by_type_count(followable_type)
75   - Follow.unblocked.for_follower(self).for_followable_type(followable_type).count
  73 + follows.unblocked.for_followable_type(followable_type).count
76 74 end
77 75  
78 76 # Allows magic names on following_by_type
... ... @@ -91,7 +89,7 @@ module ActsAsFollower #:nodoc:
91 89  
92 90 # Returns a follow record for the current instance and followable object.
93 91 def get_follow(followable)
94   - Follow.unblocked.for_follower(self).for_followable(followable).first
  92 + self.follows.unblocked.for_followable(followable).first
95 93 end
96 94  
97 95 end
... ...
lib/generators/templates/model.rb
1 1 class Follow < ActiveRecord::Base
  2 +
2 3 extend ActsAsFollower::FollowerLib
3   -
4   - scope :for_follower, lambda { |follower| where(["follower_id = ? AND follower_type = ?", follower.id, parent_class_name(follower)]) }
5   - scope :for_followable, lambda { |followable| where(["followable_id = ? AND followable_type = ?", followable.id, parent_class_name(followable)]) }
6   - scope :for_follower_type, lambda { |follower_type| where("follower_type = ?", follower_type) }
7   - scope :for_followable_type, lambda { |followable_type| where("followable_type = ?", followable_type) }
8   - scope :recent, lambda { |from| where(["created_at > ?", (from || 2.weeks.ago).to_s(:db)]) }
9   - scope :descending, order("follows.created_at DESC")
10   - scope :unblocked, where(:blocked => false)
11   - scope :blocked, where(:blocked => true)
  4 + extend ActsAsFollower::FollowScopes
12 5  
13 6 # NOTE: Follows belong to the "followable" interface, and also to followers
14 7 belongs_to :followable, :polymorphic => true
... ...