module Roqua module CoreApi # @api private class UpdatePerson < ActiveInteraction::Base model :session, class: Sessions::OAuthSession model :person, class: Models::Person # Saves the person attributes to server. # Returns true on success, false on validation errors. # Adds errors to person object based on I18n errors.messages. # Raises on other errors. def execute response = session.patch "/people/#{person.id}", person: person.serializable_hash if response.code == 422 errors_to_object(response, person) false else true end end private def errors_to_object(json, obj) json['errors'].each do |attribute, errors| errors.each do |error| obj.errors.add(attribute, I18n.t("errors.messages.#{error}")) end end end end end end