Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce JSON response in controller #46

Merged
merged 11 commits into from
Nov 21, 2024
9 changes: 9 additions & 0 deletions app/assets/javascripts/moirai_translation_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ export default class MoiraiTranslationController extends Controller {
value: event.target.innerText
}
})
})
.then(response => response.json())
.then(data => {
if (data?.fallback_translation) {
event.target.innerText = data.fallback_translation
}
})
.catch(error => {
console.error('Error:', error);
});
}
}
30 changes: 29 additions & 1 deletion app/controllers/moirai/translation_files_controller.rb
CuddlyBunion341 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,17 @@ def handle_update(translation)
if translation_params[:value].blank? || translation_same_as_current?
translation.destroy
flash.notice = "Translation #{translation.key} was successfully deleted."
redirect_to_translation_file(translation.file_path)
respond_to do |format|
format.json do
flash.discard
render json: {
fallback_translation: get_fallback_translation
}
end
format.html do
redirect_to_translation_file(translation.file_path)
end
end
return
end

Expand All @@ -60,6 +70,15 @@ def handle_create
return
end

if translation_params[:value].blank? && request.format.json?
respond_to do |format|
format.json do
render json: {fallback_translation: get_fallback_translation}
end
end
return
end
CuddlyBunion341 marked this conversation as resolved.
Show resolved Hide resolved

translation = Translation.new(translation_params)

if translation.save
Expand Down Expand Up @@ -115,5 +134,14 @@ def translation_same_as_current?

translation_params[:value] == @file_handler.parse_file(file_paths.first)[translation_params[:key]]
end

def get_fallback_translation
CuddlyBunion341 marked this conversation as resolved.
Show resolved Hide resolved
file_paths = KeyFinder.new.file_paths_for(translation_params[:key], locale: translation_params[:locale])

return "" if file_paths.empty?
return "" unless file_paths.all? { |file_path| File.exist?(file_path) }

@file_handler.parse_file(file_paths.first)[translation_params[:key]]
end
end
end
12 changes: 12 additions & 0 deletions test/controllers/moirai/translation_files_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ class TranslationFilesControllerTest < ActionDispatch::IntegrationTest
assert_equal translation_count_before, Moirai::Translation.count
end

test "create translation empty json" do
CuddlyBunion341 marked this conversation as resolved.
Show resolved Hide resolved
post translation_files_url, params: {translation: {key: "locales.german", locale: "de", value: ""}}, as: :json
assert_response :ok
assert_equal "Deutsch", JSON.parse(response.body)["fallback_translation"]
end

# Update action tests
test "update translation with blank value" do
count_before = Moirai::Translation.count
Expand All @@ -74,6 +80,12 @@ class TranslationFilesControllerTest < ActionDispatch::IntegrationTest
assert_equal count_before - 1, Moirai::Translation.count
end

test "update translation with blank value json" do
CuddlyBunion341 marked this conversation as resolved.
Show resolved Hide resolved
post translation_files_url, params: {translation: {key: "locales.german", locale: "de", value: ""}}, as: :json
assert_response :ok
assert_equal "Deutsch", JSON.parse(response.body)["fallback_translation"]
end

test "update translation with non-blank new value" do
post translation_files_url, params: {translation: {key: "locales.german", locale: "de", value: "Hochdeutsch"}}

Expand Down
Loading