From ccdeea7cdfb409b61e5d8ef23b03c52fbfd027c0 Mon Sep 17 00:00:00 2001 From: Weston Ganger Date: Fri, 1 Nov 2024 10:04:42 -0700 Subject: [PATCH] Allow alternative .yaml file extension --- CHANGELOG.md | 1 + .../forms/translation_file_form.rb | 20 +++++---- spec/request/translations_controller_spec.rb | 42 ++++++++++++++++++- 3 files changed, 54 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7bb4f90..58280db 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # CHANGELOG ### Unreleased - [View Diff](https://github.com/westonganger/rails_i18n_manager/compare/v1.0.1...master) +- Allow `.yaml` files to be uploaded. Previously the upload validation would only allow `.yml`. - Drop support for Rails v5.x - [#19](https://github.com/westonganger/rails_i18n_manager/pull/19) - Fix width issue on translation values form and view page diff --git a/app/lib/rails_i18n_manager/forms/translation_file_form.rb b/app/lib/rails_i18n_manager/forms/translation_file_form.rb index 1b00968..e983e6b 100644 --- a/app/lib/rails_i18n_manager/forms/translation_file_form.rb +++ b/app/lib/rails_i18n_manager/forms/translation_file_form.rb @@ -23,26 +23,30 @@ def validate_file return end - if [".yml", ".json"].exclude?(File.extname(file)) - errors.add(:file, "Invalid file format. Must be yml or json file.") + file_extname = File.extname(file) + + if [".yml", ".yaml", ".json"].exclude?(file_extname) + errors.add(:file, "Invalid file format. Must be yaml or json file.") return end - if File.read(file).blank? + file_contents = File.read(file) + + if file_contents.blank? errors.add(:file, "Empty file provided.") return end - case File.extname(file) - when ".yml" - if !YAML.safe_load(File.read(file)).is_a?(Hash) - errors.add(:file, "Invalid yml file.") + case file_extname + when ".yml", ".yaml" + if !YAML.safe_load(file_contents).is_a?(Hash) + errors.add(:file, "Invalid #{file_extname.sub(".","")} file.") return end when ".json" begin - JSON.parse(File.read(file)) + JSON.parse(file_contents) rescue JSON::ParserError errors.add(:file, "Invalid json file.") return diff --git a/spec/request/translations_controller_spec.rb b/spec/request/translations_controller_spec.rb index 0aac716..a05c38a 100644 --- a/spec/request/translations_controller_spec.rb +++ b/spec/request/translations_controller_spec.rb @@ -98,7 +98,7 @@ module RailsI18nManager end context "import" do - it "succeeds" do + it "behaves as expected when nothing uploaded" do get rails_i18n_manager.import_translations_path expect(response).to have_http_status(200) @@ -107,13 +107,17 @@ module RailsI18nManager post rails_i18n_manager.import_translations_url, params: {translation_app_id: translation_app.id} expect(response).to have_http_status(200) + end + it "accepts for .yml and .yaml files" do yaml = <<~YAML en: foo: bar: baz: YAML + + #.yml filename = Rails.root.join("tmp/#{SecureRandom.hex(6)}.yml") File.write(filename, yaml, mode: "wb") @@ -126,6 +130,21 @@ module RailsI18nManager expect(assigns(:form).errors.full_messages).to be_empty expect(response).to redirect_to(rails_i18n_manager.translations_path) + # .yaml + filename = Rails.root.join("tmp/#{SecureRandom.hex(6)}.yaml") + File.write(filename, yaml, mode: "wb") + + post rails_i18n_manager.import_translations_path, params: { + import_form: { + translation_app_id: translation_app.id, + file: Rack::Test::UploadedFile.new(filename) + } + } + expect(assigns(:form).errors.full_messages).to be_empty + expect(response).to redirect_to(rails_i18n_manager.translations_path) + end + + it "accepts .json files" do json_content = <<~JSON_CONTENT { "en": { @@ -135,6 +154,7 @@ module RailsI18nManager } } JSON_CONTENT + filename = Rails.root.join("tmp/#{SecureRandom.hex(6)}.json") File.write(filename, json_content, mode: "wb") @@ -147,6 +167,26 @@ module RailsI18nManager expect(assigns(:form).errors.full_messages).to be_empty expect(response).to redirect_to(rails_i18n_manager.translations_path) end + + it "renders errors when another file type provided" do + json_content = <<~JSON_CONTENT + { + "en": "foo" + } + JSON_CONTENT + + filename = Rails.root.join("tmp/#{SecureRandom.hex(6)}.conf") + File.write(filename, json_content, mode: "wb") + + post rails_i18n_manager.import_translations_path, params: { + import_form: { + translation_app_id: translation_app.id, + file: Rack::Test::UploadedFile.new(filename) + } + } + expect(assigns(:form).errors.full_messages).to eq(["File Invalid file format. Must be yaml or json file."]) + expect(response).to have_http_status(200) + end end context "destroy" do