Skip to content

Commit

Permalink
Merge branch 'hotfix/v1.3.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
odinsride committed Jun 27, 2019
2 parents 7c1f55d + ea7e289 commit 6204f86
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 19 deletions.
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased] changes

## [v1.3.2] - 2019-06-27

### Fixed
- Updated Paperclip attachment migration rake task to handle errors and report them for manual clean-up
- Updated Transaction show view to display a generic file icon for non-image attachments, since extra effort is required to generate thumbnails for those with ActiveStorage.

## [v1.3.0] - 2019-06-25

### Changed
Expand Down Expand Up @@ -107,7 +113,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Account overview with list of all accounts and balances for each


[Unreleased]: https://github.com/odinsride/olubalance/compare/v1.2.2...HEAD
[Unreleased]: https://github.com/odinsride/olubalance/compare/v1.3.2...HEAD
[v1.3.2]: https://github.com/odinsride/olubalance/compare/v1.3.0...v1.3.2
[v1.3.0]: https://github.com/odinsride/olubalance/compare/v1.2.2...v1.3.0
[v1.2.2]: https://github.com/odinsride/olubalance/compare/v1.2.1...v1.2.2
[v1.2.1]: https://github.com/odinsride/olubalance/compare/v1.2.0...v1.2.1
[v1.2.0]: https://github.com/odinsride/olubalance/compare/v1.1.3...v1.2.0
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def assign_navbar_content
end

# disabling rubocop until I have time since this was copied from the interweb
def custom_paginate_renderer # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
def custom_paginate_renderer # rubocop:disable Metrics/MethodLength
# Return nice pagination for materialize
Class.new(WillPaginate::ActionView::LinkRenderer) do
def container_attributes
Expand Down
2 changes: 1 addition & 1 deletion app/models/transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Transaction < ApplicationRecord
has_one :transaction_balance, dependent: :destroy

has_one_attached :attachment

# has_attached_file :attachment,
# # In order to determine the styles of the image we want to save
# # e.g. a small style copy of the image, plus a large style copy
Expand Down
6 changes: 5 additions & 1 deletion app/views/transactions/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@
<li class="collection-item">
<div class="row ob-no-bottom valign-wrapper">
<div class="col s5 m3 l3 center-align">
<%= link_to image_tag(@transaction.attachment.variant(resize: "100x100"), :class => 'z-depth-2 hoverable'), url_for(@transaction.attachment), :target => '_blank' %>
<% if @transaction.attachment.variable? %>
<%= link_to image_tag(@transaction.attachment.variant(resize: "100x100"), :class => 'z-depth-2 hoverable'), url_for(@transaction.attachment), :target => '_blank' %>
<% else %>
<%= link_to (fa_icon "file-text 3x", class: 'ob-text-primary'), url_for(@transaction.attachment), :target => "_blank" %>
<% end %>
</div>
<div class="col s7 m9 l9 xl10">
<%= link_to @transaction.attachment.filename, url_for(@transaction.attachment), :class => 'ob-text-primary', :target => '_blank' %>
Expand Down
54 changes: 40 additions & 14 deletions lib/tasks/migrate_paperclip_attachments.rake
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,44 @@ namespace :migrate_paperclip do
models.each do |model|
puts 'Checking Model [' + model.to_s + '] for Paperclip attachment columns ...'

errs = []
err_ids = []

# If the model has a column or columns named *_file_name,
# We are assuming this is a column added by Paperclip.
# Store the name of the attachment(s) found (e.g. "avatar") in an array named attachments
attachments = model.column_names.map do |c|
Regexp.last_match(1) if c =~ /(.+)_file_name$/
end.compact

# For
# For each attachment on the model, migrate the attachments
attachments.each do |attachment|
migrate_attachment(attachment, model)
migrate_attachment(attachment, model, errs, err_ids)
end

next if errs.empty?

# Display records that have errors
puts ''
puts 'Errored attachments:'
puts ''

errs.each do |err|
puts err
end

# Display list of errored attachment IDs
puts ''
puts 'Errored attachments list of IDs (use for SQL statements)'
puts err_ids.join(',')
puts ''
end
end
end

private

def migrate_attachment(attachment, model)
def migrate_attachment(attachment, model, errs, err_ids)
model.where.not("#{attachment}_file_name": nil).find_each do |instance|
# Set the S3 Bucket based on environment
bucket = Rails.env.production? ? ENV['S3_BUCKET_NAME'] : ENV['S3_BUCKET_NAME_DEV']
Expand All @@ -43,28 +63,34 @@ def migrate_attachment(attachment, model)
content_type = instance.send("#{attachment}_content_type")
original = CGI.unescape(filename.gsub(extension, "_original#{extension}"))

# puts ' [' + model.name + ' (ID: ' +
# instance_id.to_s + ')] ' \
# 'Copying to ActiveStorage location: ' + original
puts ' [' + model.name + ' (ID: ' +
instance_id.to_s + ')] ' \
'Copying to ActiveStorage location: ' + original

# Paperclip stores attachments in a directory structure such as:
# 000/000/001 = Instance ID 1
# 000/050/250 = Instance ID 50250
# 999/999/999 = Instance ID 999999999
# We need to build the appropriate path to get the correct URL for the attachment
instance_path = instance_id.to_s.rjust(9, "0")
instance_path = instance_path.scan(/.{1,3}/).join("/")
instance_path = instance_id.to_s.rjust(9, '0')
instance_path = instance_path.scan(/.{1,3}/).join('/')

# Build the S3 URL
url = "https://#{bucket}.s3.#{region}.amazonaws.com/#{model.name.downcase.pluralize}/#{attachment.pluralize}/#{instance_path}/original/#{filename}"
puts ' ' + url
# puts ' ' + url

# Copy the original Paperclip attachment to its new ActiveStorage location
# For debugging/testing purposes, comment out this section and print the URL to log to verify the correctness
# instance.send(attachment.to_sym).attach(
# io: open(url),
# filename: filename,
# content_type: content_type
# )
begin
instance.send(attachment.to_sym).attach(
io: open(url),
filename: filename,
content_type: content_type
)
rescue StandardError => e
puts ' ... error! ...'
errs.push("[#{model.name}][#{attachment}] - #{instance_id} - #{e}")
err_ids.push(instance_id)
end
end
end
4 changes: 3 additions & 1 deletion lib/tasks/migrate_paperclip_data.rake
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

# lib/tasks/migrate_paperclip_data.rake
require 'open-uri'

Expand Down Expand Up @@ -138,4 +140,4 @@ def checksum(attachment)
# remote files stored on a cloud service:
url = attachment.url
Digest::MD5.base64digest(Net::HTTP.get(URI(url)))
end
end

0 comments on commit 6204f86

Please sign in to comment.