Sha256: 829173cef22d87d72781daa0a7d8cbbec25996c24d32701563de7da9745e9ac3

Contents?: true

Size: 979 Bytes

Versions: 10

Compression:

Stored size: 979 Bytes

Contents

module FlexibleFeeds
  module ActsAsFollower
    extend ActiveSupport::Concern
 
    module ClassMethods
      def acts_as_follower

        has_many :follows, as: :follower, class_name: "FlexibleFeeds::Follow",
          dependent: :destroy
        has_many :followed_feeds, through: :follows, source: :feed,
          class_name: "FlexibleFeeds::Feed"
        has_many :aggregate_follows, -> { group 'flexible_feeds_events.id' },
          through: :followed_feeds, source: :events,
          class_name: "FlexibleFeeds::Event"

        send :include, InstanceMethods
      end
    end

    module InstanceMethods
      def follow(feed)
        follows.create(feed: feed)
      end

      def unfollow(feed)
        join = follows.find_by(feed: feed)
        return false if join.nil?
        join.destroy
      end

      def is_following?(feed)
        followed_feeds.include?(feed)
      end
    end
  end
end

ActiveRecord::Base.send :include, FlexibleFeeds::ActsAsFollower

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
flexible_feeds-0.4.0 lib/flexible_feeds/acts_as_follower.rb
flexible_feeds-0.3.6 lib/flexible_feeds/acts_as_follower.rb
flexible_feeds-0.3.5 lib/flexible_feeds/acts_as_follower.rb
flexible_feeds-0.3.4 lib/flexible_feeds/acts_as_follower.rb
flexible_feeds-0.3.3 lib/flexible_feeds/acts_as_follower.rb
flexible_feeds-0.3.2 lib/flexible_feeds/acts_as_follower.rb
flexible_feeds-0.3.1 lib/flexible_feeds/acts_as_follower.rb
flexible_feeds-0.3.0 lib/flexible_feeds/acts_as_follower.rb
flexible_feeds-0.2.1 lib/flexible_feeds/acts_as_follower.rb
flexible_feeds-0.1.0 lib/flexible_feeds/acts_as_follower.rb