Class: Decidim::EndorseResource

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

Overview

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

Instance Method Summary collapse

Methods inherited from Command

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

Constructor Details

#initialize(resource, current_user, current_group_id = nil) ⇒ EndorseResource

Public: Initializes the command.

resource - An instance of Decidim::Endorsable. current_user - The current user. current_group_id- (optional) The current_group that is endorsing the Resource.



12
13
14
15
16
# File 'app/commands/decidim/endorse_resource.rb', line 12

def initialize(resource, current_user, current_group_id = nil)
  @resource = resource
  @current_user = current_user
  @current_group_id = current_group_id
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.



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/commands/decidim/endorse_resource.rb', line 24

def call
  return broadcast(:invalid) if existing_group_endorsement?

  endorsement = build_resource_endorsement
  if endorsement.save
    notify_endorser_followers
    broadcast(:ok, endorsement)
  else
    broadcast(:invalid)
  end
rescue ActiveRecord::RecordNotUnique
  broadcast(:invalid)
end