Skip to content

Commit

Permalink
Switch to use gigabytes instead of gigibytes (#1798)
Browse files Browse the repository at this point in the history
* default base is now 1000

* added test to check for file size with 1000 base

* Added a few tests to make sure the base parameter is honored

Co-authored-by: Claudia Lee <[email protected]>

* Adjusted test to use KB instead of KiB

---------

Co-authored-by: Hector Correa <[email protected]>
Co-authored-by: Claudia Lee <[email protected]>
  • Loading branch information
3 people authored Apr 30, 2024
1 parent 0e5bb4f commit 410bed2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
17 changes: 17 additions & 0 deletions config/initializers/number_to_human_size_converter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true
# Source: https://github.com/rails/rails/issues/40054#issuecomment-674449143
# Additional information: https://massive.io/file-transfer/gb-vs-gib-whats-the-difference/
module ActiveSupport
module NumberHelper
class NumberToHumanSizeConverter < NumberConverter
private

# Allows a base to be specified for the conversion
# 1024 was the default and that produces gigibytes
# 1000 produces gigabytes
def base
options[:base] || 1000
end
end
end
end
14 changes: 14 additions & 0 deletions spec/models/s3_file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@
end
end

context "display file size" do
it "uses 1000 base by default when calculating display value" do
expect(s3_file.display_size).to eq "10.8 KB"
end
end

describe "#number_to_human_size" do
it "honors the base if we pass it one" do
expect(s3_file.number_to_human_size(size)).to eq "10.8 KB"
expect(s3_file.number_to_human_size(size, base: 1000)).to eq "10.8 KB"
expect(s3_file.number_to_human_size(size, base: 1024)).to eq "10.5 KB"
end
end

context "safe_id" do
it "calculates correct safe_id for files with spaces and non-alpha numeric characters" do
expect(s3_file.safe_id).to eq "10-99999-123-abc-#{work.id}-filename--with-spaces--w-----chars-txt"
Expand Down
2 changes: 1 addition & 1 deletion spec/system/work_upload_s3_objects_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
expect(page).to have_content upload_file_name
expect(page).to have_content filename1
expect(page).to have_content filename2
expect(page).to have_content "Total Size\n31.5 KB"
expect(page).to have_content "Total Size\n32.3 KB"
expect(work.reload.pre_curation_uploads.length).to eq(3)
end

Expand Down

0 comments on commit 410bed2

Please sign in to comment.