Sha256: c2b0b0c20223be82936d2a5e75ef469707775c865314321073f875de6becccf9
Contents?: true
Size: 1.93 KB
Versions: 7
Compression:
Stored size: 1.93 KB
Contents
# frozen_string_literal: true module Decidim # A command with all the business logic related with a user endorsing a resource. # This is a user creates and endorsement. class EndorseResource < Decidim::Command # Public: Initializes the command. # # resource - An instance of Decidim::Endorsable. # current_user - The current user. # current_group_id- (optional) The current_grup that is endorsing the Resource. def initialize(resource, current_user, current_group_id = nil) @resource = resource @current_user = current_user @current_group_id = current_group_id end # Executes the command. Broadcasts these events: # # - :ok when everything is valid, together with the resource vote. # - :invalid if the form wasn't valid and we couldn't proceed. # # Returns nothing. 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 end private def existing_group_endorsement? @current_group_id.present? && @resource.endorsements.exists?(decidim_user_group_id: @current_group_id) end def build_resource_endorsement endorsement = @resource.endorsements.build(author: @current_user) endorsement.user_group = user_groups.find(@current_group_id) if @current_group_id.present? endorsement end def user_groups Decidim::UserGroups::ManageableUserGroups.for(@current_user).verified end def notify_endorser_followers Decidim::EventsManager.publish( event: "decidim.events.resource_endorsed", event_class: Decidim::ResourceEndorsedEvent, resource: @resource, followers: @current_user.followers, extra: { endorser_id: @current_user.id } ) end end end
Version data entries
7 entries across 7 versions & 1 rubygems