Skip to content
This repository has been archived by the owner on Jul 24, 2020. It is now read-only.

Commit

Permalink
Migration from PaperClip to ActiveStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
dokurnon committed Dec 20, 2019
1 parent b4adab4 commit 9100e78
Show file tree
Hide file tree
Showing 35 changed files with 386 additions and 274 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
# AWS_ACCESS_KEY_ID - AWS access key identifier string
# AWS_SECRET_ACCESS_KEY - AWS secret access key
# AWS_S3_REGION - the region that the S3 bucket is located in
# STORAGE_LOCATION - Either :local or :amazon

# Uncomment the following line to enable CAS authentication
# export CAS_AUTH=1
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ coverage/
node_modules/*
*.swp
*.env
storage/
storage/
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
* Any problems should be reported via the "report an issue" link in the footer of the application.

## Unreleased

### Important: read before updating
* Follow the directions [here](https://gitlab.com/yale-sdmp/reservations/-/wikis/Upgrading-to-7.0) for previously uploaded files to continue working.

### Added
* Added bundler audit ([#1788](https://gitlab.com/yale-sdmp/reservations/issues/1788)).
* Added Gitlab CI ([#1785](https://gitlab.com/yale-sdmp/reservations/issues/1785)).
Expand All @@ -13,6 +17,7 @@
* Updated Rubyzip from 1.2.1 -> 1.3.0 ([#1789](https://gitlab.com/yale-sdmp/reservations/issues/1789)).
* Updated Simple Form from 4.0.1 -> 5.0.1 ([#1790](https://gitlab.com/yale-sdmp/reservations/issues/1790)).
* Changed CI pipeline to use latest Chromedriver ([#1795](https://gitlab.com/yale-sdmp/reservations/issues/1795)).
* Moved from Paperclip to Active Storage ([#1786](https://gitlab.com/yale-sdmp/reservations/issues/1786)).

### Fixed
* Fixed deprecation warnings ([#1787](https://gitlab.com/yale-sdmp/reservations/issues/1787)).
Expand Down
4 changes: 3 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ gem 'rails_admin', '~> 2.0.0'
gem 'net-ldap', '~> 0.16.0'

# attachments
gem 'paperclip', '~> 5.2.0'

# for exporting multiple files
gem 'rubyzip', '~> 1.3.0'
Expand Down Expand Up @@ -126,3 +125,6 @@ group :heroku do
gem 'rails_12factor', '~> 0.0.3'
end

gem "mini_magick", "~> 4.9"

gem "active_storage_validations", "~> 0.8.4"
18 changes: 5 additions & 13 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ GEM
erubi (~> 1.4)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.1, >= 1.2.0)
active_storage_validations (0.8.4)
rails (>= 5.2.0)
activejob (6.0.0)
activesupport (= 6.0.0)
globalid (>= 0.3.6)
Expand Down Expand Up @@ -125,9 +127,6 @@ GEM
childprocess (0.9.0)
ffi (~> 1.0, >= 1.0.11)
chronic (0.10.2)
climate_control (0.2.0)
cocaine (0.5.8)
climate_control (>= 0.0.3, < 1.0)
cocoon (1.2.10)
codeclimate-test-reporter (1.0.8)
simplecov (<= 0.13)
Expand Down Expand Up @@ -252,10 +251,8 @@ GEM
marcel (0.3.3)
mimemagic (~> 0.3.2)
method_source (0.8.2)
mime-types (3.3)
mime-types-data (~> 3.2015)
mime-types-data (3.2019.0904)
mimemagic (0.3.3)
mini_magick (4.9.5)
mini_mime (1.0.2)
mini_portile2 (2.4.0)
minitest (5.12.2)
Expand Down Expand Up @@ -283,12 +280,6 @@ GEM
octokit (3.8.0)
sawyer (~> 0.6.0, >= 0.5.3)
orm_adapter (0.5.0)
paperclip (5.2.1)
activemodel (>= 4.2.0)
activesupport (>= 4.2.0)
cocaine (~> 0.5.5)
mime-types
mimemagic (~> 0.3.0)
parallel (1.11.2)
parser (2.4.0.0)
ast (~> 2.2)
Expand Down Expand Up @@ -503,6 +494,7 @@ PLATFORMS
ruby

DEPENDENCIES
active_storage_validations (~> 0.8.4)
activemodel-serializers-xml (~> 1.0.1)
awesome_print (~> 1.8.0)
aws-sdk (~> 2.3.0)
Expand Down Expand Up @@ -540,11 +532,11 @@ DEPENDENCIES
kaminari (~> 0.17.0)
letter_opener (~> 1.4.1)
letter_opener_web (~> 1.3.0)
mini_magick (~> 4.9)
momentjs-rails (~> 2.17.1)
mysql2 (~> 0.5.2)
net-ldap (~> 0.16.0)
nilify_blanks (~> 1.2.1)
paperclip (~> 5.2.0)
party_foul (~> 1.5.5)
pry (~> 0.10.4)
pry-byebug (~> 3.4.2)
Expand Down
16 changes: 0 additions & 16 deletions app/controllers/equipment_models_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,6 @@ def update
delete_files

eq_params = equipment_model_params
# correct for file type
eq_params[:documentation] = fix_content_type(eq_params[:documentation])

if @equipment_model.update(eq_params)
# hard-delete any deleted checkin/checkout procedures
Expand Down Expand Up @@ -170,20 +168,6 @@ def equipment_model_params
checkout_procedures_attributes: {})
end

# from https://gist.github.com/cnk/4453c6e81837e8d38b7e
def fix_content_type(filedata)
return nil if filedata.blank?
# see what the unix file command thinks this is
if filedata.content_type == 'application/octect-stream'
filedata.content_type = type_from_file_command(filedata.path)
end
filedata
end

def type_from_file_command(file)
Paperclip::FileCommandContentTypeDetector.new(file).detect
end

def generate_calendar_reservations
# we need uniq because it otherwise includes overdue reservations in the
# date range twice
Expand Down
14 changes: 12 additions & 2 deletions app/helpers/app_configs_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,18 @@

module AppConfigsHelper
def current_favicon_and_options
return if @app_configs.favicon.blank?
return unless @app_configs.favicon.attached?
'<br><br><strong>Current Favicon: </strong> '\
"#{image_tag @app_configs.favicon.url unless @app_configs.favicon.nil?}"
"#{current_favicon}"
end

private

def current_favicon
resized_favicon = @app_configs
.favicon
.variant(resize_to_limit: [150, 150])

image_tag resized_favicon
end
end
20 changes: 4 additions & 16 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,16 @@ def markdown(text, filter = true)
no_intra_emphasis: true,
strikethrough: true, superscript: true)
# This is safe -- Redcarpet sanitizes
# rubocop:disable Rails/OutputSafety
markdown.render(text).html_safe
# rubocop:enable Rails/OutputSafety
end

def markdown_to_plain_text(text)
# This is safe -- Redcarpet sanitizes
# rubocop:disable Rails/OutputSafety
strip_tags(markdown(text)).html_safe
end

def paperclip_field_error(local_form_variable, *fields)
# Field must be symbol
fields.each do |field|
return 'error' if local_form_variable.error(field).present?
end
# rubocop:enable Rails/OutputSafety
end

def intify(integer)
Expand All @@ -39,13 +36,4 @@ def dayify(integer)
return 'unrestricted' if integer.nil? || integer == Float::INFINITY
pluralize(integer, 'day')
end

def paperclip_full_url(upload)
return '#' unless upload.url
unless Rails.application.config.action_controller.relative_url_root
return upload.url
end
"#{Rails.application.config.action_controller.relative_url_root}" \
"#{upload.url}"
end
end
4 changes: 2 additions & 2 deletions app/helpers/equipment_models_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

module EquipmentModelsHelper
def evaluate_img_presence(equipment_model)
if equipment_model.photo.exists?
equipment_model.photo.url(:small)
if equipment_model.photo.attached?
equipment_model.photo.variant(resize: '150x150')
else
'no-image-260.gif'
end
Expand Down
30 changes: 30 additions & 0 deletions app/mailers/devise_mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

class DeviseMailer < Devise::Mailer
# gives access to all helpers defined within `application_helper`.
helper :application
include Devise::Controllers::UrlHelpers # Optional. eg. `confirmation_url`
# to make sure that your mailer uses the devise views
default sender: ->() { devise_sender }
default template_path: 'devise/mailer'

def reset_password_instructions(record, token, opts = {})
super
end

private

def devise_sender
@admin_email = if AppConfig.check(:admin_email)
AppConfig.get :admin_email
else
'[email protected]'
end

Mail::Address.new @admin_email

# address = Mail::Address.new env('MAILER_FROM')
# address.display_name = env('MAILER_FROM_NAME') if env?('MAILER_FROM_NAME')
# address.format
end
end
12 changes: 4 additions & 8 deletions app/models/app_config.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
# frozen_string_literal: true

class AppConfig < ApplicationRecord
has_attached_file :favicon,
url: paperclip_url(filename: 'favicon'),
path: ':rails_root/public/attachments/app_configs/'\
':attachment/:id/:style/favicon.:extension'
has_one_attached :favicon

validates_with AttachmentContentTypeValidator,
attributes: :favicon,
content_type: 'image/vnd.microsoft.icon',
message: 'Must be .ico'
validates :favicon,
content_type: { in: 'image/vnd.microsoft.icon',
message: 'must be .ico' }

validates :site_title, presence: true,
length: { maximum: 20 }
Expand Down
8 changes: 0 additions & 8 deletions app/models/application_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,4 @@

class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true

def self.paperclip_url(filename: ':basename')
if ENV['ENABLE_PAPERCLIP_S3'].present?
return Paperclip::Attachment.default_options[:url]
end
class_plural = to_s.underscore.pluralize
"/attachments/#{class_plural}/:attachment/:id/:style/#{filename}.:extension"
end
end
66 changes: 14 additions & 52 deletions app/models/equipment_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,61 +69,23 @@ def not_associated_with_self
end

#################
## Paperclip ##
## ActiveStorage ##
#################

has_attached_file :photo, # generates profile picture
styles: {
large: { geometry: '500x500', format: 'png' },
medium: { geometry: '250x250', format: 'png' },
small: { geometry: '150x150', format: 'png' },
thumbnail: { geometry: '260x180', format: 'png' }
},
convert_options: {
large:
'-background none -gravity center -extent 500x500',
medium:
'-background none -gravity center -extent 250x250',
small:
'-background none -gravity center -extent 150x150',
thumbnail:
'-background none -gravity center -extent 260x180'
},
url: paperclip_url,
path: ':rails_root/public/attachments/equipment_models/'\
':attachment/:id/:style/:basename.:extension',
default_url: '/fat_cat.jpeg',
preserve_files: true

has_attached_file :documentation, # generates document
content_type: 'application/pdf',
url: paperclip_url,
path: ':rails_root/public/attachments/equipment_models/'\
':attachment/:id/:style/:basename.:extension',
preserve_files: true

validates_attachment_content_type :photo,
content_type: ['image/jpg', 'image/png',
'image/jpeg'],
message: 'must be jpeg, jpg, or png.'
validates_attachment_size :photo,
less_than: 1.megabytes,
message: 'must be less than 1 MB in size'
validates_attachment_content_type :documentation,
content_type: ['application/pdf'],
message: 'must be pdf'

validates_attachment_size :documentation,
less_than: 5.megabytes,
message: 'must be less than 5 MB in size'

Paperclip.interpolates :normalized_photo_name do |attachment, _style|
attachment.instance.normalized_photo_name
end
has_one_attached :photo
has_one_attached :documentation

def normalized_photo_name
"#{id}-#{photo_file_name.gsub(/[^a-zA-Z0-9_\.]/, '_')}"
end
validates :photo,
content_type: { in: ['image/jpg', 'image/png', 'image/jpeg'],
message: 'must be jpeg, jpg, or png.' },
size: { less_than: 1.megabytes,
message: 'must be less than 1 MB in size' }

validates :documentation,
content_type: { in: 'application/pdf',
message: 'must be pdf' },
size: { less_than: 5.megabytes,
message: 'must be less than 5 MB in size' }

######################
## Instance Methods ##
Expand Down
13 changes: 7 additions & 6 deletions app/views/app_configs/_form.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@
'The email to which contact form submissions will be sent.' %>
<%= f.input :default_per_cat_page, label: 'Items per page', hint:
'Default number of items to display per page in the Catalog.' %>
<%# Paperclip inline error fix %>
<%= f.input :favicon, label: 'Custom Favicon', hint:
"Displayed in the tab and address bar of most browsers. Must be <em>*.ico</em> #{current_favicon_and_options}".html_safe, wrapper_html: {class: paperclip_field_error(f, :favicon_content_type)} do %>
<%= f.input_field :favicon, as: :file %>
<%= f.error :favicon_content_type %>
<% end %>

<div class="form-field">
<%= f.file_field :favicon,
accept: 'image/vnd.microsoft.icon',
direct_upload: true %>
<%= f.label "Displayed in the tab and address bar of most browsers. Must be <em>*.ico</em> #{current_favicon_and_options}".html_safe %>
</div>

<% if @app_config.favicon.present? %>
<div class='control-group'>
Expand Down
2 changes: 1 addition & 1 deletion app/views/catalog/_equipment_model_div.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="thumbnail">
<%= link_to equipment_model do %>
<% image_tag (equipment_model.photo.exists? ? equipment_model.photo.url(:thumbnail) : 'no-image.gif') %>
<% image_tag (equipment_model.photo.attached? ? equipment_model.photo.variant(resize: "260x180") : 'no-image.gif') %>
<% end %>
<h4 class="equipment_title">
<%= link_to equipment_model.name, equipment_model, class: 'equipment_title_link', rel: 'tooltip', title: equipment_model.name %>
Expand Down
Loading

0 comments on commit 9100e78

Please sign in to comment.