lib/partisan/follow.rb in partisan-0.3 vs lib/partisan/follow.rb in partisan-0.4

- old
+ new

@@ -1,32 +1,34 @@ module Partisan - class Follow < ActiveRecord::Base - self.table_name = 'follows' + module Follow + extend ActiveSupport::Concern - # Validations - validates :followable, presence: true - validates :follower, presence: true - - # Associations - belongs_to :followable, polymorphic: true - belongs_to :follower, polymorphic: true - - # Callbacks - after_create :update_follow_counter - after_destroy :update_follow_counter - - around_create :around_create_follower - around_create :around_create_followable - around_destroy :around_destroy_follower - around_destroy :around_destroy_followable - # Constants FOLLOWER_FOLLOW_ACCESSORS = [:about_to_follow, :just_followed] FOLLOWER_UNFOLLOW_ACCESSORS = [:about_to_unfollow, :just_unfollowed] FOLLOWABLE_BEING_FOLLOWED_ACCESSORS = [:about_to_be_followed_by, :just_followed_by] FOLLOWABLE_BEING_UNFOLLOWED_ACCESSORS = [:about_to_be_unfollowed_by, :just_unfollowed_by] + included do + # Validations + validates :followable, presence: true + validates :follower, presence: true + + # Associations + belongs_to :followable, polymorphic: true + belongs_to :follower, polymorphic: true + + # Callbacks + after_create :update_follow_counter + after_destroy :update_follow_counter + + around_create :around_create_follower + around_create :around_create_followable + around_destroy :around_destroy_follower + around_destroy :around_destroy_followable + end + protected def update_follow_counter follower.update_follower_counter followable.update_followable_counter @@ -46,14 +48,10 @@ def around_destroy_followable(&blk) execute_callback :followable, :being_unfollowed, &blk end - def self.accessors_for_follow_callback(association, callback) - const_get "#{association}_#{callback}_accessors".upcase - end - private def execute_callback(association, callback, &blk) # Fetch our associated objects object = send(association) @@ -68,8 +66,14 @@ # Reset each accessor value accessors.map { |accessor| object.send "#{accessor}=", nil } true + end + + module ClassMethods + def accessors_for_follow_callback(association, callback) + const_get "#{association}_#{callback}_accessors".upcase + end end end end