Class: LikeService

Inherits:
Object
  • Object
show all
Defined in:
app/services/like_service.rb

Instance Method Summary collapse

Constructor Details

#initialize(user = nil) ⇒ LikeService

Returns a new instance of LikeService.


4
5
6
# File 'app/services/like_service.rb', line 4

def initialize(user=nil)
  @user = user
end

Instance Method Details

#create(post_id) ⇒ Object


8
9
10
11
# File 'app/services/like_service.rb', line 8

def create(post_id)
  post = post_service.find!(post_id)
  user.like!(post)
end

#destroy(like_id) ⇒ Object


13
14
15
16
17
18
19
20
21
# File 'app/services/like_service.rb', line 13

def destroy(like_id)
  like = Like.find(like_id)
  if user.owns?(like)
    user.retract(like)
    true
  else
    false
  end
end

#find_for_post(post_id) ⇒ Object


23
24
25
26
# File 'app/services/like_service.rb', line 23

def find_for_post(post_id)
  likes = post_service.find!(post_id).likes
  user ? likes.order(Arel.sql("author_id = #{user.person.id} DESC")) : likes
end

#unlike_post(post_id) ⇒ Object


28
29
30
31
32
33
34
35
36
37
# File 'app/services/like_service.rb', line 28

def unlike_post(post_id)
  likes = post_service.find!(post_id).likes
  likes = likes.order(Arel.sql("author_id = #{user.person.id} DESC"))
  if !likes.empty? && user.owns?(likes[0])
    user.retract(likes[0])
    true
  else
    false
  end
end