/
like_store.rb
143 lines (123 loc) · 4.26 KB
/
like_store.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
module Socialization
module ActiveRecordStores
class LikeStore < ActiveRecord::Base
belongs_to :liker, :polymorphic => true
belongs_to :likeable, :polymorphic => true
scope :liked_by, lambda { |liker| where(
:liker_type => liker.class.table_name.classify,
:liker_id => liker.id)
}
scope :liking, lambda { |likeable| where(
:likeable_type => likeable.class.table_name.classify,
:likeable_id => likeable.id)
}
@@after_create_hook = nil
@@after_destroy_hook = nil
class << self
def table_name
'likes'
end
def like!(liker, likeable)
unless likes?(liker, likeable)
self.create! do |like|
like.liker = liker
like.likeable = likeable
end
call_after_create_hook(liker, likeable)
liker.touch if [:all, :liker].include?(touch) && liker.respond_to?(:touch)
likeable.touch if [:all, :likeable].include?(touch) && likeable.respond_to?(:touch)
true
else
false
end
end
def unlike!(liker, likeable)
if likes?(liker, likeable)
like_for(liker, likeable).destroy_all
call_after_destroy_hook(liker, likeable)
liker.touch if [:all, :liker].include?(touch) && liker.respond_to?(:touch)
likeable.touch if [:all, :likeable].include?(touch) && likeable.respond_to?(:touch)
true
else
false
end
end
def likes?(liker, likeable)
!like_for(liker, likeable).empty?
end
# Returns an ActiveRecord::Relation of all the likers of a certain type that are liking likeable
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
# Returns all the likers of a certain type that are liking likeable
def likers(likeable, klass, opts = {})
rel = likers_relation(likeable, klass, opts)
if rel.is_a?(ActiveRecord::Relation)
rel.all
else
rel
end
end
# Returns an ActiveRecord::Relation of all the likeables of a certain type that are liked by liker
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
# Returns all the likeables of a certain type that are liked by liker
def likeables(liker, klass, opts = {})
rel = likeables_relation(liker, klass, opts)
if rel.is_a?(ActiveRecord::Relation)
rel.all
else
rel
end
end
def touch(what = nil)
if what.nil?
@touch || false
else
raise ArgumentError unless [:all, :liker, :likeable, false, nil].include?(what)
@touch = what
end
end
def after_like(method)
raise ArgumentError unless method.is_a?(Symbol) || method.nil?
@after_create_hook = method
end
def after_unlike(method)
raise ArgumentError unless method.is_a?(Symbol) || method.nil?
@after_destroy_hook = method
end
private
def call_after_create_hook(liker, likeable)
self.send(@after_create_hook, liker, likeable) if @after_create_hook
end
def call_after_destroy_hook(liker, likeable)
self.send(@after_destroy_hook, liker, likeable) if @after_destroy_hook
end
def like_for(liker, likeable)
liked_by(liker).liking( likeable)
end
end # class << self
end
end
end