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

Replace obsolete file size conversion gem with local code #2190

Merged
merged 1 commit into from
Aug 14, 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
2 changes: 0 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ end
# Use honeybadger for exception handling
gem 'honeybadger'

gem 'filesize'

gem 'newrelic_rpm', group: :production

# Use okcomputer to monitor the application
Expand Down
2 changes: 0 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ GEM
faraday (>= 1, < 3)
faraday-net_http (3.1.0)
net-http
filesize (0.2.0)
globalid (1.2.1)
activesupport (>= 6.1)
hashdiff (1.1.0)
Expand Down Expand Up @@ -400,7 +399,6 @@ DEPENDENCIES
factory_bot_rails (~> 6.4)
faraday (~> 2)
faraday-follow_redirects
filesize
high_voltage
honeybadger
importmap-rails (~> 2.0)
Expand Down
6 changes: 5 additions & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,18 @@
# available at http://guides.rubyonrails.org/i18n.html.

en:
hello: "Hello world"
restrictions:
not_accessible: 'This item cannot be accessed online. See Access conditions for more information.'
logged_in: 'Logged in.'
date:
formats:
sul: '%d-%b-%Y'
sul_was_long: '%d %B %Y'
number:
human:
storage_units:
units:
kb: kB # This is the SI unit for 10^3
viewers:
file:
title: 'File viewer'
Expand Down
2 changes: 1 addition & 1 deletion lib/embed/pretty_filesize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module PrettyFilesize
# @param [String] size the file size in bytes
# @return [String] a human readable representation in SI units
def pretty_filesize(size)
Filesize.new("#{size} B", Filesize::SI).pretty
SizeConverter.new(size, { base: 1000, precision: 2, significant: false }).convert
end
end
end
13 changes: 13 additions & 0 deletions lib/size_converter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

# Override Rails provided converter to handle SI units.
class SizeConverter < ActiveSupport::NumberHelper::NumberToHumanSizeConverter
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
2 changes: 1 addition & 1 deletion spec/features/file_viewer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

expect(page).to have_css('tr[data-tree-role="leaf"] a', text: 'Download')
expect(page).to have_css('*[data-tree-role="label"]', text: 'File1 Label')
expect(page).to have_css('td', text: '12.34 kB')
expect(page).to have_css('td', text: '12.35 kB')
end

context 'when the object has multiple files' do
Expand Down
7 changes: 1 addition & 6 deletions spec/lib/embed/pretty_filesize_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,7 @@
context 'kilobyte scale' do
let(:size) { 12_345 }

if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.4.0')
# Gaussian rounding: https://blog.heroku.com/ruby-2-4-features-hashes-integers-rounding#gaussian-rounding
it { is_expected.to eq '12.34 kB' }
else
it { is_expected.to eq '12.35 kB' }
end
it { is_expected.to eq '12.35 kB' }
end

context 'megabyte scale' do
Expand Down