Sha256: c38a537c9df45b480bc977cf4dd4f554af4858d9ec55df3f23e7208d620cb015

Contents?: true

Size: 1.16 KB

Versions: 13

Compression:

Stored size: 1.16 KB

Contents

# frozen_string_literal: true

module Decidim
  module System
    # A command with all the business logic when updating an admin in
    # the system.
    class UpdateAdmin < Decidim::Command
      # Public: Initializes the command.
      #
      # form - A form object with the params.
      def initialize(admin, form)
        @admin = admin
        @form = form
      end

      # Executes the command. Broadcasts these events:
      #
      # - :ok when everything is valid.
      # - :invalid if the form was not valid and we could not proceed.
      #
      # Returns nothing.
      def call
        return broadcast(:invalid) if form.invalid?

        update_admin
        broadcast(:ok)
      end

      private

      attr_reader :form

      def update_admin
        @admin.update!(attributes)
      end

      def attributes
        {
          email: form.email
        }.merge(password_attributes)
      end

      def password_attributes
        return {} if form.password == form.password_confirmation && form.password.blank?

        {
          password: form.password,
          password_confirmation: form.password_confirmation
        }
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
decidim-system-0.29.1 app/commands/decidim/system/update_admin.rb
decidim-system-0.28.4 app/commands/decidim/system/update_admin.rb
decidim-system-0.29.0 app/commands/decidim/system/update_admin.rb
decidim-system-0.28.3 app/commands/decidim/system/update_admin.rb
decidim-system-0.29.0.rc4 app/commands/decidim/system/update_admin.rb
decidim-system-0.29.0.rc3 app/commands/decidim/system/update_admin.rb
decidim-system-0.29.0.rc2 app/commands/decidim/system/update_admin.rb
decidim-system-0.29.0.rc1 app/commands/decidim/system/update_admin.rb
decidim-system-0.28.2 app/commands/decidim/system/update_admin.rb
decidim-system-0.28.1 app/commands/decidim/system/update_admin.rb
decidim-system-0.28.0 app/commands/decidim/system/update_admin.rb
decidim-system-0.28.0.rc5 app/commands/decidim/system/update_admin.rb
decidim-system-0.28.0.rc4 app/commands/decidim/system/update_admin.rb