Sha256: ef92fd5d1fc3a6e9fe2f4ffb82561b54a1a6d9580cb0ece4d6dd0e3cec885ed4

Contents?: true

Size: 1.11 KB

Versions: 10

Compression:

Stored size: 1.11 KB

Contents

# frozen_string_literal: true

module Decidim
  # This command updates the user's account.
  class UpdateAccount < Rectify::Command
    # Updates a user's account.
    #
    # user - The user to be updated.
    # form - The form with the data.
    def initialize(user, form)
      @user = user
      @form = form
    end

    def call
      return broadcast(:invalid) unless @form.valid?

      update_personal_data
      update_avatar
      update_password

      if @user.valid?
        @user.save!
        broadcast(:ok, @user.unconfirmed_email.present?)
      else
        if @user.errors.has_key? :avatar
          @form.errors.add :avatar, @user.errors[:avatar]
        end
        broadcast(:invalid)
      end
    end

    private

    def update_personal_data
      @user.name = @form.name
      @user.email = @form.email
    end

    def update_avatar
      @user.avatar = @form.avatar
      @user.remove_avatar = @form.remove_avatar
    end

    def update_password
      return if @form.password.blank?

      @user.password = @form.password
      @user.password_confirmation = @form.password_confirmation
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
decidim-core-0.8.4 app/commands/decidim/update_account.rb
decidim-core-0.8.3 app/commands/decidim/update_account.rb
decidim-core-0.8.2 app/commands/decidim/update_account.rb
decidim-core-0.8.1 app/commands/decidim/update_account.rb
decidim-core-0.8.0 app/commands/decidim/update_account.rb
decidim-core-0.7.4 app/commands/decidim/update_account.rb
decidim-core-0.7.3 app/commands/decidim/update_account.rb
decidim-core-0.7.2 app/commands/decidim/update_account.rb
decidim-core-0.7.1 app/commands/decidim/update_account.rb
decidim-core-0.7.0 app/commands/decidim/update_account.rb