Sha256: 60e06a6c4254da2a6c03722744f4f3659825bb04f9903ea160da9365c5131f27

Contents?: true

Size: 1.4 KB

Versions: 8

Compression:

Stored size: 1.4 KB

Contents

# frozen_string_literal: true

module Decidim
  module DecidimAwesome
    module Admin
      class CreateCustomRedirect < Command
        # Public: Initializes the command.
        #
        def initialize(form)
          @form = form
          @redirections = AwesomeConfig.find_or_initialize_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_exists", scope: "decidim.decidim_awesome.admin")) if url_exists?

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

        private

        attr_reader :form, :redirections

        delegate :to_params, to: :form

        def create_redirection!
          redirections.value = {} unless redirections.value.is_a? Hash
          redirections.value[to_params[0]] = to_params[1]
          redirections.save!
        end

        def url_exists?
          return false unless redirections
          return false unless redirections.value.is_a? Hash

          redirections.value[form.origin].present?
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

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