Skip to content

Commit

Permalink
Split Follow scopes out into their own module.
Browse files Browse the repository at this point in the history
  • Loading branch information
james2m committed Sep 22, 2011
1 parent 36a7807 commit 2954a76
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 9 deletions.
1 change: 1 addition & 0 deletions lib/acts_as_follower.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module ActsAsFollower
autoload :Follower, 'acts_as_follower/follower'
autoload :Followable, 'acts_as_follower/followable'
autoload :FollowerLib, 'acts_as_follower/follower_lib'
autoload :FollowScopes, 'acts_as_follower/follow_scopes'

require 'acts_as_follower/railtie' if defined?(Rails) && Rails::VERSION::MAJOR >= 3
end
Expand Down
39 changes: 39 additions & 0 deletions lib/acts_as_follower/follow_scopes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
module ActsAsFollower #:nodoc:
module FollowScopes

# Scopes
def for_follower(follower)
where(:follower_id => follower.id, :follower_type => parent_class_name(follower))
end

def for_followable(followable)
where(:followable_id => followable.id, :followable_type => parent_class_name(followable))
end

def for_follower_type(follower_type)
where(:follower_type => follower_type)
end

def for_followable_type(followable_type)
where(:followable_type => followable_type)
end

def recent(from)
where(["created_at > ?", (from || 2.weeks.ago).to_s(:db)])
end

def descending
order("follows.created_at DESC")
end

def unblocked
where(:blocked => false)
end

def blocked
where(:blocked => true)
end
# end Scopes

end
end
11 changes: 2 additions & 9 deletions lib/generators/templates/model.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
class Follow < ActiveRecord::Base

extend ActsAsFollower::FollowerLib

scope :for_follower, lambda { |follower| where(["follower_id = ? AND follower_type = ?", follower.id, parent_class_name(follower)]) }
scope :for_followable, lambda { |followable| where(["followable_id = ? AND followable_type = ?", followable.id, parent_class_name(followable)]) }
scope :for_follower_type, lambda { |follower_type| where("follower_type = ?", follower_type) }
scope :for_followable_type, lambda { |followable_type| where("followable_type = ?", followable_type) }
scope :recent, lambda { |from| where(["created_at > ?", (from || 2.weeks.ago).to_s(:db)]) }
scope :descending, order("follows.created_at DESC")
scope :unblocked, where(:blocked => false)
scope :blocked, where(:blocked => true)
extend ActsAsFollower::FollowScopes

# NOTE: Follows belong to the "followable" interface, and also to followers
belongs_to :followable, :polymorphic => true
Expand Down

0 comments on commit 2954a76

Please sign in to comment.