Skip to content

Commit

Permalink
Generate locale translations for ledger entries, add default for miss…
Browse files Browse the repository at this point in the history
…ing translations
  • Loading branch information
bmaclean committed Jun 13, 2024
1 parent f25a940 commit 8128b31
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ def create_initializer_file
template 'initializer.rb', 'config/initializers/ledger_accountable.rb'
end

def copy_localization_file
copy_file '../../../locale/en.yml', 'config/locales/ledger.en.yml'
end

private

def self.next_migration_number(path)
Expand Down
10 changes: 6 additions & 4 deletions lib/generators/ledger_accountable/templates/ledger_entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@ class LedgerEntry < ApplicationRecord
belongs_to :ledger_item, polymorphic: true, optional: false

enum entry_type: { addition: 0, deletion: 1, modification: 2 }

store :metadata, coder: JSON

validates :amount, presence: true
validates :entry_type, presence: true

def to_itemized_s(line_type = :line)
I18n.t! "#{TRANSLATION_PREFIX}.#{ledger_item_type.constantize.model_name.param_key}.#{line_type}",
metadata.symbolize_keys
metadata.symbolize_keys
rescue I18n::MissingTranslationData, I18n::MissingInterpolationArgument
ledger_item_type
I18n.t "#{TRANSLATION_PREFIX}.default.#{line_type}",
default: ledger_item_type,
model_name: ledger_item_type.humanize
end
end
7 changes: 7 additions & 0 deletions lib/locale/en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
en:
ledger:
default:
line: "%{model_name}"
addition: "%{model_name} added"
deletion: "%{model_name} removed"
modification: "%{model_name} modified"

0 comments on commit 8128b31

Please sign in to comment.