post replacements: allow replacing with an uploaded file.
This commit is contained in:
@@ -23,6 +23,6 @@ class PostReplacementsController < ApplicationController
|
|||||||
|
|
||||||
private
|
private
|
||||||
def create_params
|
def create_params
|
||||||
params.require(:post_replacement).permit(:replacement_url, :final_source)
|
params.require(:post_replacement).permit(:replacement_url, :replacement_file, :final_source)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -4,8 +4,7 @@ class PostReplacement < ApplicationRecord
|
|||||||
belongs_to :post
|
belongs_to :post
|
||||||
belongs_to :creator, class_name: "User"
|
belongs_to :creator, class_name: "User"
|
||||||
before_validation :initialize_fields
|
before_validation :initialize_fields
|
||||||
attr_accessible :replacement_url, :final_source
|
attr_accessor :replacement_file, :final_source
|
||||||
attr_accessor :final_source
|
|
||||||
|
|
||||||
def initialize_fields
|
def initialize_fields
|
||||||
self.creator = CurrentUser.user
|
self.creator = CurrentUser.user
|
||||||
@@ -24,7 +23,7 @@ class PostReplacement < ApplicationRecord
|
|||||||
end
|
end
|
||||||
|
|
||||||
transaction do
|
transaction do
|
||||||
upload = Upload.create!(source: replacement_url, rating: post.rating, tag_string: post.tag_string)
|
upload = Upload.create!(file: replacement_file, source: replacement_url, rating: post.rating, tag_string: post.tag_string)
|
||||||
upload.process_upload
|
upload.process_upload
|
||||||
upload.update(status: "completed", post_id: post.id)
|
upload.update(status: "completed", post_id: post.id)
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
<%= format_text(WikiPage.titled(Danbooru.config.replacement_notice_wiki_page).first.try(&:body), ragel: true) %>
|
<%= 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| %>
|
<%= simple_form_for(post_replacement, url: post_replacements_path(post_id: post_replacement.post_id), method: :post) do |f| %>
|
||||||
|
<%= f.input :replacement_file, label: "File", as: :file %>
|
||||||
<%= 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 :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 } %>
|
<%= 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 %>
|
<% end %>
|
||||||
|
|||||||
@@ -191,6 +191,18 @@ class PostReplacementTest < ActiveSupport::TestCase
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "a post with an uploaded file" do
|
||||||
|
should "work" do
|
||||||
|
Tempfile.open do |file|
|
||||||
|
file.write(File.read("#{Rails.root}/test/files/test.png"))
|
||||||
|
file.seek(0)
|
||||||
|
|
||||||
|
@post.replace!(replacement_file: file, replacement_url: "")
|
||||||
|
assert_equal(@post.md5, Digest::MD5.file(file).hexdigest)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context "a post when given a final_source" do
|
context "a post when given a final_source" do
|
||||||
should "change the source to the final_source" do
|
should "change the source to the final_source" do
|
||||||
replacement_url = "http://data.tumblr.com/afed9f5b3c33c39dc8c967e262955de2/tumblr_orwwptNBCE1wsfqepo1_raw.png"
|
replacement_url = "http://data.tumblr.com/afed9f5b3c33c39dc8c967e262955de2/tumblr_orwwptNBCE1wsfqepo1_raw.png"
|
||||||
|
|||||||
Reference in New Issue
Block a user