Module: LikesHelper

Defined in:
app/helpers/likes_helper.rb

Instance Method Summary collapse

Instance Method Details

#likeable_tag(likeable, opts = {}) ⇒ Object

赞功能 参数 likeable - Like 的对象 :cache - 当为 true 时将不会监测用户是否赞过,直接返回未赞过的状态,以用于 cache 的场景


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/helpers/likes_helper.rb', line 6

def likeable_tag(likeable, opts = {})
  return '' if likeable.blank?

  # 没登录,并且也没用用 cache 的时候,直接返回会跳转倒登录的
  return unlogin_likeable_tag if opts[:cache].blank? && current_user.blank?

  label = "#{likeable.likes_count} 个赞"
  label = '' if likeable.likes_count == 0

  if opts[:cache].blank? && likeable.liked_by_user?(current_user)
    title = '取消赞'
    state = 'active'
    icon = ('i', '', class: 'fa fa-thumbs-up')
  else
    title = ''
    state = ''
    icon = ('i', '', class: 'fa fa-thumbs-up')
  end
  like_label = raw "#{icon} <span>#{label}</span>"

  link_to(like_label, '#', title: title, 'data-count' => likeable.likes_count,
                           'data-state' => state, 'data-type' => likeable.class, 'data-id' => likeable.id,
                           class: "likeable #{state}")
end