Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module Followable
- extend ActiveSupport::Concern
- include ActiveModel::Model
- included do
- has_many :followers_followed, foreign_key: 'followed_id'
- has_many :users, through: :followers_followed
- has_many :messages, foreign_key: 'followable_id'
- after_update do
- message = self.create_message
- self.followers.each do |user|
- MessagesUser.create message: message, user: user
- end
- end
- end
- def create_message
- Message.create content: 'Yay, a message', followable_id: self.id, followable_type_code: self.class.table_name
- end
- def followers
- self.users.where('followers_followed.followable_type_code = ?', self.class.table_name)
- end
- def is_followed?(user)
- return self.followers.include? user if user.class == User
- return self.followers.any? { |follower| follower.id == user } if user.class == Fixnum
- false
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement