diff --git a/app/controllers/admin_controller.rb b/app/controllers/admin_controller.rb index 71752e9..f76d697 100644 --- a/app/controllers/admin_controller.rb +++ b/app/controllers/admin_controller.rb @@ -1,8 +1,3 @@ class AdminController < ApplicationController - before_action :authenticate! - - def authenticate! - authenticate_user! - throw(:warden) unless current_user.admin? - end + before_action :authenticate_admin! end \ No newline at end of file diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index d83690e..f274432 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -2,4 +2,9 @@ class ApplicationController < ActionController::Base # Prevent CSRF attacks by raising an exception. # For APIs, you may want to use :null_session instead. protect_from_forgery with: :exception + + def authenticate_admin! + authenticate_user! + throw(:warden) unless current_user.admin? + end end diff --git a/app/controllers/translations_controller.rb b/app/controllers/translations_controller.rb index 9a2ba7c..63495e9 100644 --- a/app/controllers/translations_controller.rb +++ b/app/controllers/translations_controller.rb @@ -1,5 +1,6 @@ class TranslationsController < ApplicationController before_action :set_problem, only: [:new, :create] + before_action :set_accept, only: [:create] # GET /translations/new def new @@ -17,7 +18,12 @@ class TranslationsController < ApplicationController @translation.author = current_user end if @translation.save - redirect_to @problem, notice: t('translations.notice.successfully_created') + if @accept + @problem.set_translation(@translation) + redirect_to @problem, notice: t('translations.notice.successfully_created_and_accepted') + else + redirect_to @problem, notice: t('translations.notice.successfully_created') + end else render :new end @@ -32,4 +38,12 @@ class TranslationsController < ApplicationController def set_problem @problem = Problem.find(params[:problem_id]) end + + def set_accept + if user_signed_in? and current_user.admin? + @accept = params.fetch(:accept, false) + else + @accept = false + end + end end diff --git a/app/views/translations/_form.html.erb b/app/views/translations/_form.html.erb index 243bba0..ce99b68 100644 --- a/app/views/translations/_form.html.erb +++ b/app/views/translations/_form.html.erb @@ -20,4 +20,7 @@ <% end %> <%= f.submit %> + <% if user_signed_in? and current_user.admin? %> + <%= f.button t('.save_and_accept'), type: :submit, name: "accept", value: true, class: "btn-success" %> + <% end %> <% end %> diff --git a/config/locales/views/translations/de.yml b/config/locales/views/translations/de.yml index 029cd7f..10f19b5 100644 --- a/config/locales/views/translations/de.yml +++ b/config/locales/views/translations/de.yml @@ -8,7 +8,9 @@ de: translation_source_explanation: Hier kommt der HTML-Quelltext der Übersetzung hin. copyright_warning_html: Sie erklären sich einverstanden, dass die Übersetzung unter der Creative Commons Lizenz CC BY-NC-SA 4.0 veröffentlicht wird. not_logged_in_warning: Wenn Sie wollen, dass die Übersetzung Ihnen zugeordnet werden kann, müssen Sie sich einloggen (oben rechts), andernfalls ist sie anonym. + save_and_accept: Übersetzung direkt setzen new: new_translation: Neue Übersetzung für Problem %{id} notice: - successfully_created: Der Übersetzungsvorschlag wurde eingereicht. Ein Organisator wird sich Ihren Vorschlag so bald wie möglich anschauen. \ No newline at end of file + successfully_created: Der Übersetzungsvorschlag wurde eingereicht. Ein Organisator wird sich Ihren Vorschlag so bald wie möglich anschauen. + successfully_created_and_accepted: Die Übersetzung wurde erfolgreich erstellt und akzeptiert. \ No newline at end of file diff --git a/test/controllers/translations_controller_test.rb b/test/controllers/translations_controller_test.rb index cc490b6..4e69f63 100644 --- a/test/controllers/translations_controller_test.rb +++ b/test/controllers/translations_controller_test.rb @@ -40,6 +40,28 @@ class TranslationsControllerTest < ActionDispatch::IntegrationTest assert_equal users(:translator), Translation.last.author end + test "should create and accept translation from admin" do + login_admin + assert_difference('Translation.count') do + post problem_translations_url(problem_id: 1, translation: @update, accept: true) + end + + assert_redirected_to problem_path(id: 1) + assert_equal users(:admin), Translation.last.author + assert_equal Problem.find(1).translation, Translation.last + end + + test "should create but not accept translation from normal user" do + login_translator + assert_difference('Translation.count') do + post problem_translations_url(problem_id: 1, translation: @update, accept: true) + end + + assert_redirected_to problem_path(id: 1) + assert_equal users(:translator), Translation.last.author + assert_not_equal Problem.find(1).translation, Translation.last + end + test "should not create incorrect translation" do assert_no_difference('Translation.count') do post problem_translations_url(problem_id: 1, translation: @incorrect)