post replacements: rename <attr>_was to old_<attr>

Rename the following post replacement attributes:

* file_size_was -> old_file_size
* file_ext_was -> old_file_ext
* image_width_was -> old_image_width
* image_height_was -> old_image_height
* md5_was -> old_md5

In Rails 6.1, having attributes named `file_size` and `file_size_was` on
the same model breaks things because it conflicts with Rails' dirty
attribute tracking.
This commit is contained in:
evazion
2020-12-19 14:18:05 -06:00
parent 09e3146819
commit 4cb39422b2
7 changed files with 36 additions and 26 deletions

View File

@@ -11,17 +11,17 @@ class PostReplacement < ApplicationRecord
self.original_url = post.source
self.tags = post.tag_string + " " + self.tags.to_s
self.file_ext_was = post.file_ext
self.file_size_was = post.file_size
self.image_width_was = post.image_width
self.image_height_was = post.image_height
self.md5_was = post.md5
self.old_file_ext = post.file_ext
self.old_file_size = post.file_size
self.old_image_width = post.image_width
self.old_image_height = post.image_height
self.old_md5 = post.md5
end
concerning :Search do
class_methods do
def search(params = {})
q = search_attributes(params, :id, :created_at, :updated_at, :md5, :md5_was, :file_ext, :file_ext_was, :original_url, :replacement_url, :creator, :post)
q = search_attributes(params, :id, :created_at, :updated_at, :md5, :old_md5, :file_ext, :old_file_ext, :original_url, :replacement_url, :creator, :post)
q.apply_default_order(params)
end
end

View File

@@ -12,8 +12,8 @@ class PostReplacementPolicy < ApplicationPolicy
end
def permitted_attributes_for_update
[:file_ext_was, :file_size_was, :image_width_was, :image_height_was,
:md5_was, :file_ext, :file_size, :image_width, :image_height, :md5,
[:old_file_ext, :old_file_size, :old_image_width, :old_image_height,
:old_md5, :file_ext, :file_size, :image_width, :image_height, :md5,
:original_url, :replacement_url]
end
end

View File

@@ -29,10 +29,10 @@
</dl>
<% end %>
<% t.column "MD5" do |post_replacement| %>
<% if post_replacement.md5_was.present? && post_replacement.md5.present? %>
<% if post_replacement.old_md5.present? && post_replacement.md5.present? %>
<dl>
<dt>Original MD5</dt>
<dd><%= post_replacement.md5_was %></dd>
<dd><%= post_replacement.old_md5 %></dd>
<dt>Replacement MD5</dt>
<dd><%= post_replacement.md5 %></dd>
@@ -40,12 +40,12 @@
<% end %>
<% end %>
<% t.column "Size" do |post_replacement| %>
<% if %i[image_width_was image_height_was file_size_was file_ext_was image_width image_height file_size file_ext].all? { |k| post_replacement[k].present? } %>
<% if %i[old_image_width old_image_height old_file_size old_file_ext image_width image_height file_size file_ext].all? { |k| post_replacement[k].present? } %>
<dl>
<dt>Original Size</dt>
<dd>
<%= post_replacement.image_width_was %>x<%= post_replacement.image_height_was %>
(<%= post_replacement.file_size_was.to_s(:human_size, precision: 4) %>, <%= post_replacement.file_ext_was %>)
<%= post_replacement.old_image_width %>x<%= post_replacement.old_image_height %>
(<%= post_replacement.old_file_size.to_s(:human_size, precision: 4) %>, <%= post_replacement.old_file_ext %>)
</dd>
<dt>Replacement Size</dt>

View File

@@ -0,0 +1,9 @@
class RenamePostReplacementAttributes < ActiveRecord::Migration[6.1]
def change
rename_column :post_replacements, :file_ext_was, :old_file_ext
rename_column :post_replacements, :file_size_was, :old_file_size
rename_column :post_replacements, :image_width_was, :old_image_width
rename_column :post_replacements, :image_height_was, :old_image_height
rename_column :post_replacements, :md5_was, :old_md5
end
end

View File

@@ -2736,11 +2736,11 @@ CREATE TABLE public.post_replacements (
replacement_url text NOT NULL,
created_at timestamp without time zone NOT NULL,
updated_at timestamp without time zone NOT NULL,
file_ext_was character varying,
file_size_was integer,
image_width_was integer,
image_height_was integer,
md5_was character varying,
old_file_ext character varying,
old_file_size integer,
old_image_width integer,
old_image_height integer,
old_md5 character varying,
file_ext character varying,
file_size integer,
image_width integer,
@@ -7436,6 +7436,7 @@ INSERT INTO "schema_migrations" (version) VALUES
('20200803022359'),
('20200816175151'),
('20201201211748'),
('20201213052805');
('20201213052805'),
('20201219201007');

View File

@@ -47,14 +47,14 @@ class PostReplacementsControllerTest < ActionDispatch::IntegrationTest
format: :json,
id: @post_replacement.id,
post_replacement: {
file_size_was: 23,
old_file_size: 23,
file_size: 42
}
}
put_auth post_replacement_path(@post_replacement), @mod, params: params
assert_response :success
assert_equal(23, @post_replacement.reload.file_size_was)
assert_equal(23, @post_replacement.reload.old_file_size)
assert_equal(42, @post_replacement.file_size)
end
end

View File

@@ -287,11 +287,11 @@ class UploadServiceTest < ActiveSupport::TestCase
should "preserve the old values" do
as(@user) { subject.process! }
assert_equal(1500, @replacement.image_width_was)
assert_equal(1000, @replacement.image_height_was)
assert_equal(2000, @replacement.file_size_was)
assert_equal("jpg", @replacement.file_ext_was)
assert_equal(@old_md5, @replacement.md5_was)
assert_equal(1500, @replacement.old_image_width)
assert_equal(1000, @replacement.old_image_height)
assert_equal(2000, @replacement.old_file_size)
assert_equal("jpg", @replacement.old_file_ext)
assert_equal(@old_md5, @replacement.old_md5)
end
should "record the new values" do