mirror of
https://github.com/projekteuler/projekteuler.git
synced 2025-12-10 08:46:41 +01:00
Fix #14: Make uniqueness check among other problems
This commit is contained in:
parent
0e9a1ce617
commit
c124808ffe
@ -2,7 +2,16 @@ class Translation < ActiveRecord::Base
|
|||||||
belongs_to :problem, inverse_of: :translations
|
belongs_to :problem, inverse_of: :translations
|
||||||
|
|
||||||
validates :title, :content, :problem_id, presence: true
|
validates :title, :content, :problem_id, presence: true
|
||||||
validates :title, uniqueness: true
|
validate :title_is_unique_among_other_problems
|
||||||
|
|
||||||
self.per_page = 50
|
self.per_page = 50
|
||||||
|
|
||||||
|
def title_is_unique_among_other_problems
|
||||||
|
Problem.where.not(id: problem_id).each do |problem|
|
||||||
|
if problem.is_translated? and problem.title == title
|
||||||
|
errors.add(:title, :taken)
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -20,6 +20,15 @@ class TranslationTest < ActiveSupport::TestCase
|
|||||||
assert_not translation.save
|
assert_not translation.save
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "should save translation with duplicate title of translation for same problem" do
|
||||||
|
translation = Translation.new(
|
||||||
|
title: translations(:translation_one).title,
|
||||||
|
content: 'This is some content',
|
||||||
|
problem_id: 1
|
||||||
|
)
|
||||||
|
assert translation.save
|
||||||
|
end
|
||||||
|
|
||||||
test "should save correct translation" do
|
test "should save correct translation" do
|
||||||
translation = Translation.new(
|
translation = Translation.new(
|
||||||
title: 'A unique title',
|
title: 'A unique title',
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user