Skip to content

Commit

Permalink
Introduce JSON response in controller (#46)
Browse files Browse the repository at this point in the history
* Refactor translation files controller

Revert "Update json tests"

This reverts commit 7ff1ffdc7e718c75a7297cbdda699bd40c6f81e2.

Refactor controller

Fix specs

Fix linter

Add api test

Update translation_files_controller

Fix linter

* Update stimulus controller

* Revert "Refactor translation files controller"

This reverts commit a8b6bd9.

* Merge

* Implement feature

* Fix bug

* Refactor

* Update test/controllers/moirai/translation_files_controller_test.rb

* Update test/controllers/moirai/translation_files_controller_test.rb

* Update app/controllers/moirai/translation_files_controller.rb

* Do not discard flash message

---------

Co-authored-by: Alessandro Rodi <[email protected]>
  • Loading branch information
CuddlyBunion341 and coorasse authored Nov 21, 2024
1 parent 2fd7204 commit 5002661
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
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);
});
}
}
26 changes: 24 additions & 2 deletions app/controllers/moirai/translation_files_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,17 @@ def open_pr
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
render json: {
fallback_translation: get_fallback_translation
}
end
format.html do
flash.notice = "Translation #{translation.key} was successfully deleted."
redirect_to_translation_file(translation.file_path)
end
end
return
end

Expand All @@ -60,6 +69,10 @@ def handle_create
return
end

if translation_params[:value].blank? && request.format.json?
return render json: {fallback_translation: get_fallback_translation}
end

translation = Translation.new(translation_params)

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

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

def get_fallback_translation
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 with blank value JSON" do
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
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

0 comments on commit 5002661

Please sign in to comment.