Sha256: 922bad9288d22dab7233b57eee81f31d4d01aa18d11887d28f2e2278ce34cf5a

Contents?: true

Size: 1.46 KB

Versions: 7

Compression:

Stored size: 1.46 KB

Contents

# frozen_string_literal: true

module Decidim
  module Votings
    module Admin
      # A command with the business logic to create the ballot style
      class CreateBallotStyle < Rectify::Command
        def initialize(form)
          @form = form
        end

        # Executes the command. Broadcast this events:
        # - :ok when everything is valid
        # - :invalid when the form wasn't valid and couldn't proceed
        #
        # Returns nothing.
        def call
          return broadcast(:invalid) unless form.valid?

          begin
            create_ballot_style!
          rescue ActiveRecord::RecordNotUnique
            form.errors.add(:code, :taken)
            return broadcast(:invalid)
          end

          create_ballot_style_questions!

          broadcast(:ok)
        end

        private

        attr_reader :form, :ballot_style

        def create_ballot_style!
          attributes = {
            code: form.code
          }

          @ballot_style = Decidim::Votings::BallotStyle.create!(
            voting: form.current_participatory_space,
            attributes: attributes
          )
        end

        def create_ballot_style_questions!
          form.question_ids.each do |question_id|
            Decidim::Votings::BallotStyleQuestion.create!(
              decidim_votings_ballot_style_id: ballot_style.id,
              decidim_elections_question_id: question_id
            )
          end
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
decidim-elections-0.25.2 app/commands/decidim/votings/admin/create_ballot_style.rb
decidim-elections-0.25.1 app/commands/decidim/votings/admin/create_ballot_style.rb
decidim-elections-0.25.0 app/commands/decidim/votings/admin/create_ballot_style.rb
decidim-elections-0.25.0.rc4 app/commands/decidim/votings/admin/create_ballot_style.rb
decidim-elections-0.25.0.rc3 app/commands/decidim/votings/admin/create_ballot_style.rb
decidim-elections-0.25.0.rc2 app/commands/decidim/votings/admin/create_ballot_style.rb
decidim-elections-0.25.0.rc1 app/commands/decidim/votings/admin/create_ballot_style.rb