Sha256: d95f66d268142437838cfeed313e3b65ee1588cb42e80a27ae2676fb959e05ad

Contents?: true

Size: 1.23 KB

Versions: 1

Compression:

Stored size: 1.23 KB

Contents

module ActiveRecord
  class Base
    def is_follower?
      false
    end
  end
end

module Socialization
  module Follower
    extend ActiveSupport::Concern

    included do
      # A follow is the Follow record of self following a followable record.
      has_many :follows, :as => :follower, :dependent => :destroy, :class_name => 'Follow'

      def is_follower?
        true
      end

      def follow!(followable)
        raise ArgumentError, "#{followable} is not followable!" unless followable.is_followable?
        raise ArgumentError, "#{self} cannot follow itself!" unless self != followable
        Follow.create!({ :follower => self, :followable => followable }, :without_protection => true)
      end

      def unfollow!(followable)
        ff = followable.followings.where(:follower_type => self.class.to_s, :follower_id => self.id)
        unless ff.empty?
          ff.each { |f| f.destroy }
        else
          raise ActiveRecord::RecordNotFound
        end
      end

      def follows?(followable)
        raise ArgumentError, "#{followable} is not followable!" unless followable.is_followable?
        !self.follows.where(:followable_type => followable.class.to_s, :followable_id => followable.id).empty?
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
socialization-0.3.0 lib/socialization/follower.rb