Class: Socialization::ActiveRecordStores::Like
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Socialization::ActiveRecordStores::Like
- Extended by:
- Mixins::Base, Stores::Mixins::Base, Stores::Mixins::Like
- Defined in:
- lib/socialization/stores/active_record/like.rb
Direct Known Subclasses
Class Method Summary collapse
- .like!(liker, likeable) ⇒ Object
-
.likeables(liker, klass, opts = {}) ⇒ Object
Returns all the likeables of a certain type that are liked by liker.
-
.likeables_relation(liker, klass, opts = {}) ⇒ Object
Returns an ActiveRecord::Relation of all the likeables of a certain type that are liked by liker.
-
.likers(likeable, klass, opts = {}) ⇒ Object
Returns all the likers of a certain type that are liking likeable.
-
.likers_relation(likeable, klass, opts = {}) ⇒ Object
Returns an ActiveRecord::Relation of all the likers of a certain type that are liking likeable.
- .likes?(liker, likeable) ⇒ Boolean
-
.remove_likeables(liker) ⇒ Object
Remove all the likeables for liker.
-
.remove_likers(likeable) ⇒ Object
Remove all the likers for likeable.
- .unlike!(liker, likeable) ⇒ Object
Methods included from Stores::Mixins::Base
touch_actor?, touch_dependents, touch_victim?
Methods included from Stores::Mixins::Like
after_like, after_unlike, touch
Methods included from Mixins::Base
Methods inherited from ActiveRecord::Base
#is_followable?, #is_follower?, #is_likeable?, #is_liker?, #is_mentionable?, #is_mentioner?
Class Method Details
.like!(liker, likeable) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/socialization/stores/active_record/like.rb', line 21 def like!(liker, likeable) unless likes?(liker, likeable) self.create! do |like| like.liker = liker like.likeable = likeable end update_counter(liker, likees_count: +1) update_counter(likeable, likers_count: +1) call_after_create_hooks(liker, likeable) true else false end end |
.likeables(liker, klass, opts = {}) ⇒ Object
Returns all the likeables of a certain type that are liked by liker
93 94 95 96 97 98 99 100 |
# File 'lib/socialization/stores/active_record/like.rb', line 93 def likeables(liker, klass, opts = {}) rel = likeables_relation(liker, klass, opts) if rel.is_a?(ActiveRecord::Relation) rel.to_a else rel end end |
.likeables_relation(liker, klass, opts = {}) ⇒ Object
Returns an ActiveRecord::Relation of all the likeables of a certain type that are liked by liker
78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/socialization/stores/active_record/like.rb', line 78 def likeables_relation(liker, klass, opts = {}) rel = klass.where(:id => self.select(:likeable_id). where(:liker_type => liker.class.to_s). where(:liker_id => liker.id) ) if opts[:pluck] rel.pluck(opts[:pluck]) else rel end end |
.likers(likeable, klass, opts = {}) ⇒ Object
Returns all the likers of a certain type that are liking likeable
68 69 70 71 72 73 74 75 |
# File 'lib/socialization/stores/active_record/like.rb', line 68 def likers(likeable, klass, opts = {}) rel = likers_relation(likeable, klass, opts) if rel.is_a?(ActiveRecord::Relation) rel.to_a else rel end end |
.likers_relation(likeable, klass, opts = {}) ⇒ Object
Returns an ActiveRecord::Relation of all the likers of a certain type that are liking likeable
53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/socialization/stores/active_record/like.rb', line 53 def likers_relation(likeable, klass, opts = {}) rel = klass.where(:id => self.select(:liker_id). where(:liker_type => klass.name.classify). where(:likeable_id => likeable.id) ) if opts[:pluck] rel.pluck(opts[:pluck]) else rel end end |
.likes?(liker, likeable) ⇒ Boolean
48 49 50 |
# File 'lib/socialization/stores/active_record/like.rb', line 48 def likes?(liker, likeable) !like_for(liker, likeable).empty? end |
.remove_likeables(liker) ⇒ Object
Remove all the likeables for liker
108 109 110 111 |
# File 'lib/socialization/stores/active_record/like.rb', line 108 def remove_likeables(liker) self.where(:liker_type => liker.class.name.classify). where(:liker_id => liker.id).destroy_all end |
.remove_likers(likeable) ⇒ Object
Remove all the likers for likeable
103 104 105 |
# File 'lib/socialization/stores/active_record/like.rb', line 103 def remove_likers(likeable) self.where(:likeable_id => likeable.id).destroy_all end |
.unlike!(liker, likeable) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/socialization/stores/active_record/like.rb', line 36 def unlike!(liker, likeable) if likes?(liker, likeable) like_for(liker, likeable).destroy_all update_counter(liker, likees_count: -1) update_counter(likeable, likers_count: -1) call_after_destroy_hooks(liker, likeable) true else false end end |