docbrown/app/services/profile_fields/remove.rb
Michael Kohl 0aa27f3d1e
Refactor profile refreshing (#14643)
* Refactor profile refreshing

* Fix guard in ProfileFields::Remove

* Add more comments to Profile

* Fix typo

* Rephrase comment

* Fix regular expression

* Clean up Profile

* Fix remove spec

* Also remove setter from store accessors

* Remove RUBY_THREAD_VM_STACK_SIZE workaround

* Travis, wake up
2021-09-13 12:15:04 -04:00

43 lines
970 B
Ruby

# This service removes a profile field and ensures that the corresponding store
# accessor also gets removed immediately.
module ProfileFields
class Remove
def self.call(id)
new(id).call
end
attr_reader :profile_field, :error_message
def initialize(id)
@id = id
@success = false
end
def call
@profile_field = ProfileField.find(@id)
if @profile_field.destroy
remove_store_attributes
Profile.refresh_attributes!
@success = true
else
@error_message = @profile_field.errors_as_sentence
end
self
end
def success?
@success
end
private
def remove_store_attributes
accessor = @profile_field.attribute_name.to_sym
store_attributes = Profile.stored_attributes.fetch(:data) { [] }
return unless accessor.in?(store_attributes)
Profile.undef_method(accessor)
Profile.undef_method("#{accessor}=")
end
end
end