Sha256: 57e600339b341e4d6c033fe70a69e35e6526bf7feb066729c35368236dc323ea

Contents?: true

Size: 1.74 KB

Versions: 2

Compression:

Stored size: 1.74 KB

Contents

module Partisan
  class Follow < ActiveRecord::Base
    self.table_name = 'follows'

    # 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

    # Follower's :follow callbacks
    around_create do |follow, blk|
      self.follower.about_to_follow = self.follower.just_followed = self.followable
      self.follower.run_callbacks :follow, &blk
      self.follower.about_to_follow = self.follower.just_followed = nil
    end

    # Followable's :follow callbacks
    around_create do |follow, blk|
      self.followable.about_to_be_followed_by = self.followable.just_followed_by = self.follower
      self.followable.run_callbacks :being_followed, &blk
      self.followable.about_to_be_followed_by = self.followable.just_followed_by = nil
    end

    # Follower's :unfollow callbacks
    around_destroy do |follow, blk|
      self.follower.about_to_unfollow = self.follower.just_unfollowed = self.followable
      self.follower.run_callbacks :unfollow, &blk
      self.follower.about_to_unfollow = self.follower.just_unfollowed = nil
    end

    # Followable's :unfollow callbacks
    around_destroy do |follow, blk|
      self.followable.about_to_be_unfollowed_by = self.followable.just_unfollowed_by = self.follower
      self.followable.run_callbacks :being_unfollowed, &blk
      self.followable.about_to_be_unfollowed_by = self.followable.just_unfollowed_by = nil
    end

  protected

    def update_follow_counter
      self.follower.update_follower_counter
      self.followable.update_followable_counter
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
partisan-0.2.3 lib/partisan/follow.rb
partisan-0.2.2 lib/partisan/follow.rb