Replace paperclip with CarrierWave

This commit is contained in:
Karina Kwiatek 2020-05-01 23:43:46 +01:00 committed by Dominik M. Kwiatek
parent 25a83fe153
commit 783e47fc2b
14 changed files with 153 additions and 98 deletions

View File

@ -28,8 +28,6 @@ gem 'bootstrap_form'
gem 'font-kit-rails'
gem 'nprogress-rails'
gem 'font-awesome-rails', '~> 4.7.0'
gem "paperclip", "~> 5.2"
gem 'delayed_paperclip'
gem 'fog-core'
gem 'fog-aws'
gem 'fog-local'
@ -39,6 +37,8 @@ gem 'bootstrap4-datetime-picker-rails'
gem 'tiny-color-rails'
gem 'jquery-minicolors-rails'
gem 'colorize'
gem 'carrierwave', '~> 2.0'
gem 'mini_magick'
gem "rolify", "~> 5.2"
@ -79,6 +79,7 @@ group :development do
gem 'spring', '~> 2.0'
gem 'byebug'
gem 'web-console', '< 4.0.0'
gem 'binding_of_caller'
end
gem 'puma'

View File

@ -68,7 +68,6 @@ GEM
addressable (2.7.0)
public_suffix (>= 2.0.2, < 5.0)
arel (9.0.0)
ast (2.4.0)
autoprefixer-rails (9.7.6)
execjs
bcrypt (3.1.13)
@ -77,6 +76,8 @@ GEM
erubi (>= 1.0.0)
rack (>= 0.9.0)
bindex (0.8.1)
binding_of_caller (0.8.0)
debug_inspector (>= 0.0.1)
bootstrap (4.4.1)
autoprefixer-rails (>= 9.1.0)
popper_js (>= 1.14.3, < 2)
@ -100,7 +101,13 @@ GEM
rack-test (>= 0.6.3)
regexp_parser (~> 1.5)
xpath (~> 3.2)
climate_control (0.2.0)
carrierwave (2.1.0)
activemodel (>= 5.0.0)
activesupport (>= 5.0.0)
addressable (~> 2.6)
image_processing (~> 1.1)
mimemagic (>= 0.3.0)
mini_mime (>= 0.1.3)
cliver (0.3.2)
coderay (1.1.2)
coffee-rails (4.2.2)
@ -115,9 +122,7 @@ GEM
connection_pool (2.2.2)
crass (1.0.6)
database_cleaner (1.8.4)
delayed_paperclip (3.0.1)
activejob (>= 4.2)
paperclip (>= 3.3)
debug_inspector (0.0.3)
devise (4.7.1)
bcrypt (~> 3.0)
orm_adapter (~> 0.1)
@ -177,6 +182,7 @@ GEM
railties (>= 3.2, < 6.1)
font-kit-rails (1.2.0)
rails
foreman (0.87.1)
formatador (0.2.5)
globalid (0.4.2)
activesupport (>= 4.2.0)
@ -197,11 +203,6 @@ GEM
haml (5.1.2)
temple (>= 0.8.0)
tilt
haml_lint (0.35.0)
haml (>= 4.0, < 5.2)
rainbow
rubocop (>= 0.50.0)
sysexits (~> 1.1)
hashie (4.1.0)
http (4.4.1)
addressable (~> 2.3)
@ -221,8 +222,10 @@ GEM
concurrent-ruby (~> 1.0)
i18n-js (3.0.0.rc10)
i18n (~> 0.6)
image_processing (1.10.3)
mini_magick (>= 4.9.5, < 5)
ruby-vips (>= 2.0.17, < 3)
ipaddress (0.8.3)
jaro_winkler (1.5.4)
jbuilder (2.10.0)
activesupport (>= 5.0.0)
jquery-minicolors-rails (2.2.6.2)
@ -250,6 +253,7 @@ GEM
activerecord
kaminari-core (= 1.2.0)
kaminari-core (1.2.0)
kgio (2.11.3)
launchy (2.5.0)
addressable (~> 2.7)
letter_opener (1.7.0)
@ -272,6 +276,7 @@ GEM
mime-types-data (~> 3.2015)
mime-types-data (3.2019.1009)
mimemagic (0.3.4)
mini_magick (4.10.1)
mini_mime (1.0.2)
mini_portile2 (2.4.0)
minitest (5.14.0)
@ -309,15 +314,6 @@ GEM
omniauth-oauth (~> 1.1)
rack
orm_adapter (0.5.0)
paperclip (5.3.0)
activemodel (>= 4.2.0)
activesupport (>= 4.2.0)
mime-types
mimemagic (~> 0.3.0)
terrapin (~> 0.6.0)
parallel (1.19.1)
parser (2.7.1.2)
ast (~> 2.4.0)
pg (1.2.3)
pghero (2.4.2)
activerecord (>= 5)
@ -386,7 +382,7 @@ GEM
method_source
rake (>= 0.8.7)
thor (>= 0.19.0, < 2.0)
rainbow (3.0.0)
raindrops (0.19.1)
rake (13.0.1)
rb-fsevent (0.10.3)
rb-inotify (0.10.1)
@ -398,7 +394,6 @@ GEM
responders (3.0.0)
actionpack (>= 5.0)
railties (>= 5.0)
rexml (3.2.4)
rolify (5.2.0)
rspec-core (3.9.1)
rspec-support (~> 3.9.1)
@ -423,15 +418,9 @@ GEM
rspec-core (~> 3.0, >= 3.0.0)
sidekiq (>= 2.4.0)
rspec-support (3.9.2)
rubocop (0.82.0)
jaro_winkler (~> 1.5.1)
parallel (~> 1.10)
parser (>= 2.7.0.1)
rainbow (>= 2.2.2, < 4.0)
rexml
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 2.0)
ruby-progressbar (1.10.1)
ruby-vips (2.0.17)
ffi (~> 1.9)
sanitize (5.1.0)
crass (~> 1.0.2)
nokogiri (>= 1.8.0)
@ -481,10 +470,7 @@ GEM
sprockets (>= 3.0.0)
sweetalert-rails (1.1.3)
railties (>= 3.1.0)
sysexits (1.2.0)
temple (0.8.2)
terrapin (0.6.0)
climate_control (>= 0.0.3, < 1.0)
thor (1.0.1)
thread_safe (0.3.6)
tilt (2.0.10)
@ -511,7 +497,9 @@ GEM
unf (0.1.4)
unf_ext
unf_ext (0.0.7.7)
unicode-display_width (1.7.0)
unicorn (5.5.4)
kgio (~> 2.6)
raindrops (~> 0.7)
warden (1.2.8)
rack (>= 2.0.6)
web-console (3.7.0)
@ -531,16 +519,17 @@ PLATFORMS
DEPENDENCIES
bcrypt (~> 3.1.7)
better_errors
binding_of_caller
bootstrap (~> 4.4, >= 4.4.1)
bootstrap4-datetime-picker-rails
bootstrap_form
brakeman
byebug
capybara
carrierwave (~> 2.0)
coffee-rails (~> 4.1)
colorize
database_cleaner
delayed_paperclip
devise (~> 4.0)
devise-async
devise-i18n
@ -552,9 +541,9 @@ DEPENDENCIES
fog-local
font-awesome-rails (~> 4.7.0)
font-kit-rails
foreman
guard-brakeman
haml (~> 5.0)
haml_lint
httparty
i18n-js (= 3.0.0.rc10)
jbuilder (~> 2.10)
@ -562,6 +551,7 @@ DEPENDENCIES
jquery-rails
jquery-turbolinks
letter_opener
mini_magick
moment-timezone-rails (~> 1.0)
momentjs-rails (>= 2.9.0)
newrelic_rpm
@ -569,7 +559,6 @@ DEPENDENCIES
omniauth
omniauth-tumblr
omniauth-twitter
paperclip (~> 5.2)
pg
pghero
poltergeist
@ -603,6 +592,7 @@ DEPENDENCIES
turbolinks (~> 2.5.3)
twitter
uglifier (>= 1.3.0)
unicorn
web-console (< 4.0.0)
BUNDLED WITH

View File

@ -54,20 +54,8 @@ class User < ApplicationRecord
validates :display_name, length: { maximum: 50 }
validates :bio, length: { maximum: 200 }
# validates :website, format: { with: WEBSITE_REGEX }
has_attached_file :profile_picture, styles: { large: "500x500#", medium: "256x256#", small: "80x80#" },
default_url: "/images/:style/no_avatar.png", use_timestamp: false,
processors: [:cropper]
validates_attachment_content_type :profile_picture, :content_type => /\Aimage\/(png|jpe?g|gif)\Z/
has_attached_file :profile_header, styles: { web: "1500x350#", mobile: "450x105#", retina: "900x210#" },
default_url: '/images/header/:style/no_header.jpg', use_timestamp: false,
processors: [:cropper]
validates_attachment_content_type :profile_header, :content_type => /\Aimage\/(png|jpe?g)\Z/
process_in_background :profile_picture
process_in_background :profile_header
mount_uploader :profile_picture, ProfilePictureUploader, mount_on: :profile_picture_file_name
mount_uploader :profile_header, ProfileHeaderUploader, mount_on: :profile_header_file_name
before_save do
self.website = if website.match %r{\Ahttps?://}

View File

@ -0,0 +1,43 @@
class ProfileHeaderUploader < CarrierWave::Uploader::Base
include CarrierWave::Compatibility::Paperclip
include CarrierWave::MiniMagick
# Choose what kind of storage to use for this uploader:
storage :fog
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"/system/uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
# Provide a default URL as a default if there hasn't been a file uploaded:
def default_url(*args)
# For Rails 3.1+ asset pipeline compatibility:
# ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
"/images/header/" + [version_name, "no_header.jpg"].compact.join('/')
end
version :web do
process resize_to_fit: [1500, 350]
end
version :mobile do
process resize_to_fit: [450, 105]
end
version :retina do
process resize_to_fit: [900, 210]
end
# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
# def extension_whitelist
# %w(jpg jpeg gif png)
# end
# Override the filename of the uploaded files:
# Avoid using model.id or version_name here, see uploader/store.rb for details.
# def filename
# "something.jpg" if original_filename
# end
end

View File

@ -0,0 +1,44 @@
class ProfilePictureUploader < CarrierWave::Uploader::Base
include CarrierWave::Compatibility::Paperclip
include CarrierWave::MiniMagick
# Choose what kind of storage to use for this uploader:
storage :fog
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"/system/uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
# Provide a default URL as a default if there hasn't been a file uploaded:
def default_url(*args)
# For Rails 3.1+ asset pipeline compatibility:
# ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
"/images/
" + [version_name, "no_avatar.png"].compact.join('/')
end
version :large do
process resize_to_fit: [500, 500]
end
version :medium do
process resize_to_fit: [256, 256]
end
version :small do
process resize_to_fit: [80, 80]
end
# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
# def extension_whitelist
# %w(jpg jpeg gif png)
# end
# Override the filename of the uploaded files:
# Avoid using model.id or version_name here, see uploader/store.rb for details.
# def filename
# "something.jpg" if original_filename
# end
end

View File

@ -29,14 +29,8 @@ Rails.application.configure do
# Action Mailer Setup
# if you want to test sending mails locally, uncomment the line below and comment the :sendmail line
# config.action_mailer.delivery_method = :letter_opener
if ENV["mailcatcher"] == "yes"
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = { :address => "localhost", :port => 1025 }
elsif ENV["letteropener"] == "yes"
config.action_mailer.delivery_method = :letter_opener
else
config.action_mailer.delivery_method = :sendmail
end
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = { :address => "localhost", :port => 1025 }
# Don't care if the mailer can't send.
config.action_mailer.raise_delivery_errors = false

View File

@ -0,0 +1,12 @@
CarrierWave.configure do |config|
config.fog_credentials = {
:provider => "Local",
:local_root => "#{Rails.root}/public",
}
config.fog_directory = "/system"
unless APP_CONFIG["fog"].nil?
config.fog_credentials = APP_CONFIG["fog"]["credentials"] unless APP_CONFIG["fog"]["credentials"].nil?
config.fog_directory = APP_CONFIG["fog"]["directory"] unless APP_CONFIG["fog"]["directory"].nil?
end
end

View File

@ -1,14 +0,0 @@
Paperclip::Attachment.default_options[:storage] = :fog
Paperclip::Attachment.default_options[:fog_credentials] = {:provider => "Local", :local_root => "#{Rails.root}/public"}
Paperclip::Attachment.default_options[:fog_directory] = "/system"
Paperclip::Attachment.default_options[:fog_host] = "/system"
unless APP_CONFIG["fog"].nil?
Paperclip::Attachment.default_options[:fog_credentials] = APP_CONFIG["fog"]["credentials"] unless APP_CONFIG["fog"]["credentials"].nil?
Paperclip::Attachment.default_options[:fog_directory] = APP_CONFIG["fog"]["directory"] unless APP_CONFIG["fog"]["directory"].nil?
Paperclip::Attachment.default_options[:fog_host] = APP_CONFIG["fog"]["host"] unless APP_CONFIG["fog"]["host"].nil?
if not APP_CONFIG["fog"]["credentials"].nil? and APP_CONFIG["fog"]["host"].nil?
Paperclip::Attachment.default_options[:fog_host] = nil
end
end

View File

@ -58,7 +58,7 @@ fog:
aws_access_key_id: 'ACCESS KEY'
aws_secret_access_key: 'SECRET KEY'
region: 'space-pluto-1'
host: 's3.wherever.com'
# bucket name, required
directory: 'retrospring'
# URL host, comment out to use default, GENERALLY you don't want to define this
# host: ''

View File

@ -1,15 +0,0 @@
# I seriously hope you guys don't do this.
class Paperclip::FileCommandContentTypeDetector
alias old_type_from_file_command type_from_file_command
def type_from_file_command
default = old_type_from_file_command
if default.strip == 'text/x-c' or default.strip == 'text/plain' or default.strip == 'text/stylesheet'
'text/css'
else
default
end
end
end
# Here be monkey patches.

View File

@ -1,11 +1,17 @@
class AddAttachmentProfilePictureToUsers < ActiveRecord::Migration[4.2]
def self.up
change_table :users do |t|
t.attachment :profile_picture
t.string :profile_picture_file_name
t.string :profile_picture_content_type
t.bigint :profile_picture_file_size
t.datetime :profile_picture_updated_at
end
end
def self.down
remove_attachment :users, :profile_picture
remove_column :users, :profile_picture_file_name
remove_column :users, :profile_picture_content_type
remove_column :users, :profile_picture_file_size
remove_column :users, :profile_picture_updated_at
end
end

View File

@ -1,7 +1,10 @@
class AddAttachmentProfileHeaderToUsers < ActiveRecord::Migration[4.2]
def change
change_table :users do |t|
t.attachment :profile_header
t.string :profile_header_file_name
t.string :profile_header_content_type
t.bigint :profile_header_file_size
t.datetime :profile_header_updated_at
t.boolean :profile_header_processing
t.integer :crop_h_x
t.integer :crop_h_y

View File

@ -30,7 +30,10 @@ class CreateThemes < ActiveRecord::Migration[4.2]
t.integer :background_text, limit: 4, default: 0x222222
t.integer :background_muted, limit: 4, default: 0xBBBBBB
t.attachment :css
t.string :css_file_name
t.string :css_content_type
t.bigint :css_file_size
t.datetime :css_updated_at
t.timestamps null: false
end

View File

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2020_05_09_094402) do
ActiveRecord::Schema.define(version: 2020_05_13_154602) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -250,7 +250,7 @@ ActiveRecord::Schema.define(version: 2020_05_09_094402) do
t.text "bio", default: "", null: false
t.string "profile_picture_file_name"
t.string "profile_picture_content_type"
t.integer "profile_picture_file_size"
t.bigint "profile_picture_file_size"
t.datetime "profile_picture_updated_at"
t.boolean "profile_picture_processing"
t.integer "crop_x"
@ -267,7 +267,7 @@ ActiveRecord::Schema.define(version: 2020_05_09_094402) do
t.integer "comment_smiled_count", default: 0, null: false
t.string "profile_header_file_name"
t.string "profile_header_content_type"
t.integer "profile_header_file_size"
t.bigint "profile_header_file_size"
t.datetime "profile_header_updated_at"
t.boolean "profile_header_processing"
t.integer "crop_h_x"