From 2954a768206156e004b6306ddb697370c75068c9 Mon Sep 17 00:00:00 2001 From: James McCarthy Date: Thu, 22 Sep 2011 18:16:53 +0100 Subject: [PATCH] Split Follow scopes out into their own module. --- lib/acts_as_follower.rb | 1 + lib/acts_as_follower/follow_scopes.rb | 39 +++++++++++++++++++++++++++ lib/generators/templates/model.rb | 11 ++------ 3 files changed, 42 insertions(+), 9 deletions(-) create mode 100644 lib/acts_as_follower/follow_scopes.rb diff --git a/lib/acts_as_follower.rb b/lib/acts_as_follower.rb index 3a9cdc1..94e0bae 100644 --- a/lib/acts_as_follower.rb +++ b/lib/acts_as_follower.rb @@ -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 diff --git a/lib/acts_as_follower/follow_scopes.rb b/lib/acts_as_follower/follow_scopes.rb new file mode 100644 index 0000000..ae3ddbd --- /dev/null +++ b/lib/acts_as_follower/follow_scopes.rb @@ -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 diff --git a/lib/generators/templates/model.rb b/lib/generators/templates/model.rb index dd5e4a4..7378734 100644 --- a/lib/generators/templates/model.rb +++ b/lib/generators/templates/model.rb @@ -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