Class: Components::LikeButton

Inherits:
BaseButton
  • Object
show all
Defined in:
app/components/like_button.rb

Instance Method Summary collapse

Constructor Details

#initialize(thing:, small: false) ⇒ LikeButton

Returns a new instance of LikeButton.



4
5
6
7
# File 'app/components/like_button.rb', line 4

def initialize(thing:, small: false)
  @thing = thing
  @small = small
end

Instance Method Details

#before_template ⇒ Object



13
14
15
16
17
18
19
20
# File 'app/components/like_button.rb', line 13

def before_template
  @liked = current_user.liked?(@thing)
  @form_attributes = @liked ?
    {id: current_user.liked_list.list_items.find_by(listable: @thing), _destroy: "1"} :
    {listable_type: @thing.model_name, listable_id: @thing.id}
  @count = @thing.like_count
  @count = nil if @count == 0
end

#render? ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
# File 'app/components/like_button.rb', line 9

def render?
  current_user&.liked_list
end

#view_template ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'app/components/like_button.rb', line 22

def view_template
  DoButton(
    icon: (@liked ? "heart-fill" : "heart"),
    variant: :secondary,
    small: @small,
    href: list_path(current_user.liked_list, list: {list_items_attributes: {"0" => @form_attributes}}),
    method: :patch,
    label: @count,
    help: (@liked ? t("components.like_button.unlike") : t("components.like_button.like"))
  )
end