Commit 2954a768206156e004b6306ddb697370c75068c9

Authored by James McCarthy
1 parent 36a7807b
Exists in master and in 1 other branch rails_3

Split Follow scopes out into their own module.

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/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
... ...