From 8da1323fd80d4387f2f49e364d5fc6f644c0110e Mon Sep 17 00:00:00 2001 From: Philipp Fischbeck Date: Sat, 2 Feb 2019 13:09:01 +0100 Subject: [PATCH 1/4] Add status to translations --- app/controllers/translations_controller.rb | 14 ++++---------- app/models/problem.rb | 8 ++++++++ app/models/translation.rb | 1 + config/locales/views/application/de.yml | 6 +++++- config/locales/views/translations/de.yml | 2 +- db/schema.rb | 3 ++- db/seeds.rb | 2 +- 7 files changed, 22 insertions(+), 14 deletions(-) diff --git a/app/controllers/translations_controller.rb b/app/controllers/translations_controller.rb index f3b0e70..25a0eab 100644 --- a/app/controllers/translations_controller.rb +++ b/app/controllers/translations_controller.rb @@ -11,18 +11,12 @@ class TranslationsController < ApplicationController end # POST /translations - # POST /translations.json def create @translation = @problem.translations.new(translation_params) - - respond_to do |format| - if @translation.save - format.html { redirect_to @problem, notice: t('translations.notice.successfully_created') } - format.json { render :show, status: :created, location: @translation } - else - format.html { render :new } - format.json { render json: @translation.errors, status: :unprocessable_entity } - end + if @translation.save + redirect_to @problem, notice: t('translations.notice.successfully_created') + else + render :new end end diff --git a/app/models/problem.rb b/app/models/problem.rb index a6ba173..e281d17 100644 --- a/app/models/problem.rb +++ b/app/models/problem.rb @@ -11,6 +11,14 @@ class Problem < ApplicationRecord !!self.translation end + def set_translation(translation) + if self.is_translated? + self.translation.outdated! + end + self.translation = translation + self.translation.in_use! + end + def original_url "https://projecteuler.net/problem=#{self.id}" end diff --git a/app/models/translation.rb b/app/models/translation.rb index e4695b6..35be7a1 100644 --- a/app/models/translation.rb +++ b/app/models/translation.rb @@ -1,5 +1,6 @@ class Translation < ApplicationRecord belongs_to :problem, inverse_of: :translations + enum status: [:pending, :in_use, :outdated, :declined] validates :title, :content, :problem_id, presence: true validate :title_is_unique_among_other_problems diff --git a/config/locales/views/application/de.yml b/config/locales/views/application/de.yml index 2340f66..550a87e 100644 --- a/config/locales/views/application/de.yml +++ b/config/locales/views/application/de.yml @@ -8,4 +8,8 @@ de: info: "Info" legal: "Impressum" copyright: "Copyright-Informationen" - bootstrap_html: 'Entworfen mit Bootstrap' \ No newline at end of file + bootstrap_html: 'Entworfen mit Bootstrap' + helpers: + submit: + translation: + create: "%{model} vorschlagen" diff --git a/config/locales/views/translations/de.yml b/config/locales/views/translations/de.yml index 69ea5f2..df84c5e 100644 --- a/config/locales/views/translations/de.yml +++ b/config/locales/views/translations/de.yml @@ -9,4 +9,4 @@ de: new: new_translation: Neue Übersetzung für Problem %{id} notice: - successfully_created: Übersetzung wurde erfolgreich erstellt. \ No newline at end of file + successfully_created: Der Übersetzungsvorschlag wurde eingereicht. Ein Organisator wird sich Ihren Vorschlag so bald wie möglich anschauen. \ No newline at end of file diff --git a/db/schema.rb b/db/schema.rb index 3475fce..f50f95e 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20150131103802) do +ActiveRecord::Schema.define(version: 2019_02_02_113250) do create_table "admins", force: :cascade do |t| t.string "email", default: "", null: false @@ -42,6 +42,7 @@ ActiveRecord::Schema.define(version: 20150131103802) do t.datetime "created_at" t.datetime "updated_at" t.integer "problem_id" + t.integer "status", default: 0 t.index ["problem_id"], name: "index_translations_on_problem_id" end diff --git a/db/seeds.rb b/db/seeds.rb index 452dab4..0986997 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -27,7 +27,7 @@ for i in 1..10 do ) problem = Problem.find(i) - problem.translation = translation + problem.set_translation(translation) problem.save! end From 0dd3092c4c75b1da7c36b47043b44fadcde5f544 Mon Sep 17 00:00:00 2001 From: Philipp Fischbeck Date: Sat, 2 Feb 2019 13:19:20 +0100 Subject: [PATCH 2/4] Add tests for status change --- test/fixtures/translations.yml | 8 ++++++++ test/models/problem_test.rb | 6 ++++++ test/models/translation_test.rb | 1 + 3 files changed, 15 insertions(+) diff --git a/test/fixtures/translations.yml b/test/fixtures/translations.yml index f8bcb86..2122ecd 100644 --- a/test/fixtures/translations.yml +++ b/test/fixtures/translations.yml @@ -4,8 +4,16 @@ translation_one: problem_id: 1 title: First title content: The content of the translation + status: 1 translation_two: problem_id: 2 title: Second title content: The content of the second translation + status: 1 + +translation_two_alternative: + problem_id: 2 + title: Second title + content: The changed content for the second problem + status: 0 diff --git a/test/models/problem_test.rb b/test/models/problem_test.rb index b203bed..44bc6b5 100644 --- a/test/models/problem_test.rb +++ b/test/models/problem_test.rb @@ -14,6 +14,12 @@ class ProblemTest < ActiveSupport::TestCase assert problems(:one).is_translated? end + test "set_translation should modify status correctly" do + problems(:two).set_translation(translations(:translation_two_alternative)) + assert translations(:translation_two).outdated? + assert translations(:translation_two_alternative).in_use? + end + test "should have correct original url" do assert_equal "https://projecteuler.net/problem=1", problems(:one).original_url end diff --git a/test/models/translation_test.rb b/test/models/translation_test.rb index 3deb421..38233bd 100644 --- a/test/models/translation_test.rb +++ b/test/models/translation_test.rb @@ -36,5 +36,6 @@ class TranslationTest < ActiveSupport::TestCase problem_id: 1 ) assert translation.save + assert translation.pending? end end From 3a2aa6ea1a98bbeca7cb8e3332bca7ae74557b63 Mon Sep 17 00:00:00 2001 From: Philipp Fischbeck Date: Sat, 2 Feb 2019 17:53:02 +0100 Subject: [PATCH 3/4] Add ability to accept or decline translations --- ...slations.coffee => translations.js.coffee} | 3 +++ .../admin/translations_controller.rb | 20 ++++++++++++++----- app/models/problem.rb | 2 +- app/views/admin/translations/index.html.erb | 6 ++++-- app/views/admin/translations/show.html.erb | 14 +++++++++++++ config/locales/views/admin/de.yml | 14 ++++++++++++- config/locales/views/application/de.yml | 3 +++ config/routes.rb | 6 +++++- ...190202113250_add_status_to_translations.rb | 15 ++++++++++++++ .../admin/translations_controller_test.rb | 14 ++++++++++++- test/models/problem_test.rb | 1 + 11 files changed, 87 insertions(+), 11 deletions(-) rename app/assets/javascripts/admin/{translations.coffee => translations.js.coffee} (73%) create mode 100644 db/migrate/20190202113250_add_status_to_translations.rb diff --git a/app/assets/javascripts/admin/translations.coffee b/app/assets/javascripts/admin/translations.js.coffee similarity index 73% rename from app/assets/javascripts/admin/translations.coffee rename to app/assets/javascripts/admin/translations.js.coffee index 24f83d1..42cfbbe 100644 --- a/app/assets/javascripts/admin/translations.coffee +++ b/app/assets/javascripts/admin/translations.js.coffee @@ -1,3 +1,6 @@ # Place all the behaviors and hooks related to the matching controller here. # All this logic will automatically be available in application.js. # You can use CoffeeScript in this file: http://coffeescript.org/ + +$(document).on "turbolinks:load", -> + $('[data-toggle="tooltip"]').tooltip() \ No newline at end of file diff --git a/app/controllers/admin/translations_controller.rb b/app/controllers/admin/translations_controller.rb index db013f0..aef7ebc 100644 --- a/app/controllers/admin/translations_controller.rb +++ b/app/controllers/admin/translations_controller.rb @@ -1,18 +1,28 @@ class Admin::TranslationsController < AdminController - before_action :set_translation, only: :show + before_action :set_translation, only: [:show, :accept, :decline] # GET /translations - # GET /translations.json def index - @translations = Translation.paginate(page: params[:page]) + @translations = Translation.pending.order(created_at: :desc).paginate(page: params[:page]) end # GET /translations/1 - # GET /translations/1.json def show end + def accept + raise t('.must_be_pending') unless @translation.pending? + @translation.problem.set_translation(@translation) + redirect_to @translation.problem, notice: t('.success_message') + end + + def decline + raise t('.must_be_pending') unless @translation.pending? + @translation.declined! + redirect_to admin_translations_path, notice: t('.success_message') + end + def set_translation - @translation = Translation.find(params[:id]) + @translation = Translation.find(params[:translation_id]) end end diff --git a/app/models/problem.rb b/app/models/problem.rb index e281d17..6a712f6 100644 --- a/app/models/problem.rb +++ b/app/models/problem.rb @@ -15,7 +15,7 @@ class Problem < ApplicationRecord if self.is_translated? self.translation.outdated! end - self.translation = translation + self.update(translation: translation) self.translation.in_use! end diff --git a/app/views/admin/translations/index.html.erb b/app/views/admin/translations/index.html.erb index 9c17d93..0476261 100644 --- a/app/views/admin/translations/index.html.erb +++ b/app/views/admin/translations/index.html.erb @@ -6,7 +6,8 @@ - + + @@ -14,7 +15,8 @@ <% @translations.each do |translation| %> - + + <% end %> diff --git a/app/views/admin/translations/show.html.erb b/app/views/admin/translations/show.html.erb index 72febdf..4e9e2a1 100644 --- a/app/views/admin/translations/show.html.erb +++ b/app/views/admin/translations/show.html.erb @@ -4,6 +4,20 @@

<%= @translation.title %> <%= t 'problems.show.problem_subtitle', id: @translation.problem_id %>

+<% if @translation.problem.is_translated? %> + +<% else %> + +<% end %> +<% if @translation.pending? %> + <%= link_to admin_translation_decline_path(@translation), method: :post, class: 'btn btn-default btn-sm pull-right' do %> + <%= icon :remove %> <%= t '.decline_translation' %> + <% end %> + <%= link_to admin_translation_accept_path(@translation), method: :post, class: 'btn btn-default btn-sm pull-right' do %> + <%= icon :ok %> <%= t '.accept_translation' %> + <% end %> +<% end %> + <%= panel do %>
<%= sanitize @translation.content %> diff --git a/config/locales/views/admin/de.yml b/config/locales/views/admin/de.yml index 4a41b80..2f1e4a9 100644 --- a/config/locales/views/admin/de.yml +++ b/config/locales/views/admin/de.yml @@ -9,4 +9,16 @@ de: update_problem_count: success_message: "Problem-Anzahl wurde erfolgreich aktualisiert!" failure_message: "Problem-Anzahl konnte nicht aktualisiert werden! Grund: %{error}" - no_problem_count: "Keine Problem-Anzahl gegeben!" \ No newline at end of file + no_problem_count: "Keine Problem-Anzahl gegeben!" + translations: + must_be_pending: "Übersetzung muss ausstehend sein, um akzeptiert oder abgelehnt zu werden!" + show: + accept_translation: "Akzeptieren" + decline_translation: "Ablehnen" + visit_current_translation: "Aktuelle Übersetzung anschauen" + already_translated: "Dieses Problem wurde bereits übersetzt." + is_new_translation: "Dieses Problem hat bisher keine Übersetzung." + accept: + success_message: "Übersetzung wurde erfolgreich akzeptiert!" + decline: + success_message: "Übersetzung wurde erfolgreich abgelehnt!" \ No newline at end of file diff --git a/config/locales/views/application/de.yml b/config/locales/views/application/de.yml index 550a87e..cccda7c 100644 --- a/config/locales/views/application/de.yml +++ b/config/locales/views/application/de.yml @@ -1,6 +1,9 @@ # ruby encoding: utf-8 de: + attributes: + created_at: Erstellt + updated_at: Aktualisiert application: site_title: "Projekt Euler" sign_in: 'Einloggen' diff --git a/config/routes.rb b/config/routes.rb index 49fc368..cb13527 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -26,6 +26,10 @@ Rails.application.routes.draw do namespace :admin do get '', to: 'dashboard#index', as: 'dashboard_index' post '/update_problem_count', to: 'dashboard#update_problem_count', as: 'dashboard_update_problem_count' - resources :translations, only: [:index, :show] + resources :translations, only: [:index] do + get '', to: 'translations#show', as: '' + post 'accept', to: 'translations#accept' + post 'decline', to: 'translations#decline' + end end end \ No newline at end of file diff --git a/db/migrate/20190202113250_add_status_to_translations.rb b/db/migrate/20190202113250_add_status_to_translations.rb new file mode 100644 index 0000000..86583d5 --- /dev/null +++ b/db/migrate/20190202113250_add_status_to_translations.rb @@ -0,0 +1,15 @@ +class AddStatusToTranslations < ActiveRecord::Migration[5.2] + def change + add_column :translations, :status, :integer, default: 0 + + reversible do |dir| + dir.up do + Problem.all.each do |problem| + if problem.is_translated? + problem.translation.in_use! + end + end + end + end + end +end diff --git a/test/controllers/admin/translations_controller_test.rb b/test/controllers/admin/translations_controller_test.rb index 31bc4d0..4536ae0 100644 --- a/test/controllers/admin/translations_controller_test.rb +++ b/test/controllers/admin/translations_controller_test.rb @@ -6,6 +6,7 @@ class Admin::TranslationsControllerTest < ActionDispatch::IntegrationTest setup do login @translation = translations(:translation_one) + @translation_alternative = translations(:translation_two_alternative) end test "should get index" do get admin_translations_url @@ -14,8 +15,19 @@ class Admin::TranslationsControllerTest < ActionDispatch::IntegrationTest end test "should show translation" do - get admin_translation_url(id: @translation) + get admin_translation_url(translation_id: @translation) assert_response :success end + test "should accept translation" do + post admin_translation_accept_path(@translation_alternative) + assert_redirected_to problem_path(2) + assert_equal @translation_alternative, Problem.find(2).translation + end + + test "should decline translation" do + post admin_translation_decline_path(@translation_alternative) + assert_redirected_to admin_translations_path + assert Translation.find(@translation_alternative.id).declined? + end end diff --git a/test/models/problem_test.rb b/test/models/problem_test.rb index 44bc6b5..eff3db1 100644 --- a/test/models/problem_test.rb +++ b/test/models/problem_test.rb @@ -18,6 +18,7 @@ class ProblemTest < ActiveSupport::TestCase problems(:two).set_translation(translations(:translation_two_alternative)) assert translations(:translation_two).outdated? assert translations(:translation_two_alternative).in_use? + assert_equal translations(:translation_two_alternative), problems(:two).translation end test "should have correct original url" do From c6a4757bc14cf5bee32e7cbbe410fb9703761641 Mon Sep 17 00:00:00 2001 From: Philipp Fischbeck Date: Sat, 2 Feb 2019 19:13:31 +0100 Subject: [PATCH 4/4] Improve links on index page --- app/views/about/index.de.html.erb | 11 +++++------ app/views/layouts/application.html.erb | 12 ++++++------ 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/app/views/about/index.de.html.erb b/app/views/about/index.de.html.erb index 6dd18b4..b18873f 100644 --- a/app/views/about/index.de.html.erb +++ b/app/views/about/index.de.html.erb @@ -14,7 +14,7 @@

Ansehen

-

Bisher wurden leider erst <%= Problem.translated_count %> der <%= Problem.count %> Probleme übersetzt, es gibt also noch einiges zu tun!

+

Sehen Sie sich die mathematischen Probleme in deutscher Sprache an.

<%= link_to problems_path, class: 'btn btn-default' do %> Zu den Problemen » @@ -22,14 +22,13 @@

-

Erweitern

-

Sie haben in Zukunft auch die Möglichkeit, eigene Übersetzungen vorzuschlagen. An diesem Feature wird aber zurzeit noch gearbeitet.

-

Übersetzung vorschlagen »

+

Übersetzen

+

Bisher wurden erst <%= Problem.translated_count %> der <%= Problem.count %> Probleme übersetzt. Helfen Sie mit, Übersetzungen zu erstellen und anzupassen!

Verbessern

-

Haben Sie Verbesserungsvorschläge für eine der Übersetzungen? Fehlt Ihnen eine Funktion auf der Webseite, oder ist Ihnen ein Fehler aufgefallen? Dann schreiben Sie uns!

-

Zum Kontaktformular »

+

Vermissen Sie eine Funktion auf der Webseite, oder ist Ihnen ein Fehler aufgefallen? Dann helfen Sie beim Entwickeln der Webseite in Ruby on Rails!

+

Projekt Euler auf GitHub »

\ No newline at end of file diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 502bbac..b610e73 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -20,15 +20,15 @@ <%= csrf_meta_tags %> -<%= render 'layouts/header' %> -
- <%= flash_messages %> -
-
+
+ <%= render 'layouts/header' %> +
+ <%= flash_messages %> +
<%= yield %>
-
+ <%= render 'layouts/footer' %>
<%= Translation.human_attribute_name(:id) %><%= Translation.human_attribute_name(:created_at) %><%= Translation.human_attribute_name(:problem_id) %> <%= Translation.human_attribute_name(:title) %>
<%= translation.id %>
<%= time_ago_in_words(translation.created_at) %>
<%= translation.problem_id %> <%= link_to translation.title, [:admin, translation] %>