Sha256: 3508ae09303d7a9bdd4c4d98b75e24ad3bc490c709693f783bfa3ef31505f737

Contents?: true

Size: 1.16 KB

Versions: 2

Compression:

Stored size: 1.16 KB

Contents

# frozen_string_literal: true

module Decidim
  module DecidimAwesome
    module Admin
      class UpdateConfig < Rectify::Command
        # Public: Initializes the command.
        #
        # form - A config form
        def initialize(form)
          @form = form
        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?

          begin
            form.attributes.each do |key, val|
              # ignore nil attributes (must specifically be set to false if necessary)
              next unless form.valid_keys.include?(key)

              setting = AwesomeConfig.find_or_initialize_by(var: key, organization: form.current_organization)

              setting.value = val.respond_to?(:attributes) ? val.attributes : val
              setting.save!
            end

            broadcast(:ok)
          rescue ActiveRecord::RecordInvalid => e
            broadcast(:invalid, e.message)
          end
        end

        attr_reader :form
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
decidim-decidim_awesome-0.6.6 app/commands/decidim/decidim_awesome/admin/update_config.rb
decidim-decidim_awesome-0.6.5 app/commands/decidim/decidim_awesome/admin/update_config.rb