Skip to content

Commit

Permalink
Monkey-patch Rails number_to_human_size_converter. (#621)
Browse files Browse the repository at this point in the history
* Monkey-patch Rails number_to_human_size_converter.

* Rubocop nit-picking
  • Loading branch information
hectorcorrea authored May 1, 2024
1 parent eb926bb commit 5ca7bfa
Showing 2 changed files with 29 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
13 changes: 12 additions & 1 deletion spec/system/show_pdc_describe_record_spec.rb
Original file line number Diff line number Diff line change
@@ -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'
@@ -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

0 comments on commit 5ca7bfa

Please sign in to comment.