Sha256: 4a6fbd9e33ab6481f3d7443e059f7d7604d60f7bfba609ce7cfbe4f34e33cf9f
Contents?: true
Size: 1.28 KB
Versions: 1
Compression:
Stored size: 1.28 KB
Contents
module Likeable extend ActiveSupport::Concern included do belongs_to :target, polymorphic: true has_many :likes, -> { where(positive: true) }, dependent: :delete_all, as: :target has_many :likers, class_name: 'User', through: :likes, source: :user has_many :dislikes, -> { where(positive: false) }, class_name: 'Like', dependent: :delete_all, as: :target has_many :dislikers, class_name: 'User', through: :dislikes, source: :user scope :liked_by, ->(user_id) do positive_likes_string = if ActiveRecord::Base.connection.instance_of?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter) "likes.positive = 't'" else 'likes.positive = 1' end select('music_videos.*, likes.created_at AS liked_at').joins("RIGHT JOIN likes ON #{positive_likes_string} AND likes.target_type = 'MusicVideo' AND likes.target_id = music_videos.id"). where('likes.user_id = ? AND music_videos.id IS NOT NULL', user_id) end end def update_likes_counter self.class.where(id: self.id).update_all likes_count: self.likes.count, dislikes_count: self.dislikes.count end module ClassMethods def likes_or_dislikes_for(user, ids) user.likes_or_dislikes.for_targets(name, ids).index_by(&:target_id) end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
voluntary-0.2.4 | app/models/concerns/likeable.rb |