Sha256: 784efe54613b0b7b9772b8a4f5ab785e52256c8cee1f06e43b11f8b1353d76cb

Contents?: true

Size: 1.65 KB

Versions: 31

Compression:

Stored size: 1.65 KB

Contents

# frozen_string_literal: true

module Decidim
  module Elections
    module Admin
      # This command is executed when the user creates an Answer
      # from the admin panel.
      class CreateAnswer < Rectify::Command
        include ::Decidim::AttachmentMethods
        include ::Decidim::GalleryMethods

        def initialize(form)
          @form = form
        end

        # Creates the answer if valid.
        #
        # Broadcasts :ok if successful, :invalid otherwise.
        def call
          return broadcast(:invalid) if invalid?

          if process_gallery?
            build_gallery
            return broadcast(:invalid) if gallery_invalid?
          end

          transaction do
            create_answer
            link_proposals
            create_gallery if process_gallery?
          end

          broadcast(:ok, answer)
        end

        private

        attr_reader :form, :answer, :gallery

        def invalid?
          form.election.started? || form.invalid?
        end

        def create_answer
          attributes = {
            question: form.question,
            title: form.title,
            description: form.description,
            weight: form.weight
          }

          @answer = Decidim.traceability.create!(
            Answer,
            form.current_user,
            attributes,
            visibility: "all"
          )
          @attached_to = @answer
        end

        def proposals
          @proposals ||= answer.sibling_scope(:proposals).where(id: form.proposal_ids)
        end

        def link_proposals
          answer.link_resources(proposals, "related_proposals")
        end
      end
    end
  end
end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
decidim-elections-0.26.7 app/commands/decidim/elections/admin/create_answer.rb
decidim-elections-0.26.5 app/commands/decidim/elections/admin/create_answer.rb
decidim-elections-0.26.4 app/commands/decidim/elections/admin/create_answer.rb
decidim-elections-0.26.3 app/commands/decidim/elections/admin/create_answer.rb
decidim-elections-0.26.2 app/commands/decidim/elections/admin/create_answer.rb
decidim-elections-0.26.1 app/commands/decidim/elections/admin/create_answer.rb
decidim-elections-0.26.0 app/commands/decidim/elections/admin/create_answer.rb
decidim-elections-0.26.0.rc2 app/commands/decidim/elections/admin/create_answer.rb
decidim-elections-0.26.0.rc1 app/commands/decidim/elections/admin/create_answer.rb
decidim-elections-0.25.2 app/commands/decidim/elections/admin/create_answer.rb
decidim-elections-0.25.1 app/commands/decidim/elections/admin/create_answer.rb
decidim-elections-0.25.0 app/commands/decidim/elections/admin/create_answer.rb
decidim-elections-0.25.0.rc4 app/commands/decidim/elections/admin/create_answer.rb
decidim-elections-0.25.0.rc3 app/commands/decidim/elections/admin/create_answer.rb
decidim-elections-0.25.0.rc2 app/commands/decidim/elections/admin/create_answer.rb
decidim-elections-0.25.0.rc1 app/commands/decidim/elections/admin/create_answer.rb
decidim-elections-0.24.3 app/commands/decidim/elections/admin/create_answer.rb
decidim-elections-0.23.6 app/commands/decidim/elections/admin/create_answer.rb
decidim-elections-0.24.2 app/commands/decidim/elections/admin/create_answer.rb
decidim-elections-0.23.5 app/commands/decidim/elections/admin/create_answer.rb