Skip to content

Commit

Permalink
Merge pull request #58 from Coding-Cactus/active-storage-optimisations
Browse files Browse the repository at this point in the history
Active storage optimisations
  • Loading branch information
Coding-Cactus authored Sep 14, 2023
2 parents 911bc31 + a242883 commit 93406c9
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 72 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ ruby "3.1.2"
# Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main"
gem "rails", "~> 7.0.4", ">= 7.0.4.2"

gem "activesupport", ">= 7.0.7.1"

# The original asset pipeline for Rails [https://github.com/rails/sprockets-rails]
gem "sprockets-rails"

Expand Down
119 changes: 60 additions & 59 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13,47 +13,47 @@ GIT
GEM
remote: https://rubygems.org/
specs:
actioncable (7.0.6)
actionpack (= 7.0.6)
activesupport (= 7.0.6)
actioncable (7.0.8)
actionpack (= 7.0.8)
activesupport (= 7.0.8)
nio4r (~> 2.0)
websocket-driver (>= 0.6.1)
actionmailbox (7.0.6)
actionpack (= 7.0.6)
activejob (= 7.0.6)
activerecord (= 7.0.6)
activestorage (= 7.0.6)
activesupport (= 7.0.6)
actionmailbox (7.0.8)
actionpack (= 7.0.8)
activejob (= 7.0.8)
activerecord (= 7.0.8)
activestorage (= 7.0.8)
activesupport (= 7.0.8)
mail (>= 2.7.1)
net-imap
net-pop
net-smtp
actionmailer (7.0.6)
actionpack (= 7.0.6)
actionview (= 7.0.6)
activejob (= 7.0.6)
activesupport (= 7.0.6)
actionmailer (7.0.8)
actionpack (= 7.0.8)
actionview (= 7.0.8)
activejob (= 7.0.8)
activesupport (= 7.0.8)
mail (~> 2.5, >= 2.5.4)
net-imap
net-pop
net-smtp
rails-dom-testing (~> 2.0)
actionpack (7.0.6)
actionview (= 7.0.6)
activesupport (= 7.0.6)
actionpack (7.0.8)
actionview (= 7.0.8)
activesupport (= 7.0.8)
rack (~> 2.0, >= 2.2.4)
rack-test (>= 0.6.3)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.2.0)
actiontext (7.0.6)
actionpack (= 7.0.6)
activerecord (= 7.0.6)
activestorage (= 7.0.6)
activesupport (= 7.0.6)
actiontext (7.0.8)
actionpack (= 7.0.8)
activerecord (= 7.0.8)
activestorage (= 7.0.8)
activesupport (= 7.0.8)
globalid (>= 0.6.0)
nokogiri (>= 1.8.5)
actionview (7.0.6)
activesupport (= 7.0.6)
actionview (7.0.8)
activesupport (= 7.0.8)
builder (~> 3.1)
erubi (~> 1.4)
rails-dom-testing (~> 2.0)
Expand All @@ -65,22 +65,22 @@ GEM
activemodel (>= 5.2.0)
activestorage (>= 5.2.0)
activesupport (>= 5.2.0)
activejob (7.0.6)
activesupport (= 7.0.6)
activejob (7.0.8)
activesupport (= 7.0.8)
globalid (>= 0.3.6)
activemodel (7.0.6)
activesupport (= 7.0.6)
activerecord (7.0.6)
activemodel (= 7.0.6)
activesupport (= 7.0.6)
activestorage (7.0.6)
actionpack (= 7.0.6)
activejob (= 7.0.6)
activerecord (= 7.0.6)
activesupport (= 7.0.6)
activemodel (7.0.8)
activesupport (= 7.0.8)
activerecord (7.0.8)
activemodel (= 7.0.8)
activesupport (= 7.0.8)
activestorage (7.0.8)
actionpack (= 7.0.8)
activejob (= 7.0.8)
activerecord (= 7.0.8)
activesupport (= 7.0.8)
marcel (~> 1.0)
mini_mime (>= 1.1.0)
activesupport (7.0.6)
activesupport (7.0.8)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 1.6, < 2)
minitest (>= 5.1)
Expand Down Expand Up @@ -133,10 +133,10 @@ GEM
docile (1.4.0)
erubi (1.12.0)
ffi (1.15.5)
globalid (1.1.0)
activesupport (>= 5.0)
globalid (1.2.1)
activesupport (>= 6.1)
hiredis (0.6.3)
i18n (1.12.0)
i18n (1.14.1)
concurrent-ruby (~> 1.0)
image_processing (1.12.2)
mini_magick (>= 4.9.5, < 5)
Expand Down Expand Up @@ -191,29 +191,29 @@ GEM
rack (2.2.7)
rack-test (2.1.0)
rack (>= 1.3)
rails (7.0.6)
actioncable (= 7.0.6)
actionmailbox (= 7.0.6)
actionmailer (= 7.0.6)
actionpack (= 7.0.6)
actiontext (= 7.0.6)
actionview (= 7.0.6)
activejob (= 7.0.6)
activemodel (= 7.0.6)
activerecord (= 7.0.6)
activestorage (= 7.0.6)
activesupport (= 7.0.6)
rails (7.0.8)
actioncable (= 7.0.8)
actionmailbox (= 7.0.8)
actionmailer (= 7.0.8)
actionpack (= 7.0.8)
actiontext (= 7.0.8)
actionview (= 7.0.8)
activejob (= 7.0.8)
activemodel (= 7.0.8)
activerecord (= 7.0.8)
activestorage (= 7.0.8)
activesupport (= 7.0.8)
bundler (>= 1.15.0)
railties (= 7.0.6)
rails-dom-testing (2.1.1)
railties (= 7.0.8)
rails-dom-testing (2.2.0)
activesupport (>= 5.0.0)
minitest
nokogiri (>= 1.6)
rails-html-sanitizer (1.5.0)
loofah (~> 2.19, >= 2.19.1)
railties (7.0.6)
actionpack (= 7.0.6)
activesupport (= 7.0.6)
railties (7.0.8)
actionpack (= 7.0.8)
activesupport (= 7.0.8)
method_source
rake (>= 12.2)
thor (~> 1.0)
Expand Down Expand Up @@ -247,7 +247,7 @@ GEM
rubocop-performance (1.18.0)
rubocop (>= 1.7.0, < 2.0)
rubocop-ast (>= 0.4.0)
rubocop-rails (2.20.2)
rubocop-rails (2.21.1)
activesupport (>= 4.2.0)
rack (>= 1.1)
rubocop (>= 1.33.0, < 2.0)
Expand Down Expand Up @@ -285,7 +285,7 @@ GEM
unicode-display_width (2.3.0)
warden (1.2.9)
rack (>= 2.0.9)
web-console (4.2.0)
web-console (4.2.1)
actionview (>= 6.0.0)
activemodel (>= 6.0.0)
bindex (>= 0.4.0)
Expand All @@ -309,6 +309,7 @@ DEPENDENCIES
actionpack (>= 7.0.5.1)
active_record_union
active_storage_validations
activesupport (>= 7.0.7.1)
appsignal
aws-sdk-s3
bootsnap
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/chat_members_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def update_params
end

def load_chats
@chats = current_user.chats.includes(:last_message).order("messages.created_at" => :desc)
@chats = current_user.chats.with_attached_pfp.includes(:last_message).order("messages.created_at" => :desc)
end

def populate_chat
Expand Down
8 changes: 4 additions & 4 deletions app/controllers/chats_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def index; end
def show
@show_page = true
@message = @chat.messages.build
@message_groups = @chat.messages.includes(:user, :statuses).order(created_at: :desc).limit(50)
@message_groups = @chat.messages.includes({ user: { pfp_attachment: :blob } }, :statuses).order(created_at: :desc).limit(50)
.reverse.reduce([]) { |groups, msg| group_up_message(groups, msg) }
end

Expand All @@ -40,15 +40,15 @@ def create
end

def edit
@members = @chat.chat_members.includes(:user).order("LOWER(users.name)").references(:users)
@members = @chat.chat_members.includes(user: { pfp_attachment: :blob }).order("LOWER(users.name)").references(:users)
end

def update
if @chat.update(update_chat_params)
flash[:notice] = "Chat updated"
redirect_to edit_chat_path(@chat)
else
@members = @chat.chat_members.includes(:user)
@members = @chat.chat_members.includes(user: { pfp_attachment: :blob })

flash.now[:alert] = "Something went wrong when updating the chat"
render :edit, status: :unprocessable_entity
Expand Down Expand Up @@ -79,7 +79,7 @@ def load_chat
end

def load_chats
@chats = current_user.chats.includes(:last_message).order("messages.created_at" => :desc)
@chats = current_user.chats.includes(:last_message).order("messages.created_at" => :desc).with_attached_pfp
end

def new_chat_params
Expand Down
6 changes: 3 additions & 3 deletions app/controllers/contacts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ class ContactsController < ApplicationController
before_action :check_pending_contact_exists, only: :destroy

def index
@contacts = current_user.contacts.order("LOWER(name)")
@contacts = current_user.contacts.order("LOWER(name)").with_attached_pfp
end

def new
@new_contact = Contact.new
@incoming_requests = current_user.incoming_contact_requests.order("LOWER(name)")
@outgoing_requests = current_user.outgoing_contact_requests.order("LOWER(name)")
@incoming_requests = current_user.incoming_contact_requests.order("LOWER(name)").with_attached_pfp
@outgoing_requests = current_user.outgoing_contact_requests.order("LOWER(name)").with_attached_pfp
end

def create
Expand Down
4 changes: 2 additions & 2 deletions app/models/chat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Chat < ApplicationRecord
has_many :messages, dependent: :destroy
has_one :last_message, -> { order(created_at: :desc) }, class_name: "Message"

has_one_attached :pfp do |attachable|
has_one_attached :pfp, dependent: :destroy do |attachable|
attachable.variant :thumb, resize_to_limit: [175, 175]
end

Expand Down Expand Up @@ -65,7 +65,7 @@ def administrator_ids

def pfp_thumbnail
return pfp if pfp.content_type == "image/gif" || errors.include?(:pfp)
pfp.variant(:thumb).processed
pfp.variant(:thumb)
end

private
Expand Down
4 changes: 2 additions & 2 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class User < ApplicationRecord
has_many :incoming_contacts, foreign_key: "target_id", class_name: "Contact", dependent: :destroy
has_many :outgoing_contacts, foreign_key: "creator_id", class_name: "Contact", dependent: :destroy

has_one_attached :pfp do |attachable|
has_one_attached :pfp, dependent: :destroy do |attachable|
attachable.variant :thumb, resize_to_limit: [175, 175]
end

Expand Down Expand Up @@ -59,7 +59,7 @@ def find_pending_contact(contact_user_id)

def pfp_thumbnail
return pfp if pfp.content_type == "image/gif" || errors.include?(:pfp)
pfp.variant(:thumb).processed
pfp.variant(:thumb)
end

protected
Expand Down
2 changes: 1 addition & 1 deletion app/views/shared/_icon.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="pfp <%= size %>" <%= "style=\"background-color: #{resource.colour};\"".html_safe unless resource.pfp.attached? %>>
<% if resource.pfp.attached? %>
<img src="<%= url_for(resource.pfp_thumbnail) %>" alt="<%= resource.name %>'s profile picture" />
<%= image_tag url_for(resource.pfp_thumbnail), alt: "#{resource.name}'s profile picture", size: 175 %>
<% else %>
<span><%= resource.initials %></span>
<% end %>
Expand Down
2 changes: 2 additions & 0 deletions config/storage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ cloudflare:
secret_access_key: <%= Rails.application.credentials.dig(:cloudflare, :secret_access_key) %>
region: auto
bucket: <%= Rails.application.credentials.dig(:cloudflare, :bucket) %><%= Rails.env.production? ? "prod" : "dev" %>
upload:
cache_control: 'public, max-age=31536000'

# Use bin/rails credentials:edit to set the AWS secrets (as aws:access_key_id|secret_access_key)
# amazon:
Expand Down

0 comments on commit 93406c9

Please sign in to comment.