From da0e80e9e0b599509f034e0246f8fd8011c1b0ab Mon Sep 17 00:00:00 2001 From: Philipp Fischbeck Date: Sat, 26 Oct 2019 15:06:34 +0200 Subject: [PATCH] Show translation diff for admins --- Gemfile | 4 +++ Gemfile.lock | 4 +++ app/assets/javascripts/mathjax-config.js | 3 ++- app/assets/stylesheets/application.css.scss | 27 +++++++++++++++++++ app/views/admin/translations/show.html.erb | 20 ++++++++++---- app/views/layouts/_header.html.erb | 11 ++++---- app/views/shared/_authors.de.html.erb | 2 +- config/locales/views/admin/de.yml | 1 + config/locales/views/application/de.yml | 5 ++-- db/seeds.rb | 29 +++++++++++---------- 10 files changed, 77 insertions(+), 29 deletions(-) diff --git a/Gemfile b/Gemfile index bef159b..c58fe8a 100644 --- a/Gemfile +++ b/Gemfile @@ -42,6 +42,10 @@ gem 'autoprefixer-rails', '~> 9.6.1' gem 'bh', '~> 1.3.6' +# Use Diffy for showing translation diffs +gem 'diffy' +gem 'diff-lcs' + gem 'will_paginate', '~> 3.1.8' gem 'will-paginate-i18n', '~> 0.1.15' gem 'will_paginate-bootstrap', '~> 1.0.2' diff --git a/Gemfile.lock b/Gemfile.lock index 780d8ea..beaec11 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -86,6 +86,8 @@ GEM railties (>= 4.1.0) responders warden (~> 1.2.3) + diff-lcs (1.3) + diffy (3.3.0) docile (1.3.2) erubi (1.8.0) execjs (2.7.0) @@ -252,6 +254,8 @@ DEPENDENCIES codacy-coverage coffee-rails (~> 5.0.0) devise (~> 4.7.1) + diff-lcs + diffy jbuilder (~> 2.9.1) jquery-rails (~> 4.3.5) mysql2 (~> 0.5.2) diff --git a/app/assets/javascripts/mathjax-config.js b/app/assets/javascripts/mathjax-config.js index 9dacc40..45f1478 100644 --- a/app/assets/javascripts/mathjax-config.js +++ b/app/assets/javascripts/mathjax-config.js @@ -3,7 +3,8 @@ window.MathJax = { tex2jax: { inlineMath: [ ["$","$"], ["\\(","\\)"] ], displayMath: [ ["$$","$$"], ["\\[","\\]"] ], - processEscapes: true + processEscapes: true, + ignoreClass: "mathjax_ignore|diff" }, "HTML-CSS": { availableFonts: ["TeX"] } }; \ No newline at end of file diff --git a/app/assets/stylesheets/application.css.scss b/app/assets/stylesheets/application.css.scss index 0ac4783..da8e9b4 100644 --- a/app/assets/stylesheets/application.css.scss +++ b/app/assets/stylesheets/application.css.scss @@ -55,6 +55,33 @@ body { margin: 20px -30px 0; } +/* Style for translation diffs */ + +.diff { + overflow:auto; + margin-bottom: 20px; + ul{background:#fff;overflow:auto;font-size:13px;list-style:none;margin:0;padding:0;display:table;width:100%;} + del, ins{display:block;text-decoration:none;} + li { + padding:0; + display:table-row; + margin: 0; + height:1em; + ins { + background:#dfd; + color:#080; + strong{font-weight:normal;background:#9f9;} + } + del { + background:#fee; + color:#b00; + strong{font-weight:normal;background:#fcc;} + } + /* try 'whitespace:pre;' if you don't want lines to wrap */ + del, ins, span {white-space:pre-wrap;font-family:courier;} + } +} + /* Custom css for fixing styles within problem content */ .problem-content { diff --git a/app/views/admin/translations/show.html.erb b/app/views/admin/translations/show.html.erb index efe40c1..7e5bd73 100644 --- a/app/views/admin/translations/show.html.erb +++ b/app/views/admin/translations/show.html.erb @@ -7,11 +7,6 @@

<%= @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' %> @@ -26,6 +21,21 @@ <%= sanitize @translation.content, scrubber: TranslationContentScrubber.new %> <% end %> + +<% if @translation.problem.is_translated? %> + <% if @translation.title != @translation.problem.title %> +

<%= t 'activerecord.attributes.translation.title' %>

+

<%= t '.changed_title_html', old: @translation.problem.title, new: @translation.title %>

+ <% end %> +

<%= t 'activerecord.attributes.translation.content' %>

+ <%= raw Diffy::Diff.new(@translation.problem.content, @translation.content, allow_empty_diff: false).to_s(:html) %> + +<% else %> +

<%= t 'activerecord.attributes.translation.content' %>

+ <%= raw Diffy::Diff.new(@translation.content, @translation.content, allow_empty_diff: false).to_s(:html) %> + +<% end %> +
<%= link_to t('problems.show.view_original_problem'), @translation.problem.original_url, target: '_blank' %>
diff --git a/app/views/layouts/_header.html.erb b/app/views/layouts/_header.html.erb index ea1d6e9..4964aa2 100644 --- a/app/views/layouts/_header.html.erb +++ b/app/views/layouts/_header.html.erb @@ -15,18 +15,17 @@ <% end %> <%= nav class: 'navbar-right' do %> <% if user_signed_in? %> - Eingeloggt als - <%= current_user.name %> + <%= t 'application.logged_in_as_html', name: current_user.name %> <% if current_user.admin? %> - <%= icon :star %> + <%= icon :star %> <% end %> - + <%= link_to(t('application.sign_out'), destroy_user_session_path, method: :delete) %> <% else %> <% if Rails.env.development? %> - <%= link_to(t('application.sign_in'), user_developer_omniauth_authorize_path) %> + <%= link_to(t('application.sign_in_with_github'), user_developer_omniauth_authorize_path) %> <% else %> - <%= link_to(t('application.sign_in'), user_github_omniauth_authorize_path) %> + <%= link_to(t('application.sign_in_with_github'), user_github_omniauth_authorize_path) %> <% end %> <% end %> <% end %> diff --git a/app/views/shared/_authors.de.html.erb b/app/views/shared/_authors.de.html.erb index af50f13..693b573 100644 --- a/app/views/shared/_authors.de.html.erb +++ b/app/views/shared/_authors.de.html.erb @@ -1,5 +1,5 @@ <% if authors.empty? %> - Diese Übersetung wurde anonym erstellt. + Diese Übersetzung wurde anonym erstellt. <% else %> Diese Übersetzung wurde von <% if local_assigns[:has_anonymous_author] %> diff --git a/config/locales/views/admin/de.yml b/config/locales/views/admin/de.yml index 5dd9125..ecf07fe 100644 --- a/config/locales/views/admin/de.yml +++ b/config/locales/views/admin/de.yml @@ -21,6 +21,7 @@ de: visit_current_translation: "Aktuelle Übersetzung anschauen" already_translated: "Dieses Problem wurde bereits übersetzt." is_new_translation: "Dieses Problem hat bisher keine Übersetzung." + changed_title_html: "Neuer Titel: %{new} statt %{old}" accept: success_message: "Übersetzung wurde erfolgreich akzeptiert!" decline: diff --git a/config/locales/views/application/de.yml b/config/locales/views/application/de.yml index 3f4896d..d4b0076 100644 --- a/config/locales/views/application/de.yml +++ b/config/locales/views/application/de.yml @@ -6,8 +6,9 @@ de: updated_at: Aktualisiert application: site_title: "Projekt Euler" - sign_in: 'Einloggen' - sign_out: 'Ausloggen' + sign_in_with_github: 'Mit GitHub anmelden' + logged_in_as_html: "Angemeldet als %{name}" + sign_out: 'Abmelden' info: "Info" privacy: "Datenschutz" legal: "Impressum" diff --git a/db/seeds.rb b/db/seeds.rb index 6a36b07..f7c26d6 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -10,13 +10,27 @@ Translation.delete_all Problem.delete_all User.delete_all +admin = User.create!( + provider: :developer, + uid: "admin", + name: "admin", + role: :admin +) -for i in 1..20 do +translator = User.create!( + provider: :developer, + uid: "translator", + name: "translator", + role: :user +) + +for i in 1..60 do Problem.create!(id: i) end for i in 1..10 do translation = Translation.create!( + author: translator, problem: Problem.find(i), title: "Problem Nummer #{i}", content: %Q(

Das hier ist der Inhalt von Problem #{i}. @@ -30,19 +44,6 @@ for i in 1..10 do end -User.create!( - provider: :developer, - uid: "admin", - name: "admin", - role: 1 -) - -User.create!( - provider: :developer, - uid: "translator", - name: "translator" -) - p "Created #{Problem.count} problems" p "Created #{Translation.count} translations" p "Created #{User.count} users"