Sha256: 7adf57472eb1bba1d4ba3b9a0692c50feb2cdce05f73eb8dabfed0e59bea925d

Contents?: true

Size: 1.79 KB

Versions: 21

Compression:

Stored size: 1.79 KB

Contents

# frozen_string_literal: true

module Decidim
  module Admin
    # A form object to select which users will be sent the selective newsletters
    class SelectiveNewsletterForm < Decidim::Form
      mimic :newsletter

      attribute :participatory_space_types, Array[SelectiveNewsletterParticipatorySpaceTypeForm]
      attribute :scope_ids, Array
      attribute :send_to_all_users, Boolean
      attribute :send_to_participants, Boolean
      attribute :send_to_followers, Boolean

      validates :send_to_all_users, presence: true, unless: ->(form) { form.send_to_participants.present? || form.send_to_followers.present? }
      validates :send_to_followers, presence: true, if: ->(form) { form.send_to_all_users.blank? && form.send_to_participants.blank? }
      validates :send_to_participants, presence: true, if: ->(form) { form.send_to_all_users.blank? && form.send_to_followers.blank? }

      validate :at_least_one_participatory_space_selected

      def map_model(_newsletter)
        self.participatory_space_types = Decidim.participatory_space_manifests.map do |manifest|
          SelectiveNewsletterParticipatorySpaceTypeForm.from_model(manifest: manifest)
        end
      end

      # Make sure the empty scope is not passed because then some logic could
      # assume erroneously that some scope is selected.
      def scope_ids
        super.select(&:presence)
      end

      private

      def at_least_one_participatory_space_selected
        return if send_to_all_users && current_user.admin?

        errors.add(:base, :at_least_one_space) if spaces_selected.blank?
      end

      def spaces_selected
        participatory_space_types.map do |type|
          spaces = type.ids.reject(&:empty?)
          [type.manifest_name, spaces] if spaces.present?
        end.compact
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
decidim-admin-0.27.9 app/forms/decidim/admin/selective_newsletter_form.rb
decidim-admin-0.27.8 app/forms/decidim/admin/selective_newsletter_form.rb
decidim-admin-0.27.7 app/forms/decidim/admin/selective_newsletter_form.rb
decidim-admin-0.27.6 app/forms/decidim/admin/selective_newsletter_form.rb
decidim-admin-0.26.10 app/forms/decidim/admin/selective_newsletter_form.rb
decidim-admin-0.26.9 app/forms/decidim/admin/selective_newsletter_form.rb
decidim-admin-0.27.5 app/forms/decidim/admin/selective_newsletter_form.rb
decidim-admin-0.26.8 app/forms/decidim/admin/selective_newsletter_form.rb
decidim-admin-0.27.4 app/forms/decidim/admin/selective_newsletter_form.rb
decidim-admin-0.27.3 app/forms/decidim/admin/selective_newsletter_form.rb
decidim-admin-0.26.7 app/forms/decidim/admin/selective_newsletter_form.rb
decidim-admin-0.26.5 app/forms/decidim/admin/selective_newsletter_form.rb
decidim-admin-0.27.2 app/forms/decidim/admin/selective_newsletter_form.rb
decidim-admin-0.27.1 app/forms/decidim/admin/selective_newsletter_form.rb
decidim-admin-0.26.4 app/forms/decidim/admin/selective_newsletter_form.rb
decidim-admin-0.27.0 app/forms/decidim/admin/selective_newsletter_form.rb
decidim-admin-0.26.3 app/forms/decidim/admin/selective_newsletter_form.rb
decidim-admin-0.27.0.rc2 app/forms/decidim/admin/selective_newsletter_form.rb
decidim-admin-0.27.0.rc1 app/forms/decidim/admin/selective_newsletter_form.rb
decidim-admin-0.26.2 app/forms/decidim/admin/selective_newsletter_form.rb