Files
sure/app/models/category_import.rb
Brendon Scheiber 0c126b1674 feat(i18n): extract hardcoded English strings to locale files (#1806)
* Extract hardcoded strings to i18n

Replace numerous hardcoded English strings with I18n lookups (t / I18n.t) across controllers, views, helpers, and components, and convert model validation error messages to symbol keys. Added multiple locale files under config/locales for models and views. This centralizes user-facing notices/alerts, UI text, import/validation messages, and prepares the app for localization and easier translation maintenance.

* Update en.yml

* Update preview-cleanup.yml

* Revert "Update preview-cleanup.yml"

This reverts commit 1ba6d3c34c.

* test: align i18n assertions with translated messages

* Standardize balance error key and tweak locales

Replace SophtronAccount's :requires_balance error key with :no_balance and update related locale strings for sophtron, plaid, and simplefin accounts to use the new key and clearer copy. Also switch the QIF upload redirect notice to use a relative translation key (t('.qif_uploaded')), remove an unused SSO providers help line, and fix a trailing-newline/whitespace issue in the subscriptions locale. These changes standardize validation keys and improve translation consistency and messaging.

---------

Co-authored-by: KiloClaw <kiloclaw@openclaw.ai>
2026-05-17 09:52:49 +02:00

117 lines
3.2 KiB
Ruby

class CategoryImport < Import
def import!
transaction do
rows.each do |row|
category_name = row.name.to_s.strip
category = family.categories.find_or_initialize_by(name: category_name)
category.color = row.category_color.presence || category.color || Category::UNCATEGORIZED_COLOR
category.lucide_icon = row.category_icon.presence || category.lucide_icon || "shapes"
category.parent = nil
category.save!
ensure_placeholder_category(row.category_parent)
end
rows.each do |row|
category = family.categories.find_by!(name: row.name.to_s.strip)
parent = ensure_placeholder_category(row.category_parent)
if parent && parent == category
errors.add(:base, :own_parent, name: category.name)
raise ActiveRecord::RecordInvalid.new(self)
end
next if category.parent == parent
category.update!(parent: parent)
end
end
end
def column_keys
%i[name category_color category_parent category_icon]
end
def required_column_keys
%i[name]
end
def mapping_steps
[]
end
def dry_run
{ categories: rows_count }
end
def csv_template
template = <<-CSV
name*,color,parent_category,lucide_icon
Food & Drink,#f97316,,carrot
Groceries,#407706,Food & Drink,shopping-basket
Salary,#22c55e,,briefcase
CSV
CSV.parse(template, headers: true)
end
def generate_rows_from_csv
rows.destroy_all
validate_required_headers!
name_header = header_for("name")
color_header = header_for("color")
parent_header = header_for("parent_category", "parent category")
icon_header = header_for("lucide_icon", "lucide icon", "icon")
csv_rows.each.with_index(1) do |row, index|
rows.create!(
source_row_number: index,
name: row[name_header].to_s.strip,
category_color: row[color_header].to_s.strip,
category_parent: row[parent_header].to_s.strip,
category_icon: row[icon_header].to_s.strip,
currency: default_currency
)
end
end
private
def validate_required_headers!
missing_headers = required_column_keys.map(&:to_s).reject { |key| header_for(key).present? }
return if missing_headers.empty?
errors.add(:base, :missing_columns, columns: missing_headers.join(", "))
raise ActiveRecord::RecordInvalid.new(self)
end
def header_for(*candidates)
candidates.each do |candidate|
normalized = normalize_header(candidate)
header = normalized_headers[normalized]
return header if header.present?
end
nil
end
def normalized_headers
@normalized_headers ||= csv_headers.to_h { |header| [ normalize_header(header), header ] }
end
def normalize_header(header)
header.to_s.strip.downcase.gsub(/\*/, "").gsub(/[\s-]+/, "_")
end
def ensure_placeholder_category(name)
trimmed_name = name.to_s.strip
return if trimmed_name.blank?
family.categories.find_or_create_by!(name: trimmed_name) do |placeholder|
placeholder.color = Category::UNCATEGORIZED_COLOR
placeholder.lucide_icon = "shapes"
end
end
end