From 89a8fbba99e823180ecf29b702bae56949dcac83 Mon Sep 17 00:00:00 2001 From: Philipp Fischbeck Date: Tue, 5 Feb 2019 12:00:06 +0100 Subject: [PATCH] Show author names in problem and translation views --- app/controllers/translations_controller.rb | 3 +++ app/models/problem.rb | 7 ++++++- app/models/translation.rb | 5 +++++ app/models/user.rb | 1 + app/views/admin/translations/index.html.erb | 8 ++++++++ app/views/admin/translations/show.html.erb | 3 +++ app/views/problems/show.html.erb | 3 +++ app/views/shared/_authors.de.html.erb | 11 +++++++++++ config/locales/models/translation/de.yml | 3 ++- config/locales/views/admin/de.yml | 2 ++ .../20190204164033_add_author_to_translations.rb | 5 +++++ db/schema.rb | 4 +++- test/controllers/translations_controller_test.rb | 12 +++++++++++- test/test_helper.rb | 9 ++++++--- 14 files changed, 69 insertions(+), 7 deletions(-) create mode 100644 app/views/shared/_authors.de.html.erb create mode 100644 db/migrate/20190204164033_add_author_to_translations.rb diff --git a/app/controllers/translations_controller.rb b/app/controllers/translations_controller.rb index 25a0eab..9a2ba7c 100644 --- a/app/controllers/translations_controller.rb +++ b/app/controllers/translations_controller.rb @@ -13,6 +13,9 @@ class TranslationsController < ApplicationController # POST /translations def create @translation = @problem.translations.new(translation_params) + if user_signed_in? + @translation.author = current_user + end if @translation.save redirect_to @problem, notice: t('translations.notice.successfully_created') else diff --git a/app/models/problem.rb b/app/models/problem.rb index 6a712f6..cb3b83d 100644 --- a/app/models/problem.rb +++ b/app/models/problem.rb @@ -4,7 +4,8 @@ class Problem < ApplicationRecord delegate :title, :content, to: :translation has_many :translations, inverse_of: :problem - + has_many :authors, -> { where(translations: { status: [:in_use, :outdated] }).distinct }, through: :translations + self.per_page = 50 def is_translated? @@ -19,6 +20,10 @@ class Problem < ApplicationRecord self.translation.in_use! end + def has_anonymous_author? + translations.where(status: [:in_use, :outdated], author: nil).any? + end + def original_url "https://projecteuler.net/problem=#{self.id}" end diff --git a/app/models/translation.rb b/app/models/translation.rb index 35be7a1..6e1971e 100644 --- a/app/models/translation.rb +++ b/app/models/translation.rb @@ -1,5 +1,6 @@ class Translation < ApplicationRecord belongs_to :problem, inverse_of: :translations + belongs_to :author, class_name: 'User', inverse_of: :translations, optional: true enum status: [:pending, :in_use, :outdated, :declined] validates :title, :content, :problem_id, presence: true @@ -7,6 +8,10 @@ class Translation < ApplicationRecord self.per_page = 50 + def has_author? + !!self.author + end + def title_is_unique_among_other_problems Problem.includes(:translation).where.not(id: problem_id).each do |problem| if problem.is_translated? and problem.title == title diff --git a/app/models/user.rb b/app/models/user.rb index 803a25c..42e62ca 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -2,6 +2,7 @@ class User < ApplicationRecord devise :omniauthable, :rememberable enum role: [:user, :admin] + has_many :translations, inverse_of: :author, dependent: :nullify def self.from_omniauth(auth) where(provider: auth.provider, uid: auth.uid).first_or_create do |user| diff --git a/app/views/admin/translations/index.html.erb b/app/views/admin/translations/index.html.erb index 0476261..447c7de 100644 --- a/app/views/admin/translations/index.html.erb +++ b/app/views/admin/translations/index.html.erb @@ -7,6 +7,7 @@ <%= Translation.human_attribute_name(:created_at) %> + <%= Translation.human_attribute_name(:author) %> <%= Translation.human_attribute_name(:problem_id) %> <%= Translation.human_attribute_name(:title) %> @@ -16,6 +17,13 @@ <% @translations.each do |translation| %>
<%= time_ago_in_words(translation.created_at) %>
+ + <% if translation.has_author? %> + <%= translation.author.name %> + <% else %> + <%= t('.anonymous') %> + <% end %> + <%= translation.problem_id %> <%= link_to translation.title, [:admin, translation] %> diff --git a/app/views/admin/translations/show.html.erb b/app/views/admin/translations/show.html.erb index 4e9e2a1..0c96aac 100644 --- a/app/views/admin/translations/show.html.erb +++ b/app/views/admin/translations/show.html.erb @@ -1,6 +1,9 @@ <% provide(:title, t('problems.show.problem_subtitle', id: @translation.problem_id)) %> diff --git a/app/views/problems/show.html.erb b/app/views/problems/show.html.erb index bcfbdd7..246f0bf 100644 --- a/app/views/problems/show.html.erb +++ b/app/views/problems/show.html.erb @@ -1,6 +1,9 @@ <% provide(:title, t('problems.show.problem_subtitle', id: @problem.id)) %> <%= link_to new_problem_translation_path(@problem), class: 'btn btn-default btn-sm pull-right' do %> diff --git a/app/views/shared/_authors.de.html.erb b/app/views/shared/_authors.de.html.erb new file mode 100644 index 0000000..af50f13 --- /dev/null +++ b/app/views/shared/_authors.de.html.erb @@ -0,0 +1,11 @@ +<% if authors.empty? %> + Diese Übersetung wurde anonym erstellt. +<% else %> + Diese Übersetzung wurde von + <% if local_assigns[:has_anonymous_author] %> + <%= (authors.map(&:name)+Array("anonymen Nutzern")).to_sentence %> + <% else %> + <%= authors.map(&:name).to_sentence %> + <% end %> + erstellt. +<% end %> \ No newline at end of file diff --git a/config/locales/models/translation/de.yml b/config/locales/models/translation/de.yml index 404edfe..2bfb901 100644 --- a/config/locales/models/translation/de.yml +++ b/config/locales/models/translation/de.yml @@ -11,4 +11,5 @@ de: id: ID problem_id: Problem-ID title: Titel - content: Inhalt \ No newline at end of file + content: Inhalt + author: Autor \ No newline at end of file diff --git a/config/locales/views/admin/de.yml b/config/locales/views/admin/de.yml index 2f1e4a9..fdd78ba 100644 --- a/config/locales/views/admin/de.yml +++ b/config/locales/views/admin/de.yml @@ -11,6 +11,8 @@ de: failure_message: "Problem-Anzahl konnte nicht aktualisiert werden! Grund: %{error}" no_problem_count: "Keine Problem-Anzahl gegeben!" translations: + index: + anonymous: "Anonym" must_be_pending: "Übersetzung muss ausstehend sein, um akzeptiert oder abgelehnt zu werden!" show: accept_translation: "Akzeptieren" diff --git a/db/migrate/20190204164033_add_author_to_translations.rb b/db/migrate/20190204164033_add_author_to_translations.rb new file mode 100644 index 0000000..1a36bd9 --- /dev/null +++ b/db/migrate/20190204164033_add_author_to_translations.rb @@ -0,0 +1,5 @@ +class AddAuthorToTranslations < ActiveRecord::Migration[5.2] + def change + add_reference :translations, :author, index: true, null: true, foreign_key: {to_table: :users} + end +end \ No newline at end of file diff --git a/db/schema.rb b/db/schema.rb index 1d5881a..0be06c1 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: 2019_02_03_164629) do +ActiveRecord::Schema.define(version: 2019_02_04_164033) do create_table "problems", force: :cascade do |t| t.datetime "created_at" @@ -26,6 +26,8 @@ ActiveRecord::Schema.define(version: 2019_02_03_164629) do t.datetime "updated_at" t.integer "problem_id" t.integer "status", default: 0 + t.integer "author_id" + t.index ["author_id"], name: "index_translations_on_author_id" t.index ["problem_id"], name: "index_translations_on_problem_id" end diff --git a/test/controllers/translations_controller_test.rb b/test/controllers/translations_controller_test.rb index 6f165b4..cc490b6 100644 --- a/test/controllers/translations_controller_test.rb +++ b/test/controllers/translations_controller_test.rb @@ -22,7 +22,7 @@ class TranslationsControllerTest < ActionDispatch::IntegrationTest assert_response :success end - test "should create translation" do + test "should create translation anonymously" do assert_difference('Translation.count') do post problem_translations_url(problem_id: 1, translation: @update) end @@ -30,6 +30,16 @@ class TranslationsControllerTest < ActionDispatch::IntegrationTest assert_redirected_to problem_path(id: 1) end + test "should create translation with user" do + login_translator + assert_difference('Translation.count') do + post problem_translations_url(problem_id: 1, translation: @update) + end + + assert_redirected_to problem_path(id: 1) + assert_equal users(:translator), Translation.last.author + end + test "should not create incorrect translation" do assert_no_difference('Translation.count') do post problem_translations_url(problem_id: 1, translation: @incorrect) diff --git a/test/test_helper.rb b/test/test_helper.rb index 4940770..e8d4041 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -10,6 +10,12 @@ class ActiveSupport::TestCase fixtures :all # Add more helper methods to be used by all tests here... +end + + +class ActionDispatch::IntegrationTest + include Devise::Test::IntegrationHelpers + def login_admin admin = users(:admin) sign_in admin @@ -19,10 +25,7 @@ class ActiveSupport::TestCase translator = users(:translator) sign_in translator end -end - -class ActionDispatch::IntegrationTest OmniAuth.config.test_mode = true OmniAuth.config.mock_auth[:github] = OmniAuth::AuthHash.new({ provider: :github,