From c27668d2ef9ef09813415dbbeed3d7bb3f618f96 Mon Sep 17 00:00:00 2001 From: evazion Date: Thu, 22 Jun 2017 15:19:41 -0500 Subject: [PATCH] post replacements: add option to fix source after replacement. Adds a "Final Source" field to the post replacement dialog. If specified, the post's source field will be changed to this value after replacement. This makes fixing the source back to the HTML page after replacement easier. --- app/controllers/post_replacements_controller.rb | 2 +- app/models/post_replacement.rb | 5 +++-- app/views/post_replacements/_new.html.erb | 3 ++- test/unit/post_replacement_test.rb | 10 ++++++++++ 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/app/controllers/post_replacements_controller.rb b/app/controllers/post_replacements_controller.rb index b9bd22984..d0c95cc8d 100644 --- a/app/controllers/post_replacements_controller.rb +++ b/app/controllers/post_replacements_controller.rb @@ -23,6 +23,6 @@ class PostReplacementsController < ApplicationController private def create_params - params.require(:post_replacement).permit(:replacement_url) + params.require(:post_replacement).permit(:replacement_url, :final_source) end end diff --git a/app/models/post_replacement.rb b/app/models/post_replacement.rb index 6eea498e5..1346f631c 100644 --- a/app/models/post_replacement.rb +++ b/app/models/post_replacement.rb @@ -4,7 +4,8 @@ class PostReplacement < ApplicationRecord belongs_to :post belongs_to :creator, class_name: "User" before_validation :initialize_fields - attr_accessible :replacement_url + attr_accessible :replacement_url, :final_source + attr_accessor :final_source def initialize_fields self.creator = CurrentUser.user @@ -37,7 +38,7 @@ class PostReplacement < ApplicationRecord post.image_width = upload.image_width post.image_height = upload.image_height post.file_size = upload.file_size - post.source = upload.source + post.source = final_source.presence || upload.source post.tag_string = upload.tag_string rescale_notes update_ugoira_frame_data(upload) diff --git a/app/views/post_replacements/_new.html.erb b/app/views/post_replacements/_new.html.erb index d226ca26d..a80dfce44 100644 --- a/app/views/post_replacements/_new.html.erb +++ b/app/views/post_replacements/_new.html.erb @@ -1,5 +1,6 @@ <%= format_text(WikiPage.titled(Danbooru.config.replacement_notice_wiki_page).first.try(&:body), ragel: true) %> <%= simple_form_for(post_replacement, url: post_replacements_path(post_id: post_replacement.post_id), method: :post) do |f| %> - <%= f.input :replacement_url, label: "New Source", input_html: { value: "" } %> + <%= f.input :replacement_url, label: "Replacement URL", hint: "The source URL to download the replacement from.", as: :string, input_html: { value: post_replacement.post.normalized_source } %> + <%= f.input :final_source, label: "Final Source", hint: "If present, the source field will be changed to this after replacement.", as: :string, input_html: { value: post_replacement.post.source } %> <% end %> diff --git a/test/unit/post_replacement_test.rb b/test/unit/post_replacement_test.rb index 23c48bd7e..bdc6017a2 100644 --- a/test/unit/post_replacement_test.rb +++ b/test/unit/post_replacement_test.rb @@ -190,5 +190,15 @@ class PostReplacementTest < ActiveSupport::TestCase assert(File.exists?(@post.large_file_path)) end end + + context "a post when given a final_source" do + should "change the source to the final_source" do + replacement_url = "http://data.tumblr.com/afed9f5b3c33c39dc8c967e262955de2/tumblr_orwwptNBCE1wsfqepo1_raw.png" + final_source = "https://noizave.tumblr.com/post/162094447052" + @post.replace!(replacement_url: replacement_url, final_source: final_source) + + assert_equal(final_source, @post.source) + end + end end end