Sha256: f4aba0cc3d2a3a11f1eea7a210f9fbee8d7622a74cdcc4d3e27ebabbd23af711

Contents?: true

Size: 1.37 KB

Versions: 5

Compression:

Stored size: 1.37 KB

Contents

# frozen_string_literal: true

module Decidim
  module DecidimAwesome
    module Admin
      class DestroyProposalCustomField < Rectify::Command
        # Public: Initializes the command.
        #
        # key - the key to destroy inise proposal_custom_fields
        # organization
        def initialize(key, organization)
          @key = key
          @organization = organization
        end

        # Executes the command. Broadcasts these events:
        #
        # - :ok when everything is valid.
        # - :invalid if we couldn't proceed.
        #
        # Returns nothing.
        def call
          fields = AwesomeConfig.find_by(var: :proposal_custom_fields, organization: @organization)
          return broadcast(:invalid, "Not a hash") unless fields&.value.is_a? Hash
          return broadcast(:invalid, "#{key} key invalid") unless fields.value.has_key?(@key)

          fields.value.except!(@key)
          fields.save!
          # remove constrains associated (a new config var is generated automatically, by removing it, it will trigger destroy on dependents)
          constraint = AwesomeConfig.find_by(var: "proposal_custom_field_#{@key}", organization: @organization)
          constraint.destroy! if constraint.present?

          broadcast(:ok, @key)
        rescue StandardError => e
          broadcast(:invalid, e.message)
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
decidim-decidim_awesome-0.8.3 app/commands/decidim/decidim_awesome/admin/destroy_proposal_custom_field.rb
decidim-decidim_awesome-0.8.2 app/commands/decidim/decidim_awesome/admin/destroy_proposal_custom_field.rb
decidim-decidim_awesome-0.8.1 app/commands/decidim/decidim_awesome/admin/destroy_proposal_custom_field.rb
decidim-decidim_awesome-0.8.0 app/commands/decidim/decidim_awesome/admin/destroy_proposal_custom_field.rb
decidim-decidim_awesome-0.7.2 app/commands/decidim/decidim_awesome/admin/destroy_proposal_custom_field.rb