-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathreaction.rb
More file actions
41 lines (35 loc) · 924 Bytes
/
reaction.rb
File metadata and controls
41 lines (35 loc) · 924 Bytes
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
require './database'
require 'rumoji'
class Reaction < ActiveRecord::Base
belongs_to :news, counter_cache: true
belongs_to :user
def self.raw_reactions_by_news_id(news_id)
reactions = Reaction.reactions_by_news_id(news_id)
reactions.count.map do |k, v|
{
'reaction' => Rumoji.decode(k),
'amount' => v,
'date' => reactions.minimum(:date).select { |r| r == k }[k]
}
end
end
def self.reactions_by_news_id(news_id)
Reaction.where("news_id = #{news_id}").group(:reaction)
end
def self.react(news_id:, user_id:, reaction:)
r = News.find(news_id)
.reaction
.where(user_id: user_id, reaction: reaction)
.first
if r.nil?
Reaction.create(
reaction: reaction,
news_id: news_id,
user_id: user_id,
date: DateTime.now.strftime('%s')
)
else
r.destroy
end
end
end