Module: Recommendable::ActsAsRecommendedTo::LikeMethods
- Defined in:
- lib/recommendable/acts_as_recommended_to.rb
Instance Method Summary collapse
-
#like(object) ⇒ Object
Creates a Recommendable::Like to associate self to a passed object.
-
#liked ⇒ Array
An array of ActiveRecord objects that self has liked.
-
#liked_for(klass) ⇒ Array
Get a list of records belonging to a passed class that self currently likes.
-
#likes?(object) ⇒ Boolean
Checks to see if self has already liked a passed object.
-
#unlike(object) ⇒ Object
Destroys a Recommendable::Like currently associating self with object.
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.
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 |
#liked ⇒ Array
Returns 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.
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.
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
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 |