Class: Decidim::LikeResource

Inherits:
Command
  • Object
show all
Defined in:
app/commands/decidim/like_resource.rb

Overview

A command with all the business logic related with a user liking a resource. This is a user creates and like.

Instance Method Summary collapse

Methods inherited from Command

call, #evaluate, #method_missing, #respond_to_missing?, #transaction, #with_events

Constructor Details

#initialize(resource, current_user) ⇒ LikeResource

Public: Initializes the command.

resource - An instance of Decidim::Likeable. current_user - The current user.



11
12
13
14
# File 'app/commands/decidim/like_resource.rb', line 11

def initialize(resource, current_user)
  @resource = resource
  @current_user = current_user
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Decidim::Command

Instance Method Details

#callObject

Executes the command. Broadcasts these events:

  • :ok when everything is valid, together with the resource vote.

  • :invalid if the form was not valid and we could not proceed.

Returns nothing.



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

def call
  like = build_resource_like
  if like.save
    notify_liker_followers
    broadcast(:ok, like)
  else
    broadcast(:invalid)
  end
rescue ActiveRecord::RecordNotUnique
  broadcast(:invalid)
end