Sha256: 943d56aef1e5bc849080b661a3acfd0eda7e2108b26660438032a9a976db5ddb

Contents?: true

Size: 1.2 KB

Versions: 4

Compression:

Stored size: 1.2 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!
        @form.remove_avatar = false
        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
      if @form.avatar
        @user.avatar = @form.avatar
      elsif @form.remove_avatar
        @user.remove_avatar = true
      end
    end

    def update_password
      return unless @form.password.present?

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
decidim-core-0.3.2 app/commands/decidim/update_account.rb
decidim-core-0.3.1 app/commands/decidim/update_account.rb
decidim-core-0.3.0 app/commands/decidim/update_account.rb
decidim-core-0.2.0 app/commands/decidim/update_account.rb