Skip to content
Open
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
4 changes: 4 additions & 0 deletions app/controllers/better_together/uploads_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions app/helpers/better_together/uploads_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 8 additions & 2 deletions app/views/better_together/uploads/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
<h1>Files#index</h1>
<p>Find me in app/views/better_together/files/index.html.erb</p>
<% content_for :page_title do %>
<%= resource_class.model_name.human.pluralize %>
<% end %>

<div class="container my-3">
<h1><%= resource_class.model_name.human.pluralize %></h1>
<p><%= t('.storage_usage', size: number_to_human_size(@total_size)) %></p>
</div>
3 changes: 3 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 6 additions & 13 deletions spec/helpers/better_together/uploads_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 0 additions & 11 deletions spec/views/better_together/files/index.html.erb_spec.rb

This file was deleted.

15 changes: 15 additions & 0 deletions spec/views/better_together/uploads/index.html.erb_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Loading