Skip to content

Commit

Permalink
Fix file not found issues with file import
Browse files Browse the repository at this point in the history
  • Loading branch information
westonganger committed Feb 4, 2025
1 parent 0f97703 commit 2fd1594
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 35 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# CHANGELOG

### Unreleased - [View Diff](https://github.com/westonganger/rails_i18n_manager/compare/v1.1.0...master)
- [#26](https://github.com/westonganger/rails_i18n_manager/pull/26) - Fix file not found issues with file import
- [#25](https://github.com/westonganger/rails_i18n_manager/pull/25) - Remove catch-all 404 route definition

### v1.1.0 - January 17, 2025 - [View Diff](https://github.com/westonganger/rails_i18n_manager/compare/v1.0.3...v1.1.0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,16 @@ def import
render
else
if @form.valid?
if @form.file.path.end_with?(".json")
parsed_file_contents = JSON.parse(@form.file.read)
else
parsed_file_contents = YAML.safe_load(@form.file.read)
end

begin
TranslationsImportJob.new.perform(
translation_app_id: @form.translation_app_id,
import_file: @form.file.path,
parsed_file_contents: parsed_file_contents,
overwrite_existing: @form.overwrite_existing,
mark_inactive_translations: @form.mark_inactive_translations,
)
Expand Down
5 changes: 0 additions & 5 deletions app/jobs/rails_i18n_manager/application_job.rb

This file was deleted.

16 changes: 5 additions & 11 deletions app/jobs/rails_i18n_manager/translations_import_job.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
module RailsI18nManager
class TranslationsImportJob < ApplicationJob
class TranslationsImportJob

class ImportAbortedError < StandardError; end

def perform(translation_app_id:, import_file:, overwrite_existing: false, mark_inactive_translations: false)
def perform(translation_app_id:, parsed_file_contents:, overwrite_existing: false, mark_inactive_translations: false)
app_record = TranslationApp.find(translation_app_id)

if import_file.end_with?(".json")
translations_hash = JSON.parse(File.read(import_file))
else
translations_hash = YAML.safe_load(File.read(import_file))
end

new_locales = translations_hash.keys - app_record.all_locales
new_locales = parsed_file_contents.keys - app_record.all_locales

if new_locales.any?
raise ImportAbortedError.new("Import aborted. Locale not listed in translation app: #{new_locales.join(', ')}")
end

all_keys = RailsI18nManager.fetch_flattened_dot_notation_keys(translations_hash)
all_keys = RailsI18nManager.fetch_flattened_dot_notation_keys(parsed_file_contents)

key_records_by_key = app_record.translation_keys.includes(:translation_values).index_by(&:key)

Expand All @@ -35,7 +29,7 @@ def perform(translation_app_id:, import_file:, overwrite_existing: false, mark_i
app_record.all_locales.each do |locale|
split_keys = [locale] + key.split(".").map{|x| x}

val = translations_hash.dig(*split_keys)
val = parsed_file_contents.dig(*split_keys)

if val.present?
val_record = key_record.translation_values.detect{|x| x.locale == locale.to_s }
Expand Down
27 changes: 9 additions & 18 deletions spec/unit/jobs/translations_import_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ module RailsI18nManager
es:
foo: foo
YAML
File.write(@filename, yaml, mode: "wb")

expect do
TranslationsImportJob.new.perform(translation_app_id: translation_app.id, import_file: @filename)
TranslationsImportJob.new.perform(translation_app_id: translation_app.id, parsed_file_contents: YAML.safe_load(yaml))
end.to raise_error(TranslationsImportJob::ImportAbortedError)
end

Expand All @@ -43,9 +42,8 @@ module RailsI18nManager
fr:
fr_only_key: fr_only
YAML
File.write(@filename, yaml, mode: "wb")

TranslationsImportJob.new.perform(translation_app_id: translation_app.id, import_file: @filename)
TranslationsImportJob.new.perform(translation_app_id: translation_app.id, parsed_file_contents: YAML.safe_load(yaml))

translation_app.reload

Expand All @@ -70,9 +68,8 @@ module RailsI18nManager
en:
foo: updated
YAML
File.write(@filename, yaml, mode: "wb")

TranslationsImportJob.new.perform(translation_app_id: translation_app.id, import_file: @filename, overwrite_existing: true)
TranslationsImportJob.new.perform(translation_app_id: translation_app.id, parsed_file_contents: YAML.safe_load(yaml), overwrite_existing: true)

foo_value.reload

Expand Down Expand Up @@ -107,9 +104,8 @@ module RailsI18nManager
foo: foo_updated
bar: bar_updated
YAML
File.write(@filename, yaml, mode: "wb")

TranslationsImportJob.new.perform(translation_app_id: translation_app.id, import_file: @filename, overwrite_existing: false)
TranslationsImportJob.new.perform(translation_app_id: translation_app.id, parsed_file_contents: YAML.safe_load(yaml), overwrite_existing: false)

foo_value.reload
blank_value.reload
Expand All @@ -127,9 +123,8 @@ module RailsI18nManager
bar:
baz:
YAML
File.write(@filename, yaml, mode: "wb")

TranslationsImportJob.new.perform(translation_app_id: translation_app.id, import_file: @filename, mark_inactive_translations: true)
TranslationsImportJob.new.perform(translation_app_id: translation_app.id, parsed_file_contents: YAML.safe_load(yaml), mark_inactive_translations: true)
translation_app.translation_keys.reload
expect(translation_app.translation_keys.select(&:active).size).to eq(3)
expect(translation_app.translation_keys.reject(&:active).size).to eq(0)
Expand All @@ -140,9 +135,8 @@ module RailsI18nManager
#bar:
baz:
YAML
File.write(@filename, yaml, mode: "wb")

TranslationsImportJob.new.perform(translation_app_id: translation_app.id, import_file: @filename, mark_inactive_translations: true)
TranslationsImportJob.new.perform(translation_app_id: translation_app.id, parsed_file_contents: YAML.safe_load(yaml), mark_inactive_translations: true)
translation_app.translation_keys.reload
expect(translation_app.translation_keys.select(&:active).size).to eq(2)
expect(translation_app.translation_keys.reject(&:active).size).to eq(1)
Expand All @@ -153,9 +147,8 @@ module RailsI18nManager
bar:
baz:
YAML
File.write(@filename, yaml, mode: "wb")

TranslationsImportJob.new.perform(translation_app_id: translation_app.id, import_file: @filename, mark_inactive_translations: true)
TranslationsImportJob.new.perform(translation_app_id: translation_app.id, parsed_file_contents: YAML.safe_load(yaml), mark_inactive_translations: true)
translation_app.translation_keys.reload
expect(translation_app.translation_keys.select(&:active).size).to eq(3)
expect(translation_app.translation_keys.reject(&:active).size).to eq(0)
Expand All @@ -168,9 +161,8 @@ module RailsI18nManager
bar:
baz:
YAML
File.write(@filename, yaml, mode: "wb")

TranslationsImportJob.new.perform(translation_app_id: translation_app.id, import_file: @filename, mark_inactive_translations: false)
TranslationsImportJob.new.perform(translation_app_id: translation_app.id, parsed_file_contents: YAML.safe_load(yaml), mark_inactive_translations: false)
expect(translation_app.translation_keys.select(&:active).size).to eq(3)
expect(translation_app.translation_keys.reject(&:active).size).to eq(0)

Expand All @@ -180,9 +172,8 @@ module RailsI18nManager
#bar:
baz:
YAML
File.write(@filename, yaml, mode: "wb")

TranslationsImportJob.new.perform(translation_app_id: translation_app.id, import_file: @filename, mark_inactive_translations: false)
TranslationsImportJob.new.perform(translation_app_id: translation_app.id, parsed_file_contents: YAML.safe_load(yaml), mark_inactive_translations: false)
expect(translation_app.translation_keys.select(&:active).size).to eq(3)
expect(translation_app.translation_keys.reject(&:active).size).to eq(0)
end
Expand Down

0 comments on commit 2fd1594

Please sign in to comment.