From 38633d6e7919ad4fd654b42616defcead147a288 Mon Sep 17 00:00:00 2001 From: Philipp Fischbeck Date: Sat, 2 May 2020 20:18:04 +0200 Subject: [PATCH] Prefill original title and content from projecteuler.net --- Gemfile | 2 ++ Gemfile.lock | 14 ++++++++- app/controllers/admin/dashboard_controller.rb | 13 +++------ app/controllers/problems_controller.rb | 3 -- app/controllers/translations_controller.rb | 3 ++ app/jobs/pull_problem_content_job.rb | 21 ++++++++++++++ app/jobs/pull_problems_job.rb | 23 +++++++++++++++ app/views/admin/dashboard/index.html.erb | 13 +++++---- app/views/problems/index.html.erb | 8 ++--- app/views/problems/show.html.erb | 29 +++++++++++++++---- app/views/problems/untranslated.html.erb | 10 ------- .../_translation-tips.de.html.erb | 11 +++---- config/locales/views/admin/de.yml | 11 ++++--- config/locales/views/problems/de.yml | 6 ++-- config/routes.rb | 2 +- ...501095104_add_original_data_to_problems.rb | 7 +++++ db/schema.rb | 15 ++++++---- .../admin/dashboard_controller_test.rb | 15 ++++------ test/jobs/pull_problem_content_job_test.rb | 15 ++++++++++ test/jobs/pull_problems_job_test.rb | 26 +++++++++++++++++ test/test_helper.rb | 3 ++ 21 files changed, 178 insertions(+), 72 deletions(-) create mode 100644 app/jobs/pull_problem_content_job.rb create mode 100644 app/jobs/pull_problems_job.rb delete mode 100644 app/views/problems/untranslated.html.erb create mode 100644 db/migrate/20200501095104_add_original_data_to_problems.rb create mode 100644 test/jobs/pull_problem_content_job_test.rb create mode 100644 test/jobs/pull_problems_job_test.rb diff --git a/Gemfile b/Gemfile index 22e69a3..eba9e89 100644 --- a/Gemfile +++ b/Gemfile @@ -23,6 +23,8 @@ gem 'rails-i18n', '~> 6.0.0' gem 'rails-controller-testing' +gem 'webmock', group: :test + # Use jquery as the JavaScript library gem 'jquery-rails', '~> 4.3.5' # Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks diff --git a/Gemfile.lock b/Gemfile.lock index 18d0c18..d094c6d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -56,6 +56,8 @@ GEM minitest (~> 5.1) tzinfo (~> 1.1) zeitwerk (~> 2.2) + addressable (2.7.0) + public_suffix (>= 2.0.2, < 5.0) autoprefixer-rails (9.7.3) execjs bcrypt (3.1.13) @@ -80,6 +82,8 @@ GEM execjs coffee-script-source (1.12.2) concurrent-ruby (1.1.6) + crack (0.4.3) + safe_yaml (~> 1.0.0) crass (1.0.6) devise (4.7.1) bcrypt (~> 3.0) @@ -99,6 +103,7 @@ GEM sassc (>= 1.11) globalid (0.4.2) activesupport (>= 4.2.0) + hashdiff (1.0.1) hashie (4.1.0) i18n (1.8.2) concurrent-ruby (~> 1.0) @@ -146,6 +151,7 @@ GEM omniauth (~> 1.9) orm_adapter (0.5.0) popper_js (1.14.5) + public_suffix (4.0.4) rack (2.2.2) rack-test (1.1.0) rack (>= 1.0, < 3) @@ -187,6 +193,7 @@ GEM responders (3.0.0) actionpack (>= 5.0) railties (>= 5.0) + safe_yaml (1.0.5) sassc (2.2.1) ffi (~> 1.9) sassc-rails (2.1.2) @@ -226,6 +233,10 @@ GEM activemodel (>= 6.0.0) bindex (>= 0.4.0) railties (>= 6.0.0) + webmock (3.8.3) + addressable (>= 2.3.6) + crack (>= 0.3.2) + hashdiff (>= 0.4.0, < 2.0.0) websocket-driver (0.7.1) websocket-extensions (>= 0.1.0) websocket-extensions (0.1.4) @@ -262,8 +273,9 @@ DEPENDENCIES tzinfo-data uglifier (~> 4.2.0) web-console (~> 4.0.1) + webmock will_paginate (~> 3.3.0) will_paginate-bootstrap4 (~> 0.2.2) BUNDLED WITH - 2.0.2 + 2.1.4 diff --git a/app/controllers/admin/dashboard_controller.rb b/app/controllers/admin/dashboard_controller.rb index c049480..4634dba 100644 --- a/app/controllers/admin/dashboard_controller.rb +++ b/app/controllers/admin/dashboard_controller.rb @@ -1,16 +1,11 @@ class Admin::DashboardController < AdminController def index @current_problem_count = Problem.count + @most_recent_pull = Problem.maximum(:pulled_at) end - def update_problem_count - begin - new_problem_count = params[:problem_count].to_i - raise t('no_problem_count') unless new_problem_count - Problem.update_count(new_problem_count) - redirect_to({:controller => 'admin/dashboard', :action => :index}, notice: t('.success_message')) - rescue => e - redirect_to({:controller => 'admin/dashboard', :action => :index}, alert: t('.failure_message', error: e.message)) - end + def pull_problems + PullProblemsJob.perform_later + redirect_to({:controller => 'admin/dashboard', :action => :index}, notice: t('.pull_problems_initiated')) end end diff --git a/app/controllers/problems_controller.rb b/app/controllers/problems_controller.rb index f8b17dd..f1c7ca5 100644 --- a/app/controllers/problems_controller.rb +++ b/app/controllers/problems_controller.rb @@ -7,9 +7,6 @@ class ProblemsController < ApplicationController end def show - unless @problem.is_translated? - render action: "untranslated" - end end private diff --git a/app/controllers/translations_controller.rb b/app/controllers/translations_controller.rb index 63495e9..c708db2 100644 --- a/app/controllers/translations_controller.rb +++ b/app/controllers/translations_controller.rb @@ -8,6 +8,9 @@ class TranslationsController < ApplicationController if @problem.is_translated? @translation.title = @problem.translation.title @translation.content = @problem.translation.content + else + @translation.title = @problem.original_title + @translation.content = @problem.original_content end end diff --git a/app/jobs/pull_problem_content_job.rb b/app/jobs/pull_problem_content_job.rb new file mode 100644 index 0000000..779a8b5 --- /dev/null +++ b/app/jobs/pull_problem_content_job.rb @@ -0,0 +1,21 @@ +require 'open-uri' + +class PullProblemContentJob < ApplicationJob + queue_as :default + + def perform(problem) + html = URI.open("https://projecteuler.net/minimal=#{problem.id}").read + html.strip! + + # Linked problems + html.gsub!('<%= t('.administration') %> <%= link_to t('.view_translations'), admin_translations_path, class: 'btn btn-primary' %> - -

<%= t('.update_problem_count') %>

-<%= form_tag '/admin/update_problem_count', method: :post, class: 'form-inline' do %> - <%= number_field_tag 'problem_count', @current_problem_count, min: @current_problem_count, class: 'form-control' %> - <%= submit_tag t('.update'), class: 'btn btn-warning' %> -<% end %> \ No newline at end of file +

+ <%= t('.number_of_problems') %>: <%= @current_problem_count %>
+ <% if @most_recent_pull.present? %> + <%= t('.time_since_last_pull') %>: <%= time_ago_in_words @most_recent_pull %> + <% end %> +

+<%= button_to t('.pull_problems'), '/admin/pull_problems', method: :post, class: 'btn btn-warning' %> \ No newline at end of file diff --git a/app/views/problems/index.html.erb b/app/views/problems/index.html.erb index 2704c7d..19cd68b 100644 --- a/app/views/problems/index.html.erb +++ b/app/views/problems/index.html.erb @@ -28,12 +28,10 @@ <% if problem.is_translated? %> <%= link_to problem.title, problem %> <% else %> - <%= t 'problems.not_yet_translated' %> - <%= link_to new_problem_translation_path(problem), class: 'btn btn-primary btn-sm' do %> - <%= icon('fas', 'edit') %> <%= t '.suggest_translation' %> - <% end %> + <%= link_to problem do %> + <%= problem.original_title %> <%= t 'problems.not_translated_yet' %> + <% end %> <% end %> - <% end %> diff --git a/app/views/problems/show.html.erb b/app/views/problems/show.html.erb index 8fb6807..53948ca 100644 --- a/app/views/problems/show.html.erb +++ b/app/views/problems/show.html.erb @@ -1,7 +1,13 @@ <% provide(:title, t('problems.show.problem_subtitle', id: @problem.id)) %>
-

<%= @problem.title %>

+

+ <% if @problem.is_translated? %> + <%= @problem.title %> + <% else %> + <%= @problem.original_title %> <%= t 'problems.not_translated_yet' %> + <% end %> +

<% if Problem.exists?(@problem.id-1) %> <%= link_to problem_path(@problem.id-1), title: t('problems.show.problem_subtitle', id: @problem.id-1), class: 'problem-prev' do %> @@ -19,15 +25,26 @@
<%= link_to new_problem_translation_path(@problem), class: 'problem-buttons-inner btn btn-primary btn-sm' do %> - <%= icon('fas', 'edit') %> <%= t '.improve_translation' %> + <%= icon('fas', 'edit') %> + <% if @problem.is_translated? %> + <%= t '.improve_translation' %> + <% else %> + <%= t '.suggest_translation' %> + <% end %> <% end %>
- <%= sanitize @problem.content, scrubber: TranslationContentScrubber.new %> -
- + <% if @problem.is_translated? %> + + <% end %>
<%= link_to t('.view_original_problem'), @problem.original_url, target: '_blank' %> diff --git a/app/views/problems/untranslated.html.erb b/app/views/problems/untranslated.html.erb deleted file mode 100644 index f67ec01..0000000 --- a/app/views/problems/untranslated.html.erb +++ /dev/null @@ -1,10 +0,0 @@ -<% provide(:title, t('problems.show.problem_subtitle', id: @problem.id)) %> - -
-

<%= t 'problems.show.problem_subtitle', id: @problem.id %>

-
- -<%= t 'problems.not_yet_translated' %> -<%= link_to new_problem_translation_path(@problem), class: 'btn btn-primary btn-sm' do %> - <%= icon('fas', 'edit') %> <%= t 'problems.index.suggest_translation' %> -<% end %> \ No newline at end of file diff --git a/app/views/translations/_translation-tips.de.html.erb b/app/views/translations/_translation-tips.de.html.erb index e0bb687..4fcaf75 100644 --- a/app/views/translations/_translation-tips.de.html.erb +++ b/app/views/translations/_translation-tips.de.html.erb @@ -1,15 +1,12 @@ -
+
Hinweise zur Erstellung von Übersetzungen