Module: Recommendable::ActsAsRecommendedTo::LikeMethods

Defined in:
lib/recommendable/acts_as_recommended_to.rb

Instance Method Summary collapse

Instance Method Details

#like(object) ⇒ Object

Creates a Recommendable::Like to associate self to a passed object. If self is currently found to have disliked object, the corresponding Recommendable::Dislike will be destroyed. It will also be removed from the user’s stash or ignores.

Parameters:

  • object (Object)

    the object you want self to like.

Returns:

  • true if object has been liked

Raises:



42
43
44
45
46
47
48
49
# File 'lib/recommendable/acts_as_recommended_to.rb', line 42

def like(object)
  raise RecordNotRecommendableError unless object.recommendable?
  return if likes?(object)
  completely_unrecommend(object)
  likes.create!(:likeable_id => object.id, :likeable_type => object.class.to_s)
  Resque.enqueue RecommendationRefresher, self.id, object.send(:rates_by)
  true
end

#likedArray

Returns an array of ActiveRecord objects that self has liked.

Returns:

  • (Array)

    an array of ActiveRecord objects that self has liked



73
74
75
# File 'lib/recommendable/acts_as_recommended_to.rb', line 73

def liked
  likes.map {|like| like.likeable}
end

#liked_for(klass) ⇒ Array

Get a list of records belonging to a passed class that self currently likes.

Parameters:

  • klass (Class, String, Symbol)

    the class of records. Can be the class constant, or a String/Symbol representation of the class name.

Returns:

  • (Array)

    an array of ActiveRecord objects that self has liked belonging to klass



82
83
84
# File 'lib/recommendable/acts_as_recommended_to.rb', line 82

def liked_for(klass)
  klass.to_s.classify.constantize.find likes_for(klass).map(&:likeable_id)
end

#likes?(object) ⇒ Boolean

Checks to see if self has already liked a passed object.

Parameters:

  • object (Object)

    the object you want to check

Returns:

  • (Boolean)

    true if self likes object, false if not



55
56
57
# File 'lib/recommendable/acts_as_recommended_to.rb', line 55

def likes?(object)
  likes.exists?(:likeable_id => object.id, :likeable_type => object.class.to_s)
end

#unlike(object) ⇒ Object

Destroys a Recommendable::Like currently associating self with object

Parameters:

  • object (Object)

    the object you want to remove from self’s likes

Returns:

  • true if object is unliked, nil if nothing happened



63
64
65
66
67
68
# File 'lib/recommendable/acts_as_recommended_to.rb', line 63

def unlike(object)
  if likes.where(:likeable_id => object.id, :likeable_type => object.class.to_s).first.try(:destroy)
    Resque.enqueue RecommendationRefresher, self.id, object.send(:rates_by)
    true
  end
end