Skip to content

Commit

Permalink
Merge pull request #345 from Trim/fix-diary-converter
Browse files Browse the repository at this point in the history
Fix diary converter
  • Loading branch information
Oumph authored Jan 29, 2023
2 parents c176575 + d61cf09 commit 368abcf
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 18 deletions.
9 changes: 7 additions & 2 deletions app/models/diary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class Diary < Content
length: { maximum: 100, message: "Le titre est trop long" }
validates :wiki_body, presence: { message: "Vous ne pouvez pas poster un journal vide" }

validate :convert_only_cc_licensed_diary, on: :convert

wikify_attr :body
truncate_attr :body

Expand Down Expand Up @@ -57,8 +59,12 @@ def destroyable_by?(account)
end

### Moving ###
def convert_only_cc_licensed_diary
errors.add :base, :cannot_convert, message: "Le journal n’a pas été publié sous licence CC By-SA 4.0, il ne peut donc pas être proposé en dépêche." unless node.cc_licensed?
end

def convert
validate!(:convert)
@news = News.new title: title,
wiki_body: "**TODO** insérer une synthèse du journal",
wiki_second_part: wiki_body,
Expand All @@ -68,9 +74,8 @@ def convert
@news.transaction do
@news.save!
$redis.set "convert/#{@news.id}", self.id
@news.node.update_column(:cc_licensed, true) if node.cc_licensed?
@news.node.update_column(:cc_licensed, node.cc_licensed)
@news.links.create title: "Journal à l’origine de la dépêche", url: "https://#{MY_DOMAIN}/users/#{owner.to_param}/journaux/#{to_param}", lang: "fr"
@news.submit! unless node.cc_licensed?
@news
end
end
Expand Down
22 changes: 6 additions & 16 deletions test/models/diary_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,13 @@ class DiaryTest < ActiveSupport::TestCase
assert_equal "draft", news.state;
end

test "convert copyrighted diary to news in moderation space" do
test "cannot convert non CC licensed diary to news" do
diary = diaries(:lorem_copyright);
diary.node = nodes(:diary_lorem_copyright);
created_news = diary.convert();
# Retrieve News from database to ensure it were saved correctly
news = News.find(created_news.id);
# Ensure convert work
assert_equal diary.title, news.title;
assert_equal "**TODO** insérer une synthèse du journal", news.versions.first().body;
assert_equal diary.wiki_body, news.versions.first().second_part;
assert_equal diary.owner.try(:name), news.author_name;
assert_equal diary.owner.try(:account).try(:email), news.author_email;
# Even if original diary were copyrighted, the created news is cc_licensed
assert_not_equal diary.node.cc_licensed, news.node.cc_licensed;
assert news.node.cc_licensed;
assert_equal sections(:default).id, news.section_id;
# As diary is not cc_licensed, news cannot be reworked collectively in the redaction space
assert_equal "candidate", news.state;
assert_raises(ActiveRecord::RecordInvalid) do
diary.convert
end
assert_equal diary.errors.details[:base].first[:error], :cannot_convert
assert diary.invalid?(:convert)
end
end

0 comments on commit 368abcf

Please sign in to comment.