Sha256: 6e0babb2b4faccde49c4f940a5aded83eb47cfac9c6c1a10bdc494451b0f48b6

Contents?: true

Size: 1.76 KB

Versions: 27

Compression:

Stored size: 1.76 KB

Contents

# frozen_string_literal: true

module Decidim
  module Proposals
    module Admin
      # A common abstract to be used by the Merge and Split proposals forms.
      class ProposalsForkForm < Decidim::Form
        mimic :proposals_import

        attribute :target_component_id, Integer
        attribute :proposal_ids, Array

        validates :target_component, :proposals, :current_component, presence: true
        validate :same_participatory_space
        validate :mergeable_to_same_component

        def proposals
          @proposals ||= Decidim::Proposals::Proposal.where(component: current_component, id: proposal_ids).uniq
        end

        def target_component
          return current_component if clean_target_component_id == current_component.id

          @target_component ||= current_component.siblings.find_by(id: target_component_id)
        end

        def same_component?
          target_component == current_component
        end

        private

        def mergeable_to_same_component
          return true unless same_component?

          public_proposals = proposals.any? do |proposal|
            !proposal.official? || proposal.votes.any? || proposal.endorsements.any?
          end

          errors.add(:proposal_ids, :invalid) if public_proposals
        end

        def same_participatory_space
          return if !target_component || !current_component

          errors.add(:target_component, :invalid) if current_component.participatory_space != target_component.participatory_space
        end

        # Private: Returns the id of the target component.
        #
        # We receive this as ["id"] since it's from a select in a form.
        def clean_target_component_id
          target_component_id.first.to_i
        end
      end
    end
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
decidim-proposals-0.25.2 app/forms/decidim/proposals/admin/proposals_fork_form.rb
decidim-proposals-0.25.1 app/forms/decidim/proposals/admin/proposals_fork_form.rb
decidim-proposals-0.25.0 app/forms/decidim/proposals/admin/proposals_fork_form.rb
decidim-proposals-0.25.0.rc4 app/forms/decidim/proposals/admin/proposals_fork_form.rb
decidim-proposals-0.25.0.rc3 app/forms/decidim/proposals/admin/proposals_fork_form.rb
decidim-proposals-0.25.0.rc2 app/forms/decidim/proposals/admin/proposals_fork_form.rb
decidim-proposals-0.25.0.rc1 app/forms/decidim/proposals/admin/proposals_fork_form.rb
decidim-proposals-0.24.3 app/forms/decidim/proposals/admin/proposals_fork_form.rb
decidim-proposals-0.23.6 app/forms/decidim/proposals/admin/proposals_fork_form.rb
decidim-proposals-0.24.2 app/forms/decidim/proposals/admin/proposals_fork_form.rb
decidim-proposals-0.23.5 app/forms/decidim/proposals/admin/proposals_fork_form.rb
decidim-proposals-0.24.1 app/forms/decidim/proposals/admin/proposals_fork_form.rb
decidim-proposals-0.24.0 app/forms/decidim/proposals/admin/proposals_fork_form.rb
decidim-proposals-0.24.0.rc2 app/forms/decidim/proposals/admin/proposals_fork_form.rb
decidim-proposals-0.23.4 app/forms/decidim/proposals/admin/proposals_fork_form.rb
decidim-proposals-0.24.0.rc1 app/forms/decidim/proposals/admin/proposals_fork_form.rb
decidim-proposals-0.23.3 app/forms/decidim/proposals/admin/proposals_fork_form.rb
decidim-proposals-0.23.2 app/forms/decidim/proposals/admin/proposals_fork_form.rb
decidim-proposals-0.23.1 app/forms/decidim/proposals/admin/proposals_fork_form.rb
decidim-proposals-0.23.1.rc1 app/forms/decidim/proposals/admin/proposals_fork_form.rb