Skip to content

Commit

Permalink
Allow alternative .yaml file extension
Browse files Browse the repository at this point in the history
  • Loading branch information
westonganger committed Nov 1, 2024
1 parent 9ea8169 commit ccdeea7
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 9 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.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

Expand Down
20 changes: 12 additions & 8 deletions app/lib/rails_i18n_manager/forms/translation_file_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 41 additions & 1 deletion spec/request/translations_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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")

Expand All @@ -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": {
Expand All @@ -135,6 +154,7 @@ module RailsI18nManager
}
}
JSON_CONTENT

filename = Rails.root.join("tmp/#{SecureRandom.hex(6)}.json")
File.write(filename, json_content, mode: "wb")

Expand All @@ -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
Expand Down

0 comments on commit ccdeea7

Please sign in to comment.