1
0
mirror of https://github.com/projekteuler/projekteuler.git synced 2026-01-26 18:18:51 +01:00

Show author names in problem and translation views

This commit is contained in:
2019-02-05 12:00:06 +01:00
parent ddb1cfac33
commit 89a8fbba99
14 changed files with 69 additions and 7 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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|

View File

@@ -7,6 +7,7 @@
<thead>
<tr>
<th><%= Translation.human_attribute_name(:created_at) %></th>
<th><%= Translation.human_attribute_name(:author) %></th>
<th><%= Translation.human_attribute_name(:problem_id) %></th>
<th><%= Translation.human_attribute_name(:title) %></th>
</tr>
@@ -16,6 +17,13 @@
<% @translations.each do |translation| %>
<tr>
<td><div data-toggle="tooltip" data-placement="left" title="<%= l translation.created_at %>"><%= time_ago_in_words(translation.created_at) %></div></td>
<td>
<% if translation.has_author? %>
<%= translation.author.name %>
<% else %>
<i><%= t('.anonymous') %></i>
<% end %>
</td>
<td><%= translation.problem_id %></td>
<td><%= link_to translation.title, [:admin, translation] %></td>
</tr>

View File

@@ -1,6 +1,9 @@
<% provide(:title, t('problems.show.problem_subtitle', id: @translation.problem_id)) %>
<div class="page-header">
<p class="text-muted">
<%= render 'shared/authors', authors: Array(@translation.author) %>
</p>
<h1><%= @translation.title %> <small><%= t 'problems.show.problem_subtitle', id: @translation.problem_id %></small></h1>
</div>

View File

@@ -1,6 +1,9 @@
<% provide(:title, t('problems.show.problem_subtitle', id: @problem.id)) %>
<div class="page-header">
<p class="text-muted">
<%= render 'shared/authors', authors: @problem.authors, has_anonymous_author: @problem.has_anonymous_author? %>
</p>
<h1><%= @problem.title %> <small><%= t '.problem_subtitle', id: @problem.id %></small></h1>
</div>
<%= link_to new_problem_translation_path(@problem), class: 'btn btn-default btn-sm pull-right' do %>

View File

@@ -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 %>