Sha256: 032e910fa1507b67b7cb18a771bb09477f5dc8bae43406a40992879abf0cbaca

Contents?: true

Size: 1.82 KB

Versions: 7

Compression:

Stored size: 1.82 KB

Contents

# frozen_string_literal: true

module Decidim
  module Votings
    module Admin
      # A command with all the business logic when creating a new voting space
      class CreateVoting < Decidim::Command
        # Public: Initializes the command.
        #
        # form - A form object with the params.
        def initialize(form)
          @form = form
        end

        # Executes the command. Broadcasts these events:
        #
        # - :ok when everything is valid.
        # - :invalid if the form was not valid and we could not proceed.
        #
        # Returns nothing.
        def call
          return broadcast(:invalid) if form.invalid?

          voting = create_voting!

          if voting.persisted?
            broadcast(:ok, voting)
          else
            form.errors.add(:banner_image, voting.errors[:banner_image]) if voting.errors.include? :banner_image
            form.errors.add(:introductory_image, voting.errors[:introductory_image]) if voting.errors.include? :introductory_image
            broadcast(:invalid)
          end
        end

        private

        attr_reader :form

        def create_voting!
          Decidim.traceability.create(
            Voting,
            form.current_user,
            organization: form.current_organization,
            title: form.title,
            slug: form.slug,
            description: form.description,
            scope: form.scope,
            start_time: form.start_time,
            end_time: form.end_time,
            promoted: form.promoted,
            banner_image: form.banner_image,
            introductory_image: form.introductory_image,
            voting_type: form.voting_type,
            census_contact_information: form.census_contact_information,
            show_check_census: form.show_check_census
          )
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
decidim-elections-0.28.4 app/commands/decidim/votings/admin/create_voting.rb
decidim-elections-0.28.3 app/commands/decidim/votings/admin/create_voting.rb
decidim-elections-0.28.2 app/commands/decidim/votings/admin/create_voting.rb
decidim-elections-0.28.1 app/commands/decidim/votings/admin/create_voting.rb
decidim-elections-0.28.0 app/commands/decidim/votings/admin/create_voting.rb
decidim-elections-0.28.0.rc5 app/commands/decidim/votings/admin/create_voting.rb
decidim-elections-0.28.0.rc4 app/commands/decidim/votings/admin/create_voting.rb