Sha256: e50b7e2321a27bda8a23e25318c304f344685ae52fcc2a945b38863b9008f896

Contents?: true

Size: 1.5 KB

Versions: 19

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true

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

        def initialize(form)
          @form = form
        end

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

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

          transaction do
            create_election!
            create_gallery if process_gallery?
          end

          broadcast(:ok, election)
        end

        private

        attr_reader :form, :election, :gallery

        def create_election!
          attributes = {
            title: form.title,
            description: form.description,
            start_time: form.start_time,
            end_time: form.end_time,
            component: form.current_component,
            questionnaire: Decidim::Forms::Questionnaire.new,
            salt: Tokenizer.random_salt
          }

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

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
decidim-elections-0.26.10 app/commands/decidim/elections/admin/create_election.rb
decidim-elections-0.26.9 app/commands/decidim/elections/admin/create_election.rb
decidim-elections-0.26.8 app/commands/decidim/elections/admin/create_election.rb
decidim-elections-0.26.7 app/commands/decidim/elections/admin/create_election.rb
decidim-elections-0.26.5 app/commands/decidim/elections/admin/create_election.rb
decidim-elections-0.26.4 app/commands/decidim/elections/admin/create_election.rb
decidim-elections-0.26.3 app/commands/decidim/elections/admin/create_election.rb
decidim-elections-0.26.2 app/commands/decidim/elections/admin/create_election.rb
decidim-elections-0.26.1 app/commands/decidim/elections/admin/create_election.rb
decidim-elections-0.26.0 app/commands/decidim/elections/admin/create_election.rb
decidim-elections-0.26.0.rc2 app/commands/decidim/elections/admin/create_election.rb
decidim-elections-0.26.0.rc1 app/commands/decidim/elections/admin/create_election.rb
decidim-elections-0.25.2 app/commands/decidim/elections/admin/create_election.rb
decidim-elections-0.25.1 app/commands/decidim/elections/admin/create_election.rb
decidim-elections-0.25.0 app/commands/decidim/elections/admin/create_election.rb
decidim-elections-0.25.0.rc4 app/commands/decidim/elections/admin/create_election.rb
decidim-elections-0.25.0.rc3 app/commands/decidim/elections/admin/create_election.rb
decidim-elections-0.25.0.rc2 app/commands/decidim/elections/admin/create_election.rb
decidim-elections-0.25.0.rc1 app/commands/decidim/elections/admin/create_election.rb