Likeable

Things can be likeable

Likeable is a feature to allow participants to promote (reivindicate, etc.) resources in the platform to their followers.

When liking an element the likes counter for this element is increased and a notification to all the followers of the participant is sent.

Participants can like with their own identity. Each liking identity on its own will increment the likes counter by one.

Data model

A decidim_likes table registers each like that each identity gives to each element. This is, one likeable has many likes, and each like belongs to one likeable. For performance, a likeable has a counter cache of likes.

+----------------------+
|  Decidim::Likeable |
|   ((Proposal,...))   |
+----------------------+  0..N +--------------------+
|-has_many likes|-------+Decidim::Like|
|#counter cache column |       +--------------------+   +-------------+
|-likes_counter |       |-author: a user     |<--+Decidim::User|
+----------------------+       +--------------------+   +-------------+

Thus, each likeable must have the likes counter cache column. This is an example migration to add the likes counter cache column to a resource:

class AddLikesCounterCacheToProposals < ActiveRecord::Migration[5.2]
  def change
    add_column :decidim_proposals_proposals, :likes_count, :integer, null: false, default: 0
  end
end

Administration Panel

It is a good practice to give the opportunity to the admin to switch Likes on and off.

There are two switches that are normally defined in the manifest of the element in the following way (usually this would be at component.rb in a Decidim engine):

    settings.attribute :likes_enabled, type: :boolean, default: true
    settings.attribute :likes_blocked, type: :boolean
  • likes_enabled: when enabled like functionality appears in the public views, when disabled, this functionality is hidden.

  • likes_blocked: when blocked, the counter of likes is visible but no more likes can be added or withdrawn, the button is hidden.

Permissions

In some cases, it may be interesting to require the user to be verified in order to be able to like. To do so, add the like action to the component manifest:

  component.actions = %w(like vote create withdraw amend)

Given that some settings have been defined in the Administration Panel, for the user to have permissions to like likes should be enabled and not blocked.

Public view

The "Like" buttons cell

It normally appears in the resource detail view (show), at the bottom of the resource content, but above the comments when comments are enabled. It allows users to like with any of their personal identity. It also shows the current number of likes for this resource.

To render this button, decidim-core offers the decidim/like_buttons cell. It is strongly recommended to use this cell to make new resources likeable.

cell("decidim/like_buttons", resource)

This cell will render the like buttons depending on whether user liked the resource or not. The likes are labeled as Likes.

# By default the `show` method is invoked as usual
# Renders `render_likes_count` and `render_likes_button` in a block.
#
# It takes into account:
# - if likes are enabled
# - if users are logged in
# - if users require verification
 #
cell("decidim/like_buttons", resource)

The list of likes

The Decidim::LikersListCell renders the list of likes of a resource. It is usually rendered in the show page of the resource, just upside the comments. Additionally, this cell also renders the pop-up required to view the likes of a certain resource.

# to render the list of likes, the cell requires the likeable resource, and the current user
cell "decidim/likers_list", resource
# or using the helper
likers_list_cell(resource)