1
0
mirror of https://github.com/projekteuler/projekteuler.git synced 2025-12-10 08:46:41 +01:00

Allow admins to directly set translations

This commit is contained in:
Philipp Fischbeck 2019-02-23 13:49:25 +01:00
parent 1c2a73ba54
commit d56f0bf587
6 changed files with 49 additions and 8 deletions

View File

@ -1,8 +1,3 @@
class AdminController < ApplicationController class AdminController < ApplicationController
before_action :authenticate! before_action :authenticate_admin!
def authenticate!
authenticate_user!
throw(:warden) unless current_user.admin?
end
end end

View File

@ -2,4 +2,9 @@ class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception. # Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead. # For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception protect_from_forgery with: :exception
def authenticate_admin!
authenticate_user!
throw(:warden) unless current_user.admin?
end
end end

View File

@ -1,5 +1,6 @@
class TranslationsController < ApplicationController class TranslationsController < ApplicationController
before_action :set_problem, only: [:new, :create] before_action :set_problem, only: [:new, :create]
before_action :set_accept, only: [:create]
# GET /translations/new # GET /translations/new
def new def new
@ -17,7 +18,12 @@ class TranslationsController < ApplicationController
@translation.author = current_user @translation.author = current_user
end end
if @translation.save 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 else
render :new render :new
end end
@ -32,4 +38,12 @@ class TranslationsController < ApplicationController
def set_problem def set_problem
@problem = Problem.find(params[:problem_id]) @problem = Problem.find(params[:problem_id])
end end
def set_accept
if user_signed_in? and current_user.admin?
@accept = params.fetch(:accept, false)
else
@accept = false
end
end
end end

View File

@ -20,4 +20,7 @@
<% end %> <% end %>
</div> </div>
<%= f.submit %> <%= 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 %> <% end %>

View File

@ -8,7 +8,9 @@ de:
translation_source_explanation: Hier kommt der HTML-Quelltext der Übersetzung hin. 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 <a class="alert-link" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.de" target="_blank">CC BY-NC-SA 4.0</a> veröffentlicht wird. copyright_warning_html: Sie erklären sich einverstanden, dass die Übersetzung unter der Creative Commons Lizenz <a class="alert-link" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.de" target="_blank">CC BY-NC-SA 4.0</a> 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. 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:
new_translation: Neue Übersetzung für Problem %{id} new_translation: Neue Übersetzung für Problem %{id}
notice: notice:
successfully_created: Der Übersetzungsvorschlag wurde eingereicht. Ein Organisator wird sich Ihren Vorschlag so bald wie möglich anschauen. 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.

View File

@ -40,6 +40,28 @@ class TranslationsControllerTest < ActionDispatch::IntegrationTest
assert_equal users(:translator), Translation.last.author assert_equal users(:translator), Translation.last.author
end 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 test "should not create incorrect translation" do
assert_no_difference('Translation.count') do assert_no_difference('Translation.count') do
post problem_translations_url(problem_id: 1, translation: @incorrect) post problem_translations_url(problem_id: 1, translation: @incorrect)