mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-08-02 20:15:22 +02:00
Account-level import configuration templates (#1946)
* Account-level import configuration templates * Default import to family's preferred date format
This commit is contained in:
parent
5b2fa3d707
commit
0544089710
15 changed files with 108 additions and 43 deletions
|
@ -2,8 +2,11 @@ class Import::AccountMapping < Import::Mapping
|
|||
validates :mappable, presence: true, if: :requires_mapping?
|
||||
|
||||
class << self
|
||||
def mapping_values(import)
|
||||
import.rows.map(&:account).uniq
|
||||
def mappables_by_key(import)
|
||||
unique_values = import.rows.map(&:account).uniq
|
||||
accounts = import.family.accounts.where(name: unique_values).index_by(&:name)
|
||||
|
||||
unique_values.index_with { |value| accounts[value] }
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -2,8 +2,8 @@ class Import::AccountTypeMapping < Import::Mapping
|
|||
validates :value, presence: true
|
||||
|
||||
class << self
|
||||
def mapping_values(import)
|
||||
import.rows.map(&:entity_type).uniq
|
||||
def mappables_by_key(import)
|
||||
import.rows.map(&:entity_type).uniq.index_with { nil }
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -1,7 +1,10 @@
|
|||
class Import::CategoryMapping < Import::Mapping
|
||||
class << self
|
||||
def mapping_values(import)
|
||||
import.rows.map(&:category).uniq
|
||||
def mappables_by_key(import)
|
||||
unique_values = import.rows.map(&:category).uniq
|
||||
categories = import.family.categories.where(name: unique_values).index_by(&:name)
|
||||
|
||||
unique_values.index_with { |value| categories[value] }
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -18,19 +18,8 @@ class Import::Mapping < ApplicationRecord
|
|||
find_by(key: key)&.mappable
|
||||
end
|
||||
|
||||
def sync(import)
|
||||
unique_values = mapping_values(import).uniq
|
||||
|
||||
unique_values.each do |value|
|
||||
mapping = find_or_initialize_by(key: value, import: import, create_when_empty: value.present?)
|
||||
mapping.save(validate: false) if mapping.new_record?
|
||||
end
|
||||
|
||||
where(import: import).where.not(key: unique_values).destroy_all
|
||||
end
|
||||
|
||||
def mapping_values(import)
|
||||
raise NotImplementedError, "Subclass must implement mapping_values"
|
||||
def mappables_by_key(import)
|
||||
raise NotImplementedError, "Subclass must implement mappables_by_key"
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -30,11 +30,10 @@ class Import::Row < ApplicationRecord
|
|||
end
|
||||
end
|
||||
|
||||
def sync_mappings
|
||||
Import::CategoryMapping.sync(import) if import.column_keys.include?(:category)
|
||||
Import::TagMapping.sync(import) if import.column_keys.include?(:tags)
|
||||
Import::AccountMapping.sync(import) if import.column_keys.include?(:account)
|
||||
Import::AccountTypeMapping.sync(import) if import.column_keys.include?(:entity_type)
|
||||
def update_and_sync(params)
|
||||
assign_attributes(params)
|
||||
save!(validate: false)
|
||||
import.sync_mappings
|
||||
end
|
||||
|
||||
private
|
||||
|
|
|
@ -1,7 +1,11 @@
|
|||
class Import::TagMapping < Import::Mapping
|
||||
class << self
|
||||
def mapping_values(import)
|
||||
import.rows.map(&:tags_list).flatten.uniq
|
||||
def mappables_by_key(import)
|
||||
unique_values = import.rows.map(&:tags_list).flatten.uniq
|
||||
|
||||
tags = import.family.tags.where(name: unique_values).index_by(&:name)
|
||||
|
||||
unique_values.index_with { |value| tags[value] }
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue