Sha256: 863280b7dff97fc4999e09ee2565b1d38f39af2b1127ce6a064170616a874b35

Contents?: true

Size: 1.63 KB

Versions: 3

Compression:

Stored size: 1.63 KB

Contents

# frozen_string_literal: true

module Decidim
  module DecidimAwesome
    module Admin
      class RenameScopeLabel < Command
        # Public: Initializes the command.
        #
        # params - A constraint params
        def initialize(params, organization)
          @text = params[:text]&.strip&.gsub(" ", "_")&.parameterize&.truncate(64)
          @scope = params[:scope]
          @key = params[:key]
          @attribute = params[:attribute]
          @organization = organization
        end

        # Executes the command. Broadcasts these events:
        #
        # - :ok when everything is valid.
        # - :invalid if we couldn't proceed.
        #
        # Returns nothing.
        def call
          raise StandardError, "empty value" if @text.blank?
          raise StandardError, "key already exists" if config.value.keys.include? @text

          transaction do
            config.value[@text] = config.value.delete @key
            config.save!
            if config_scope
              config_scope.var = scope
              config_scope.save!
            end
          end

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

        private

        def scope
          @scope.gsub(/_#{@key}$/, "_#{@text}")
        end

        def config
          @config ||= Decidim::DecidimAwesome::AwesomeConfig.find_by!(var: @attribute, organization: @organization)
        end

        def config_scope
          @config_scope ||= Decidim::DecidimAwesome::AwesomeConfig.find_by(var: @scope, organization: @organization)
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
decidim-decidim_awesome-0.11.3 app/commands/decidim/decidim_awesome/admin/rename_scope_label.rb
decidim-decidim_awesome-0.11.2 app/commands/decidim/decidim_awesome/admin/rename_scope_label.rb
decidim-decidim_awesome-0.11.1 app/commands/decidim/decidim_awesome/admin/rename_scope_label.rb