diff --git a/app/controllers/better_together/uploads_controller.rb b/app/controllers/better_together/uploads_controller.rb index 98264a6a5..d9ae33b79 100644 --- a/app/controllers/better_together/uploads_controller.rb +++ b/app/controllers/better_together/uploads_controller.rb @@ -6,6 +6,10 @@ class UploadsController < FriendlyResourceController before_action :set_resource_instance, only: %i[show edit update destroy download] before_action :authorize_resource, only: %i[new show edit update destroy download] + def index + @total_size = policy_scope(Upload).sum(&:byte_size) + end + def download # rubocop:todo Metrics/AbcSize, Metrics/MethodLength if resource_instance.attached? # Trigger the background job to log the download diff --git a/app/helpers/better_together/uploads_helper.rb b/app/helpers/better_together/uploads_helper.rb index 2fd264ac5..29f80f135 100644 --- a/app/helpers/better_together/uploads_helper.rb +++ b/app/helpers/better_together/uploads_helper.rb @@ -3,5 +3,8 @@ module BetterTogether # helper methods for file uploads module UploadsHelper + def total_upload_size(uploads) + number_to_human_size(uploads.sum(&:byte_size)) + end end end diff --git a/app/views/better_together/uploads/index.html.erb b/app/views/better_together/uploads/index.html.erb index b10c9114b..1092a86a0 100644 --- a/app/views/better_together/uploads/index.html.erb +++ b/app/views/better_together/uploads/index.html.erb @@ -1,2 +1,8 @@ -

Files#index

-

Find me in app/views/better_together/files/index.html.erb

+<% content_for :page_title do %> + <%= resource_class.model_name.human.pluralize %> +<% end %> + +
+

<%= resource_class.model_name.human.pluralize %>

+

<%= t('.storage_usage', size: number_to_human_size(@total_size)) %>

+
diff --git a/config/locales/en.yml b/config/locales/en.yml index 0d0df6c58..66adee15d 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -698,6 +698,9 @@ en: contact_details: Contact details details: Details images: Images + uploads: + index: + storage_usage: "You're using %{size} of storage" contact_details: contact_information: Contact information title: Contact Details diff --git a/spec/helpers/better_together/uploads_helper_spec.rb b/spec/helpers/better_together/uploads_helper_spec.rb index f073ef34c..a8e8e5bad 100644 --- a/spec/helpers/better_together/uploads_helper_spec.rb +++ b/spec/helpers/better_together/uploads_helper_spec.rb @@ -2,20 +2,13 @@ require 'rails_helper' -# Specs in this file have access to a helper object that includes -# the UploadsHelper. For example: -# -# describe UploadsHelper do -# describe "string concat" do -# it "concats two strings with spaces" do -# expect(helper.concat_strings("this","that")).to eq("this that") -# end -# end -# end module BetterTogether - RSpec.describe UploadsHelper do - it 'exists' do - expect(described_class).to be # rubocop:todo RSpec/Be + RSpec.describe UploadsHelper, type: :helper do + describe '#total_upload_size' do + it 'returns human readable total size' do + uploads = [double(byte_size: 2.megabytes), double(byte_size: 3.megabytes)] + expect(helper.total_upload_size(uploads)).to eq '5 MB' + end end end end diff --git a/spec/views/better_together/files/index.html.erb_spec.rb b/spec/views/better_together/files/index.html.erb_spec.rb deleted file mode 100644 index 055402569..000000000 --- a/spec/views/better_together/files/index.html.erb_spec.rb +++ /dev/null @@ -1,11 +0,0 @@ -# frozen_string_literal: true - -require 'rails_helper' - -RSpec.describe 'files/index.html.erb' do - it 'works' do # rubocop:todo RSpec/ExampleWording - # rubocop:todo RSpec/IdenticalEqualityAssertion - expect(true).to be(true) # rubocop:todo RSpec/ExpectActual, RSpec/IdenticalEqualityAssertion - # rubocop:enable RSpec/IdenticalEqualityAssertion - end -end diff --git a/spec/views/better_together/uploads/index.html.erb_spec.rb b/spec/views/better_together/uploads/index.html.erb_spec.rb new file mode 100644 index 000000000..e4e5e7d5c --- /dev/null +++ b/spec/views/better_together/uploads/index.html.erb_spec.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe 'better_together/uploads/index.html.erb', type: :view do + it 'renders total storage usage' do + view.define_singleton_method(:resource_class) { BetterTogether::Upload } + assign(:uploads, []) + assign(:total_size, 3.megabytes) + + render + + expect(rendered).to include("You're using 3 MB of storage") + end +end