Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Monkey-patch Rails number_to_human_size_converter. #621

Merged
merged 2 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
13 changes: 12 additions & 1 deletion spec/system/show_pdc_describe_record_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
it "renders total file size" do
visit '/catalog/doi-10-34770-bm4s-t361'
expect(page).to have_content("Total Size")
expect(page).to have_content("373 MB")
expect(page).to have_content("391 MB")
end
it "renders rights statement" do
visit '/catalog/doi-10-34770-bm4s-t361'
Expand All @@ -32,6 +32,17 @@
first_filename_spot = find(:css, '#files-table>tbody>tr:first-child>td', match: :first).text
expect(first_filename_spot).to eq("Fig11b_readme.hdf")
end

it "reports sizes using MB and KB" do
visit '/catalog/doi-10-34770-bm4s-t361'
# These tests are to validate our monkey-patched number_to_human_size (see number_to_human_size_converter.rb)
# is dividing the size by 1000 instead of using the Rails default of 1024.
file_size = find(:css, '#files-table>tbody>tr:first-child>td:nth-child(3)', match: :first).text
expect(file_size).to eq("22 KB")
file_size = find(:css, '#files-table>tbody>tr:nth-child(6)>td:nth-child(3)', match: :first).text
expect(file_size).to eq("32.7 MB")
end

it 'sorts files by name initially' do
visit '/catalog/doi-10-34770-bm4s-t361'
third_filename_spot = find(:xpath, "//table[@id='files-table']/tbody/tr[3]/td[1]").text
Expand Down