Sha256: e008363fb3ada8f31ceccd9955a52400cf8fcf1906754e060589865913278672

Contents?: true

Size: 1.67 KB

Versions: 19

Compression:

Stored size: 1.67 KB

Contents

# frozen_string_literal: true

module Decidim
  module Proposals
    module Admin
      # A form object to be used when admin users want to import a collection of proposals
      # from another component.
      class ProposalsImportForm < Decidim::Form
        mimic :proposals_import

        attribute :origin_component_id, Integer
        attribute :import_proposals, Boolean
        attribute :states, Array

        validates :origin_component_id, :origin_component, :states, :current_component, presence: true
        validates :import_proposals, allow_nil: false, acceptance: true
        validate :valid_states

        VALID_STATES = %w(accepted not_answered evaluating rejected withdrawn).freeze

        def states_collection
          VALID_STATES.map do |state|
            OpenStruct.new(
              name: I18n.t(state, scope: "decidim.proposals.answers"),
              value: state
            )
          end
        end

        def states
          super.reject(&:blank?)
        end

        def origin_component
          @origin_component ||= origin_components.find_by(id: origin_component_id)
        end

        def origin_components
          @origin_components ||= current_participatory_space.components.where.not(id: current_component.id).where(manifest_name: :proposals)
        end

        def origin_components_collection
          origin_components.map do |component|
            [component.name[I18n.locale.to_s], component.id]
          end
        end

        private

        def valid_states
          return if states.all? do |state|
            VALID_STATES.include?(state)
          end

          errors.add(:states, :invalid)
        end
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
decidim-proposals-0.16.1 app/forms/decidim/proposals/admin/proposals_import_form.rb
decidim-proposals-0.16.0 app/forms/decidim/proposals/admin/proposals_import_form.rb
decidim-proposals-0.15.2 app/forms/decidim/proposals/admin/proposals_import_form.rb
decidim-proposals-0.15.1 app/forms/decidim/proposals/admin/proposals_import_form.rb
decidim-proposals-0.15.0 app/forms/decidim/proposals/admin/proposals_import_form.rb
decidim-proposals-0.14.4 app/forms/decidim/proposals/admin/proposals_import_form.rb
decidim-proposals-0.14.3 app/forms/decidim/proposals/admin/proposals_import_form.rb
decidim-proposals-0.14.2 app/forms/decidim/proposals/admin/proposals_import_form.rb
decidim-proposals-0.14.1 app/forms/decidim/proposals/admin/proposals_import_form.rb
decidim-proposals-0.13.1 app/forms/decidim/proposals/admin/proposals_import_form.rb
decidim-proposals-0.12.2 app/forms/decidim/proposals/admin/proposals_import_form.rb
decidim-proposals-0.13.0 app/forms/decidim/proposals/admin/proposals_import_form.rb
decidim-proposals-0.12.1 app/forms/decidim/proposals/admin/proposals_import_form.rb
decidim-proposals-0.13.0.pre1 app/forms/decidim/proposals/admin/proposals_import_form.rb
decidim-proposals-0.12.0 app/forms/decidim/proposals/admin/proposals_import_form.rb
decidim-proposals-0.11.2 app/forms/decidim/proposals/admin/proposals_import_form.rb
decidim-proposals-0.12.0.pre app/forms/decidim/proposals/admin/proposals_import_form.rb
decidim-proposals-0.11.1 app/forms/decidim/proposals/admin/proposals_import_form.rb
decidim-proposals-0.11.0.pre1 app/forms/decidim/proposals/admin/proposals_import_form.rb