1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-07-21 22:29:38 +02:00
Maybe/app/models/import/mapping.rb
Zach Gollwitzer 398b246965
CSV Imports Overhaul (Transactions, Trades, Accounts, and Mint import support) (#1209)
* Remove stale 1.0 import logic and model

* Fresh start

* Checkpoint before removing nav

* First working prototype

* Add trade, account, and mint import flows

* Basic working version with tests

* System tests for each import type

* Clean up mappings flow

* Clean up PR, refactor stale code, tests

* Add back row validations

* Row validations

* Fix import job test

* Fix import navigation

* Fix mint import configuration form

* Currency preset for new accounts
2024-10-01 10:47:59 -04:00

56 lines
1.6 KiB
Ruby

class Import::Mapping < ApplicationRecord
CREATE_NEW_KEY = "internal_new_resource"
belongs_to :import
belongs_to :mappable, polymorphic: true, optional: true
validates :key, presence: true, uniqueness: { scope: [ :import_id, :type ] }, allow_blank: true
scope :for_import, ->(import) { where(import: import) }
scope :creational, -> { where(create_when_empty: true, mappable: nil) }
scope :categories, -> { where(type: "Import::CategoryMapping") }
scope :tags, -> { where(type: "Import::TagMapping") }
scope :accounts, -> { where(type: "Import::AccountMapping") }
scope :account_types, -> { where(type: "Import::AccountTypeMapping") }
class << self
def mappable_for(key)
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"
end
end
def selectable_values
raise NotImplementedError, "Subclass must implement selectable_values"
end
def values_count
raise NotImplementedError, "Subclass must implement values_count"
end
def mappable_class
nil
end
def creatable?
mappable.nil? && key.present? && create_when_empty
end
def create_mappable!
raise NotImplementedError, "Subclass must implement create_mappable!"
end
end