Sha256: 5f13d7eaf03688bc5fb6b15f82fbad1aeb84f4397ddd874cca156ebf10e3eaef

Contents?: true

Size: 1.31 KB

Versions: 6

Compression:

Stored size: 1.31 KB

Contents

# frozen_string_literal: true

module Decidim
  module DecidimAwesome
    module Admin
      class UpdateCustomRedirect < Command
        # Public: Initializes the command.
        #
        def initialize(form, item)
          @form = form
          @item = item
          @redirects = AwesomeConfig.find_by(var: :custom_redirects, organization: form.current_organization)
        end

        # Executes the command. Broadcasts these events:
        #
        # - :ok when everything is valid.
        # - :invalid if we couldn't proceed.
        #
        # Returns nothing.
        def call
          return broadcast(:invalid) if form.invalid?
          return broadcast(:invalid, I18n.t("custom_redirects.origin_missing", scope: "decidim.decidim_awesome.admin")) unless url_exists?

          redirects.value&.except!(item.origin)
          redirects.value[to_params[0]] = to_params[1]
          redirects.save!

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

        private

        attr_reader :form, :redirects, :item

        delegate :to_params, to: :form

        def url_exists?
          return unless redirects
          return unless redirects.value.is_a? Hash

          redirects.value[item.origin].present?
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
decidim-decidim_awesome-0.10.3 app/commands/decidim/decidim_awesome/admin/update_custom_redirect.rb
decidim-decidim_awesome-0.10.2 app/commands/decidim/decidim_awesome/admin/update_custom_redirect.rb
decidim-decidim_awesome-0.10.1 app/commands/decidim/decidim_awesome/admin/update_custom_redirect.rb
decidim-decidim_awesome-0.10.0 app/commands/decidim/decidim_awesome/admin/update_custom_redirect.rb
decidim-decidim_awesome-0.9.3 app/commands/decidim/decidim_awesome/admin/update_custom_redirect.rb
decidim-decidim_awesome-0.9.1 app/commands/decidim/decidim_awesome/admin/update_custom_redirect.rb