Sha256: 01a2db462f3274c01c285495b488be5aa35af0c719e369c0b5bf3782bc703eee

Contents?: true

Size: 1.25 KB

Versions: 4

Compression:

Stored size: 1.25 KB

Contents

# frozen_string_literal: true

module Decidim
  module Proposals
    module Admin
      # A command with all the business logic when an admin creates a private note proposal.
      class CreateProposalNote < Rectify::Command
        # Public: Initializes the command.
        #
        # form         - A form object with the params.
        # current_user - The current user.
        # proposal - the proposal to relate.
        def initialize(form, proposal, current_user)
          @form = form
          @proposal = proposal
          @current_user = current_user
        end

        # Executes the command. Broadcasts these events:
        #
        # - :ok when everything is valid, together with the note proposal.
        # - :invalid if the form wasn't valid and we couldn't proceed.
        #
        # Returns nothing.
        def call
          return broadcast(:invalid) if form.invalid?

          create_proposal_note

          broadcast(:ok, proposal_note)
        end

        private

        attr_reader :form, :proposal_note

        def create_proposal_note
          @proposal_note = ProposalNote.create!(
            body: form.body,
            proposal: @proposal,
            author: @current_user
          )
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
decidim-proposals-0.9.3 app/commands/decidim/proposals/admin/create_proposal_note.rb
decidim-proposals-0.9.2 app/commands/decidim/proposals/admin/create_proposal_note.rb
decidim-proposals-0.9.1 app/commands/decidim/proposals/admin/create_proposal_note.rb
decidim-proposals-0.9.0 app/commands/decidim/proposals/admin/create_proposal_note.rb