Skip to content

Commit 15e7d7b

Browse files
committed
Show total upload size
1 parent d34cf20 commit 15e7d7b

File tree

7 files changed

+38
-23
lines changed

7 files changed

+38
-23
lines changed

app/controllers/better_together/uploads_controller.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ class UploadsController < FriendlyResourceController
66
before_action :set_resource_instance, only: %i[show edit update destroy download]
77
before_action :authorize_resource, only: %i[new show edit update destroy download]
88

9+
def index
10+
@total_size = policy_scope(Upload).sum(&:byte_size)
11+
end
12+
913
def download # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
1014
if resource_instance.attached?
1115
# Trigger the background job to log the download

app/helpers/better_together/uploads_helper.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,8 @@
33
module BetterTogether
44
# helper methods for file uploads
55
module UploadsHelper
6+
def total_upload_size(uploads)
7+
number_to_human_size(uploads.sum(&:byte_size))
8+
end
69
end
710
end
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
1-
<h1>Files#index</h1>
2-
<p>Find me in app/views/better_together/files/index.html.erb</p>
1+
<% content_for :page_title do %>
2+
<%= resource_class.model_name.human.pluralize %>
3+
<% end %>
4+
5+
<div class="container my-3">
6+
<h1><%= resource_class.model_name.human.pluralize %></h1>
7+
<p><%= t('.storage_usage', size: number_to_human_size(@total_size)) %></p>
8+
</div>

config/locales/en.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,9 @@ en:
613613
contact_details: Contact details
614614
details: Details
615615
images: Images
616+
uploads:
617+
index:
618+
storage_usage: "You're using %{size} of storage"
616619
contact_details:
617620
contact_information: Contact information
618621
title: Contact Details

spec/helpers/better_together/uploads_helper_spec.rb

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,13 @@
22

33
require 'rails_helper'
44

5-
# Specs in this file have access to a helper object that includes
6-
# the UploadsHelper. For example:
7-
#
8-
# describe UploadsHelper do
9-
# describe "string concat" do
10-
# it "concats two strings with spaces" do
11-
# expect(helper.concat_strings("this","that")).to eq("this that")
12-
# end
13-
# end
14-
# end
155
module BetterTogether
166
RSpec.describe UploadsHelper, type: :helper do
17-
it 'exists' do
18-
expect(described_class).to be
7+
describe '#total_upload_size' do
8+
it 'returns human readable total size' do
9+
uploads = [double(byte_size: 2.megabytes), double(byte_size: 3.megabytes)]
10+
expect(helper.total_upload_size(uploads)).to eq '5 MB'
11+
end
1912
end
2013
end
2114
end

spec/views/better_together/files/index.html.erb_spec.rb

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# frozen_string_literal: true
2+
3+
require 'rails_helper'
4+
5+
RSpec.describe 'better_together/uploads/index.html.erb', type: :view do
6+
it 'renders total storage usage' do
7+
allow(view).to receive(:resource_class).and_return(BetterTogether::Upload)
8+
assign(:uploads, [])
9+
assign(:total_size, 3.megabytes)
10+
11+
render
12+
13+
expect(rendered).to include("You're using 3 MB of storage")
14+
end
15+
end

0 commit comments

Comments
 (0)