class LikeSystem::Like
Like
class
This class defines the like model in like system
Public Class Methods
Creates a {Like} relationship between a {Liker} object and a {Likee} object
@param [Liker] liker - the {Liker} of the relationship @param [Likee] likee - the {Likee} of the relationship @return [Boolean]
# File lib/like_system/like.rb, line 30 def self.like(liker, likee) validate_likee(likee) validate_liker(liker) if likes?(liker, likee) false else like = scope_by_liker(liker).scope_by_likee(likee).build like.save true end end
Specifies if a {Liker} object likes a {Likee} object
@param [Liker] liker - the {Liker} object to test against @param [Likee] likee - the {Likee} object to test against @return [Boolean]
# File lib/like_system/like.rb, line 88 def self.likes?(liker, likee) validate_likee(likee) validate_liker(liker) scope_by_liker(liker).scope_by_likee(likee).exists? end
Retrieves a scope of {Like} objects filtered by a {Likee} object
@param [Likee] likee - the {Likee} to filter @return [ActiveRecord::Relation]
# File lib/like_system/like.rb, line 101 def self.scope_by_likee(likee) where(likee: likee) end
Retrieves a scope of {Like} objects filtered by a {Likee} type
@param [Class] klass - the {Class} to filter @return [ActiveRecord::Relation]
# File lib/like_system/like.rb, line 111 def self.scope_by_likee_type(klass) where(likee_type: klass.to_s.classify) end
Retrieves a scope of {Like} objects filtered by a {Liker} object
@param [Liker] liker - the {Liker} to filter @return [ActiveRecord::Relation]
# File lib/like_system/like.rb, line 121 def self.scope_by_liker(liker) where(liker: liker) end
Retrieves a scope of {Like} objects filtered by a {Liker} type
@param [Class] klass - the {Class} to filter @return [ActiveRecord::Relation]
# File lib/like_system/like.rb, line 131 def self.scope_by_liker_type(klass) where(liker_type: klass.to_s.classify) end
Toggles a {Like} relationship between a {Liker} object and a {Likee} object
@param [Liker] liker - the {Liker} of the relationship @param [Likee] likee - the {Likee} of the relationship @return [Boolean]
# File lib/like_system/like.rb, line 70 def self.toggle_like(liker, likee) validate_likee(likee) validate_liker(liker) if likes?(liker, likee) unlike(liker, likee) else like(liker, likee) end end
Destroys a {Like} relationship between a {Liker} object and a {Likee} object
@param [Liker] liker - the {Liker} of the relationship @param [Likee] likee - the {Likee} of the relationship @return [Boolean]
# File lib/like_system/like.rb, line 50 def self.unlike(liker, likee) validate_likee(likee) validate_liker(liker) if likes?(liker, likee) like = scope_by_liker(liker).scope_by_likee(likee).take like.destroy true else false end end
Private Class Methods
Validates a likee object
@raise [ArgumentError] if the likee object is invalid
# File lib/like_system/like.rb, line 141 def self.validate_likee(likee) raise ArgumentError.new unless likee.respond_to?(:is_likee?) && likee.is_likee? end
Validates a liker object
@raise [ArgumentError] if the liker object is invalid
# File lib/like_system/like.rb, line 150 def self.validate_liker(liker) raise ArgumentError.new unless liker.respond_to?(:is_liker?) && liker.is_liker? end