docbrown/app/services/profiles/update.rb
Michael Kohl d284e597cd
[deploy] Add Profiles::Update service object (#9912)
* Add Profiles::Update service object

* Refactor

* Rename Profile.fields to Profile.attributes

* Keep sync from user -> profile

* And and update comments

* Rename Profile.refresh_store_accessors!

* Make forwarding getters in user dynamic

* Add more explanation

* Fix typo

* Update service object

* Fix return value

* Fix specs

* Travis, let's be friends again?

* Remove Travis change

* Add require_dependency in Profile

* Refactor

* More refactoring

* Avoid double sync

* Fix specs

* Fix mapped attributes sync
2020-09-03 09:19:25 -04:00

42 lines
1.1 KiB
Ruby

module Profiles
class Update
def self.call(profile, updated_attributes = {})
new(profile, updated_attributes).call
end
attr_reader
def initialize(profile, updated_attributes)
@profile = profile
@updated_attributes = updated_attributes
@success = false
end
def call
# Ensure we have up to date attributes
Profile.refresh_attributes!
# We don't update `data` directly. This uses the defined store_attributes
# so we can make use of their typecasting.
@profile.assign_attributes(@updated_attributes)
# Before saving, filter out obsolete profile fields
@profile.data.slice!(*Profile.attributes)
return unless @profile.save
# Propagate changes back to the `users` table
user_attributes = @profile.data.transform_keys do |key|
Profile::MAPPED_ATTRIBUTES.fetch(key, key).to_s
end
@profile.user._skip_profile_sync = true
@success = true if @profile.user.update(user_attributes)
@profile.user._skip_profile_sync = false
self
end
def success?
@success
end
end
end