Sha256: c7385a095f0bab973a965a2a5b65840a473ff5cca8097f30e4ceaf57c1560626

Contents?: true

Size: 1.4 KB

Versions: 9

Compression:

Stored size: 1.4 KB

Contents

# frozen_string_literal: true

module Decidim
  module DecidimAwesome
    module Admin
      class CreateMenuHack < Rectify::Command
        # Public: Initializes the command.
        #
        def initialize(form, menu_name)
          @form = form
          @menu = AwesomeConfig.find_or_initialize_by(var: menu_name, 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("menu_hacks.url_exists", scope: "decidim.decidim_awesome.admin")) if url_exists?

          menu.value = [] unless menu.value.is_a? Array
          menu.value << to_params
          menu.save!
          broadcast(:ok, menu)
        rescue StandardError => e
          broadcast(:invalid, e.message)
        end

        private

        attr_reader :form, :menu

        def url_exists?
          return false unless menu

          menu.value&.detect { |i| i["url"] == form.url.gsub(/\?.*/, "") }
        end

        def to_params
          params = form.to_params
          url = Addressable::URI.parse(params[:url])
          params[:url] = url.path if url.host == form.current_organization.host
          params
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
decidim-decidim_awesome-0.8.3 app/commands/decidim/decidim_awesome/admin/create_menu_hack.rb
decidim-decidim_awesome-0.8.2 app/commands/decidim/decidim_awesome/admin/create_menu_hack.rb
decidim-decidim_awesome-0.8.1 app/commands/decidim/decidim_awesome/admin/create_menu_hack.rb
decidim-decidim_awesome-0.8.0 app/commands/decidim/decidim_awesome/admin/create_menu_hack.rb
decidim-decidim_awesome-0.7.2 app/commands/decidim/decidim_awesome/admin/create_menu_hack.rb
decidim-decidim_awesome-0.7.0 app/commands/decidim/decidim_awesome/admin/create_menu_hack.rb
decidim-decidim_awesome-0.6.7 app/commands/decidim/decidim_awesome/admin/create_menu_hack.rb
decidim-decidim_awesome-0.6.6 app/commands/decidim/decidim_awesome/admin/create_menu_hack.rb
decidim-decidim_awesome-0.6.5 app/commands/decidim/decidim_awesome/admin/create_menu_hack.rb