mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-08-02 20:15:22 +02:00
Encapsulate enrichment actions, add tests
This commit is contained in:
parent
dd605a577e
commit
94a807c3c9
9 changed files with 149 additions and 71 deletions
|
@ -22,16 +22,23 @@ module Enrichable
|
|||
}
|
||||
end
|
||||
|
||||
def log_enrichment!(attribute_name:, attribute_value:, source:, metadata: {})
|
||||
de = DataEnrichment.find_or_create_by!(
|
||||
enrichable: self,
|
||||
attribute_name: attribute_name,
|
||||
source: source,
|
||||
)
|
||||
# Convenience method for a single attribute
|
||||
def enrich_attribute(attr, value, source:, metadata: {})
|
||||
enrich_attributes({ attr => value }, source:, metadata:)
|
||||
end
|
||||
|
||||
de.value = attribute_value
|
||||
de.metadata = metadata
|
||||
de.save!
|
||||
# Enriches all attributes that haven't been locked yet
|
||||
def enrich_attributes(attrs, source:, metadata: {})
|
||||
enrichable_attrs = Array(attrs).reject { |k, _v| locked?(k) }
|
||||
|
||||
ActiveRecord::Base.transaction do
|
||||
enrichable_attrs.each do |attr, value|
|
||||
self.send("#{attr}=", value)
|
||||
log_enrichment(attribute_name: attr, attribute_value: value, source: source, metadata: metadata)
|
||||
end
|
||||
|
||||
save
|
||||
end
|
||||
end
|
||||
|
||||
def locked?(attr)
|
||||
|
@ -57,6 +64,18 @@ module Enrichable
|
|||
end
|
||||
|
||||
private
|
||||
def log_enrichment(attribute_name:, attribute_value:, source:, metadata: {})
|
||||
de = DataEnrichment.find_or_create_by(
|
||||
enrichable: self,
|
||||
attribute_name: attribute_name,
|
||||
source: source,
|
||||
)
|
||||
|
||||
de.value = attribute_value
|
||||
de.metadata = metadata
|
||||
de.save
|
||||
end
|
||||
|
||||
def ignored_enrichable_attributes
|
||||
%w[id updated_at created_at]
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue