Class: Socialization::ActiveRecordStores::Like

Inherits:
ActiveRecord::Base show all
Extended by:
Mixins::Base, Stores::Mixins::Base, Stores::Mixins::Like
Defined in:
lib/socialization/stores/active_record/like.rb

Direct Known Subclasses

Like

Class Method Summary collapse

Methods included from Stores::Mixins::Base

touch_actor?, touch_dependents, touch_subject?

Methods included from Stores::Mixins::Like

after_like, after_unlike, touch

Methods included from Mixins::Base

update_counter

Methods inherited from ActiveRecord::Base

#is_followable?, #is_follower?, #is_likeable?, #is_liker?, #is_mentionable?, #is_mentioner?

Class Method Details

.like!(liker, likeable) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/socialization/stores/active_record/like.rb', line 22

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



91
92
93
# File 'lib/socialization/stores/active_record/like.rb', line 91

def likeables(liker, klass, opts = {})
  likeables_relation(liker, klass, opts)
end

.likeables_relation(liker, klass, opts = {}) ⇒ Object

Returns an ActiveRecord::Relation of all the likeables of a certain type that are liked by liker



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/socialization/stores/active_record/like.rb', line 75

def likeables_relation(liker, klass, opts = {})
  rel = klass.where(klass.primary_key =>
    self.select(:likeable_id).
      where(:likeable_type => klass.name.classify).
      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



70
71
72
# File 'lib/socialization/stores/active_record/like.rb', line 70

def likers(likeable, klass, opts = {})
  likers_relation(likeable, klass, opts)
end

.likers_relation(likeable, klass, opts = {}) ⇒ Object

Returns an ActiveRecord::Relation of all the likers of a certain type that are liking likeable



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/socialization/stores/active_record/like.rb', line 54

def likers_relation(likeable, klass, opts = {})
  rel = klass.where(klass.primary_key =>
    self.select(:liker_id).
      where(:liker_type => klass.name.classify).
      where(:likeable_type => likeable.class.to_s).
      where(:likeable_id => likeable.id)
  )

  if opts[:pluck]
    rel.pluck(opts[:pluck])
  else
    rel
  end
end

.likes?(liker, likeable) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/socialization/stores/active_record/like.rb', line 49

def likes?(liker, likeable)
  !like_for(liker, likeable).empty?
end

.remove_likeables(liker) ⇒ Object

Remove all the likeables for liker



102
103
104
105
# File 'lib/socialization/stores/active_record/like.rb', line 102

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



96
97
98
99
# File 'lib/socialization/stores/active_record/like.rb', line 96

def remove_likers(likeable)
  self.where(:likeable_type => likeable.class.name.classify).
       where(:likeable_id => likeable.id).destroy_all
end

.unlike!(liker, likeable) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/socialization/stores/active_record/like.rb', line 37

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