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::Like

after_like, after_unlike, touch

Methods included from Stores::Mixins::Base

touch_actor?, touch_dependents, touch_victim?

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
# 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
    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



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

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



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

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



66
67
68
69
70
71
72
73
# File 'lib/socialization/stores/active_record/like.rb', line 66

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



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/socialization/stores/active_record/like.rb', line 50

def likers_relation(likeable, klass, opts = {})
  rel = klass.where(:id =>
    self.select(:liker_id).
      where(:liker_type => klass.table_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



45
46
47
# File 'lib/socialization/stores/active_record/like.rb', line 45

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



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

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

.unlike!(liker, likeable) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/socialization/stores/active_record/like.rb', line 35

def unlike!(liker, likeable)
  if likes?(liker, likeable)
    like_for(liker, likeable).destroy_all
    call_after_destroy_hooks(liker, likeable)
    true
  else
    false
  end
end