Skip to content

Commit

Permalink
Merge pull request #541 from pgharts/upgrade-to-active-storage
Browse files Browse the repository at this point in the history
Upgrade trusty-cms to use Active Storage and Deprecate Paperclip
  • Loading branch information
Crankums authored Oct 5, 2021
2 parents 689549e + 3d880bf commit 5d73a41
Show file tree
Hide file tree
Showing 13 changed files with 97 additions and 75 deletions.
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ gem 'trustygems', '~> 0.2.0'
gemspec

group :development, :test do
gem 'activestorage-validator'
gem 'acts_as_list'
gem 'database_cleaner'
gem 'factory_bot_rails', '6.2.0'
gem 'kt-paperclip', '~> 7.0.0'
gem 'file_validators'
gem 'launchy', '~> 2.5.0'
gem 'mysql2'
gem 'poltergeist', '~> 1.18.1'
Expand Down
32 changes: 21 additions & 11 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
PATH
remote: .
specs:
trusty-cms (4.3.4)
trusty-cms (5.0.0)
RedCloth (= 4.3.2)
activestorage-validator
acts_as_list (>= 0.9.5, < 1.1.0)
acts_as_tree (~> 2.9.1)
ckeditor (>= 4.2.2, < 4.4.0)
Expand All @@ -12,6 +13,7 @@ PATH
haml
haml-rails (~> 2.0)
highline (>= 1.7.8, < 2.1.0)
image_processing
kraken-io
mini_racer
mysql2
Expand Down Expand Up @@ -86,6 +88,8 @@ GEM
activesupport (= 6.1.4.1)
marcel (~> 1.0.0)
mini_mime (>= 1.1.0)
activestorage-validator (0.1.4)
rails (>= 5.2.0)
activesupport (6.1.4.1)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 1.6, < 2)
Expand Down Expand Up @@ -144,6 +148,9 @@ GEM
factory_bot (~> 6.2.0)
railties (>= 5.0.0)
ffi (1.15.4)
file_validators (3.0.0)
activemodel (>= 3.2)
mime-types (>= 1.0)
globalid (0.5.2)
activesupport (>= 5.0)
haml (5.2.2)
Expand All @@ -161,37 +168,36 @@ GEM
haml (>= 4.0, < 6)
nokogiri (>= 1.6.0)
ruby_parser (~> 3.5)
httparty (0.18.1)
httparty (0.20.0)
mime-types (~> 3.0)
multi_xml (>= 0.5.2)
i18n (1.8.10)
concurrent-ruby (~> 1.0)
image_processing (1.12.1)
mini_magick (>= 4.9.5, < 5)
ruby-vips (>= 2.0.17, < 3)
json (2.5.1)
kraken-io (0.1.3)
activesupport
httparty
json
multipart-post
kt-paperclip (7.0.0)
activemodel (>= 4.2.0)
activesupport (>= 4.2.0)
marcel (~> 1.0.1)
mime-types
terrapin (~> 0.6.0)
launchy (2.5.0)
addressable (~> 2.7)
libv8-node (15.14.0.1-x86_64-darwin-18)
libv8-node (15.14.0.1-x86_64-darwin-20)
libv8-node (15.14.0.1-x86_64-linux)
loofah (2.12.0)
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
mail (2.7.1)
mini_mime (>= 0.1.1)
marcel (1.0.1)
marcel (1.0.2)
method_source (1.0.0)
mime-types (3.3.1)
mime-types-data (~> 3.2015)
mime-types-data (3.2021.0225)
mini_magick (4.11.0)
mini_mime (1.1.1)
mini_racer (0.4.0)
libv8-node (~> 15.14.0.0)
Expand Down Expand Up @@ -280,6 +286,8 @@ GEM
rspec-mocks (~> 3.10)
rspec-support (~> 3.10)
rspec-support (3.10.2)
ruby-vips (2.1.3)
ffi (~> 1.12)
ruby_parser (3.17.0)
sexp_processor (~> 4.15, >= 4.15.1)
sass-rails (6.0.0)
Expand Down Expand Up @@ -331,13 +339,15 @@ GEM

PLATFORMS
x86_64-darwin-18
x86_64-darwin-20
x86_64-linux

DEPENDENCIES
activestorage-validator
acts_as_list
database_cleaner
factory_bot_rails (= 6.2.0)
kt-paperclip (~> 7.0.0)
file_validators
launchy (~> 2.5.0)
mysql2
poltergeist (~> 1.18.1)
Expand All @@ -349,4 +359,4 @@ DEPENDENCIES
trustygems (~> 0.2.0)

BUNDLED WITH
2.2.19
2.2.27
1 change: 1 addition & 0 deletions app/assets/stylesheets/admin/partials/_content.scss
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ main {

.reversed main {
.content {
line-height: 1.5;
padding: 1em;
}
}
Expand Down
6 changes: 5 additions & 1 deletion app/controllers/admin/assets_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
class Admin::AssetsController < Admin::ResourceController
paginate_models(per_page: 50)
COMPRESS_FILE_TYPE = ['image/jpeg', 'image/png', 'image/gif', 'image/svg+xml'].freeze
before_action do
ActiveStorage::Current.host = request.base_url
end

def index
assets = Asset.order('created_at DESC')
Expand Down Expand Up @@ -67,6 +70,7 @@ def refresh
private

def compress(uploaded_asset)
require 'open-uri'
data = $kraken.upload(uploaded_asset.tempfile.path, 'lossy' => true)
File.write(uploaded_asset.tempfile, URI.open(data.kraked_url).read, mode: 'wb')
uploaded_asset
Expand All @@ -81,4 +85,4 @@ def set_owner_or_editor
def asset_params
params.permit(:id, :for_attachment, asset: [:caption, :for_attachment, asset: []])
end
end
end
87 changes: 39 additions & 48 deletions app/models/asset.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
class Asset < ActiveRecord::Base
include Paperclip::Glue

has_many :page_attachments, dependent: :destroy
has_many :pages, through: :page_attachments
has_site if respond_to? :has_site
Expand Down Expand Up @@ -33,52 +31,43 @@ class Asset < ActiveRecord::Base
end
}

has_attached_file :asset,
styles: lambda { |attachment|
AssetType.for(attachment).paperclip_styles
},
processors: lambda { |asset|
asset.paperclip_processors
},
whiny: false,
storage: TrustyCms.config['paperclip.storage'],
path: TrustyCms.config['paperclip.path'],
url: TrustyCms.config['paperclip.url'],
fog_credentials: TrustyCmsClippedExtension::Cloud.credentials,
fog_directory: TrustyCms.config['paperclip.fog.directory'],
fog_public: TrustyCms.config['paperclip.fog.public?'] || true,
fog_host: TrustyCmsClippedExtension::Cloud.host,
fog_file: {
'Cache-Control' => 'max-age=31536000',
}

validates_attachment_content_type :asset, content_type: ['application/zip', 'image/jpg', 'image/jpeg', 'image/png', 'image/gif', 'application/pdf', 'application/javascript', 'text/javascript', 'text/css']

has_one_attached :asset
validates :asset,
presence: true,
blob:
{
content_type: %w[application/zip image/jpg image/jpeg image/png image/gif application/pdf text/css],
size_range: 1..5.megabytes,
}
before_save :assign_title
before_save :assign_uuid

after_post_process :read_dimensions

validates_attachment_presence :asset, message: 'You must choose a file to upload!'
if TrustyCms.config['paperclip.skip_filetype_validation'] != 'true' && TrustyCms.config['paperclip.content_types']
validates_attachment_content_type :asset, content_type: TrustyCms.config['paperclip.content_types'].gsub(' ', '').split(',')
else
validates_attachment_presence :asset, message: 'Your uploaded file must have an extension in its name!'
end
validates_attachment_size :asset, less_than: (TrustyCms.config['assets.max_asset_size'] || 5).to_i.megabytes

def asset_type
AssetType.for(asset)
end
delegate :paperclip_processors, :paperclip_styles, :style_dimensions, :style_format, to: :asset_type
delegate :paperclip_processors, :paperclip_styles, :active_storage_styles, :style_dimensions, :style_format,
to: :asset_type

def thumbnail(style_name = 'original')
return asset.url if style_name.to_sym == :original
return asset.url(style_name.to_sym) if style?(style_name)
return asset.url if style_name == 'original'
return asset_variant(style_name).processed.url if asset.variable?

asset_type.icon(style_name)
end

def asset_variant(style_name)
case style_name
when 'thumbnail'
asset.variant(gravity: 'Center', resize: '100x100^', crop: '100x100+0+0')
when 'normal'
asset.variant(gravity: 'Center', resize: '640x640^')
when 'small'
asset.variant(gravity: 'Center', resize: '320x320^')
when 'icon'
asset.variant(gravity: 'Center', resize: '50x50^')
end
end

def style?(style_name = 'original')
style_name == 'original' || paperclip_styles.keys.include?(style_name.to_sym)
end
Expand Down Expand Up @@ -110,16 +99,20 @@ def original_geometry
end

def geometry(style_name = 'original')
raise Paperclip::StyleError, "Requested style #{style_name} is not defined for this asset." unless style?(style_name)
unless style?(style_name)
raise Paperclip::StyleError,
"Requested style #{style_name} is not defined for this asset."
end

@geometry ||= {}
begin
@geometry[style_name] ||= if style_name.to_s == 'original'
original_geometry
else
style = asset.styles[style_name.to_sym]
original_geometry.transformed_by(style.geometry) # this can return dimensions for fully specified style sizes but not for relative sizes when there are no original dimensions
end
original_geometry.transformed_by(style.geometry)
# this can return dimensions for fully specified style sizes but not for relative sizes when there are no original dimensions
end
rescue Paperclip::TransformationError => e
Rails.logger.warn "geometry transformation error: #{e}"
original_geometry # returns a blank geometry if the real geometry cannot be calculated
Expand Down Expand Up @@ -174,19 +167,17 @@ def dimensions_known?
# original file and calculate thumbnail dimensions later, on demand.

def read_dimensions
if image?
if file = asset.queued_for_write[:original]
geometry = Paperclip::Geometry.from_file(file)
self.original_width = geometry.width
self.original_height = geometry.height
self.original_extension = File.extname(file.path)
end
if image? && file = asset.queued_for_write[:original]
geometry = Paperclip::Geometry.from_file(file)
self.original_width = geometry.width
self.original_height = geometry.height
self.original_extension = File.extname(file.path)
end
true
end

def assign_title
self.title = asset_file_name.downcase.sub(original_extension, '').sub('.', '')
self.title = asset.filename.base
end

def assign_uuid
Expand Down Expand Up @@ -235,7 +226,7 @@ def self.thumbnail_names

# this is a convenience for image-pickers
def self.thumbnail_options
asset_sizes = thumbnail_sizes.collect do |k, v|
asset_sizes = thumbnail_sizes.map do |k, v|
size_id = k
size_description = "#{k}: "
size_description << (v.is_a?(Array) ? v.join(' as ') : v)
Expand Down
8 changes: 6 additions & 2 deletions app/models/asset_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ def paperclip_styles
@paperclip_styles
end

def active_storage_styles
@active_storage_styles ||= normalize_style_rules(configured_styles.merge(styles))
end

# Takes a motley collection of differently-defined styles and renders them into the standard hash-of-hashes format.
# Solitary strings are assumed to be #
def normalize_style_rules(styles = {})
Expand Down Expand Up @@ -173,8 +177,8 @@ def define_radius_tags
# class methods

def self.for(attachment)
extension = File.extname(attachment.original_filename).sub(/^\.+/, '')
from_extension(extension) || from_mimetype(attachment.instance_read(:content_type)) || catchall
extension = attachment.record.original_extension
from_extension(extension) || from_mimetype(attachment.content_type) || catchall
end

def self.from_extension(extension)
Expand Down
8 changes: 5 additions & 3 deletions app/views/admin/assets/edit.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
= "#{t("clipped_extension.filename")}: #{@asset.asset_file_name unless @asset.new_record?}"
%br
%label.url
= "#{t("clipped_extension.asset_url")}: #{image_path @asset.thumbnail unless @asset.new_record?}"
= "#{t("clipped_extension.asset_url")}: #{image_path @asset.asset.url unless @asset.new_record?}"
%br
%label.id
= "#{t("clipped_extension.asset_id")}: #{@asset.asset.id unless @asset.new_record?}"
%p.asset
- display_size = TrustyCms::config['assets.display_size'] || 'normal'
= image_tag @asset.thumbnail(display_size.to_sym), :class => 'preview'
= image_tag @asset.thumbnail("normal"), :class => 'preview'

= updated_stamp @asset

Expand Down
6 changes: 0 additions & 6 deletions config/initializers/interpolation.rb

This file was deleted.

1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ en:
asset_page_attachment_note: 'Attached assets. These can be inserted with drag and drop or shown with the <code>&lt;r:assets:each /&gt;</code> tag.'
asset_note: 'Attached assets. '
asset_url: 'Asset URL'
asset_id: 'Asset ID'
assets: 'Assets'
assets_explanation: 'Assets and images can be uploaded and then attached or inserted into pages, snippets and layouts. Click on an asset name above to edit it or click <code>Remove</code> to delete it.'
assets_title: 'Assets'
Expand Down
2 changes: 1 addition & 1 deletion lib/trusty_cms.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

unless defined? TrustyCms::VERSION
module TrustyCms
VERSION = '4.3.4'.freeze
VERSION = '5.0.0'.freeze
end
end
13 changes: 13 additions & 0 deletions lib/trusty_cms/deprecation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'active_support'

class AssetType
def paperclip_processors
ActiveSupport::Deprecation.warn('Paperclip processors will be deprecated soon in favor of ActiveStorage.')

active_storage_styles
end

def paperclip_styles
ActiveSupport::Deprecation.warn('Paperclip styles will be deprecated soon in favor of ActiveStorage.')
end
end
3 changes: 2 additions & 1 deletion trusty_cms.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ a general purpose content managment system--not merely a blogging engine.'
s.summary = 'A no-fluff content management system designed for small teams.'
s.license = 'MIT'
s.test_files = Dir['spec/**/*']

s.add_dependency 'activestorage-validator'
s.add_dependency 'acts_as_list', '>= 0.9.5', '< 1.1.0'
s.add_dependency 'acts_as_tree', '~> 2.9.1'
s.add_dependency 'ckeditor', '>= 4.2.2', '< 4.4.0'
Expand All @@ -33,6 +33,7 @@ a general purpose content managment system--not merely a blogging engine.'
s.add_dependency 'haml'
s.add_dependency 'haml-rails', '~> 2.0'
s.add_dependency 'highline', '>= 1.7.8', '< 2.1.0'
s.add_dependency 'image_processing'
s.add_dependency 'kraken-io'
s.add_dependency 'mini_racer'
s.add_dependency 'mysql2'
Expand Down
Loading

0 comments on commit 5d73a41

Please sign in to comment.