Initial generator for beginning migration from carrierwave to activestorage

This commit is contained in:
Eric 2020-04-30 15:39:25 -05:00
parent 54dcc45e4a
commit c92c172f2d
21 changed files with 711 additions and 0 deletions

View file

@ -0,0 +1,8 @@
Description:
Explain the generator
Example:
rails generate cw_to_activestorage
This will create:
what/will/it/create

View file

@ -0,0 +1,39 @@
# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later
class CwToActivestorageGenerator < Rails::Generators::Base
source_root File.expand_path('templates', __dir__)
def copy_uploaders
if (!File.exists?('app/uploaders'))
directory 'uploaders', 'app/uploaders'
end
end
def include_uploaders
file_and_search = [
["campaign.rb", "class Campaign < ApplicationRecord\n"],
["profile.rb", "class Profile < ApplicationRecord\n"],
['nonprofit.rb', "class Nonprofit < ApplicationRecord\n"],
['image_attachment.rb', "class ImageAttachment < ApplicationRecord\n"],
['event.rb', "class Event < ApplicationRecord\n"]
]
file_and_search
.select{|filename, _| !File.read("app/models/#{filename}").include?('###MIGRATION_FIELDS_BEGIN')}\
.each do |filename, find_string|
gsub_file("app/models/#{filename}",find_string ) do |match|
match << File.read(Pathname(File.expand_path('templates', __dir__, )) + 'models' + "#{filename}")
end
end
end
def create_column_migration_file
if (Dir.glob("db/migrate/*_rename_uploader_columns.rb").none?)
copy_file "migrate/rename_uploader_columns.rb",
"db/migrate/#{DateTime.now.strftime('%Y%m%d%H%M%S')}_rename_uploader_columns.rb"
end
end
def add_carrierwave_gems
gem "carrierwave", "~> 1"
gem "carrierwave-aws"
end
end

View file

@ -0,0 +1,17 @@
# frozen_string_literal: true
# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later
CarrierWave.configure do |config|
config.storage = :aws
config.aws_bucket = ENV['CARRIERWAVE_AWS_BUCKET']
config.aws_acl = :public_read
config.asset_host = ENV['CARRIERWAVE_IMAGE_HOST'] || "https://#{config.aws_bucket}.s3.amazonaws.com"
# config.aws_authenticated_url_expiration = 60 * 60 * 24 * 365
# config.aws_credentials = {
# access_key_id: ENV['CARRIERWAVE_AWS_ACCESS_KEY_ID'],
# secret_access_key: ENV['CARRIERWAVE_SECRET_ACCESS_KEY'],
# config: AWS.config(cache_dir: "#{Rails.root}/tmp/uploads", region: ENV['CARRIERWAVE_SECRET_ACCESS_KEY'])
# }
end

View file

@ -0,0 +1,19 @@
# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later
class RenameUploaderColumns < ActiveRecord::Migration[5.2]
rename_column :campaigns, :main_image, :main_image_temp
rename_column :campaigns, :background_image, :background_image_temp
rename_column :campaigns, :banner_image, :banner_image_temp
rename_column :events, :main_image, :main_image_temp
rename_column :events, :background_image, :background_image_temp
rename_column :image_attachments, :file, :file_temp
rename_column :nonprofits, :main_image, :main_image_temp
rename_column :nonprofits, :second_image, :second_image_temp
rename_column :nonprofits, :third_image, :third_image_temp
rename_column :nonprofits, :background_image, :background_image_temp
rename_column :nonprofits, :logo_image, :logo_image_temp
rename_column :profiles, :picture, :picture_temp
end

View file

@ -0,0 +1,5 @@
###MIGRATION_FIELDS_BEGIN
mount_uploader :main_image_temp, CampaignMainImageUploader
mount_uploader :background_image_temp, CampaignBackgroundImageUploader
mount_uploader :banner_image_temp, CampaignBannerImageUploader
###MIGRATION_FIELDS_END

View file

@ -0,0 +1,4 @@
###MIGRATION_FIELDS_BEGIN
mount_uploader :main_image_temp, EventMainImageUploader
mount_uploader :background_image_temp, EventBackgroundImageUploader
###MIGRATION_FIELDS_END

View file

@ -0,0 +1,3 @@
###MIGRATION_FIELDS_BEGIN
mount_uploader :file_temp, ImageAttachmentUploader
###MIGRATION_FIELDS_END

View file

@ -0,0 +1,7 @@
###MIGRATION_FIELDS_BEGIN
mount_uploader :main_image_temp, NonprofitUploader
mount_uploader :second_image_temp, NonprofitUploader
mount_uploader :third_image_temp, NonprofitUploader
mount_uploader :background_image_temp, NonprofitBackgroundUploader
mount_uploader :logo_temp, NonprofitLogoUploader
###MIGRATION_FIELDS_END

View file

@ -0,0 +1,3 @@
###MIGRATION_FIELDS_BEGIN
mount_uploader :picture_temp, ProfileUploader
###MIGRATION_FIELDS_END

View file

@ -0,0 +1,54 @@
# frozen_string_literal: true
# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later
# encoding: utf-8
class ArticleBackgroundUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
# include CarrierWave::RMagick
include CarrierWave::MiniMagick
# Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility:
# include Sprockets::Helpers::RailsHelper
# include Sprockets::Helpers::IsolatedHelper
# 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
"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
# For Rails 3.1+ asset pipeline compatibility:
Image::DefaultNonprofitUrl
end
# Process files as they are uploaded:
# process :scale => [200, 300]
#
# def scale(width, height)
# # do something
# end
# Create different versions of your uploaded files:
version :large do
process resize_to_fill: [600, 400]
end
# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
def extension_white_list
%w[jpg jpeg 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
def cache_dir
"#{Rails.root}/tmp/uploads"
end
end

View file

@ -0,0 +1,54 @@
# frozen_string_literal: true
# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later
# encoding: utf-8
class ArticleUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
# include CarrierWave::RMagick
include CarrierWave::MiniMagick
# Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility:
# include Sprockets::Helpers::RailsHelper
# include Sprockets::Helpers::IsolatedHelper
# 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
"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
# For Rails 3.1+ asset pipeline compatibility:
Image::DefaultNonprofitUrl
end
# Process files as they are uploaded:
# process :scale => [200, 300]
#
# def scale(width, height)
# # do something
# end
# Create different versions of your uploaded files:
version :thumb do
process resize_to_fill: [200, 200]
end
# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
def extension_white_list
%w[jpg jpeg 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
def cache_dir
"#{Rails.root}/tmp/uploads"
end
end

View file

@ -0,0 +1,25 @@
# frozen_string_literal: true
# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later
# encoding: utf-8
class CampaignBackgroundImageUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
def store_dir
"uploads/campaigns/#{mounted_as}/#{model.id}"
end
# Create different versions of your uploaded files:
version :normal do
process resize_to_fill: [1000, 600]
end
def extension_white_list
%w[jpg jpeg png]
end
def cache_dir
"#{Rails.root}/tmp/uploads"
end
end

View file

@ -0,0 +1,18 @@
# frozen_string_literal: true
# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later
class CampaignBannerImageUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
def store_dir
"uploads/campaigns/#{mounted_as}/#{model.id}"
end
def extension_white_list
%w[jpg jpeg png]
end
def cache_dir
"#{Rails.root}/tmp/uploads"
end
end

View file

@ -0,0 +1,56 @@
# frozen_string_literal: true
# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later
class CampaignMainImageUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
# include CarrierWave::RMagick
include CarrierWave::MiniMagick
# Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility:
# include Sprockets::Helpers::RailsHelper
# include Sprockets::Helpers::IsolatedHelper
# 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
"uploads/campaigns/#{mounted_as}/#{model.id}"
end
# Provide a default URL as a default if there hasn't been a file uploaded:
def default_url
# For Rails 3.1+ asset pipeline compatibility:
Image::DefaultProfileUrl
end
# Process files as they are uploaded:
# process :scale => [200, 300]
#
# def scale(width, height)
# # do something
# end
# Create different versions of your uploaded files:
version :normal do
process resize_to_fill: [524, 360]
end
version :thumb do
process resize_to_fill: [180, 150]
end
# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
def extension_white_list
%w[jpg jpeg 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
def cache_dir
"#{Rails.root}/tmp/uploads"
end
end

View file

@ -0,0 +1,52 @@
# frozen_string_literal: true
# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later
class EventBackgroundImageUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
# include CarrierWave::RMagick
include CarrierWave::MiniMagick
# Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility:
# include Sprockets::Helpers::RailsHelper
# include Sprockets::Helpers::IsolatedHelper
# 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
"uploads/events/#{mounted_as}/#{model.id}"
end
# Provide a default URL as a default if there hasn't been a file uploaded:
def default_url
# For Rails 3.1+ asset pipeline compatibility:
Image::DefaultCampaignUrl
end
# Process files as they are uploaded:
# process :scale => [200, 300]
#
# def scale(width, height)
# # do something
# end
# Create different versions of your uploaded files:
version :normal do
process resize_to_fill: [1000, 600]
end
# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
def extension_white_list
%w[jpg jpeg 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
def cache_dir
"#{Rails.root}/tmp/uploads"
end
end

View file

@ -0,0 +1,56 @@
# frozen_string_literal: true
# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later
class EventMainImageUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
# include CarrierWave::RMagick
include CarrierWave::MiniMagick
# Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility:
# include Sprockets::Helpers::RailsHelper
# include Sprockets::Helpers::IsolatedHelper
# 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
"uploads/events/#{mounted_as}/#{model.id}"
end
# Provide a default URL as a default if there hasn't been a file uploaded:
def default_url
# For Rails 3.1+ asset pipeline compatibility:
Image::DefaultProfileUrl
end
# Process files as they are uploaded:
# process :scale => [200, 300]
#
# def scale(width, height)
# # do something
# end
# Create different versions of your uploaded files:
version :normal do
process resize_to_fill: [400, 400]
end
version :thumb do
process resize_to_fill: [100, 100]
end
# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
def extension_white_list
%w[jpg jpeg 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
def cache_dir
"#{Rails.root}/tmp/uploads"
end
end

View file

@ -0,0 +1,64 @@
# frozen_string_literal: true
# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later
# encoding: utf-8
class ImageAttachmentUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
# include CarrierWave::RMagick
include CarrierWave::MiniMagick
# Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility:
# include Sprockets::Helpers::RailsHelper
# include Sprockets::Helpers::IsolatedHelper
# 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
"uploads/image_attachments/#{mounted_as}/#{model.id}"
end
# Provide a default URL as a default if there hasn't been a file uploaded:
def default_url
# For Rails 3.1+ asset pipeline compatibility:
Image::DefaultProfileUrl
end
# Process files as they are uploaded:
# process :scale => [200, 300]
#
# def scale(width, height)
# # do something
# end
# Create different versions of your uploaded files:
version :large do
process resize_to_fill: [600, 400]
end
version :medium do
process resize_to_fill: [400, 266]
end
version :small do
process resize_to_fill: [400, 266]
end
# slightly smaller than the normal thumb
version :thumb_explore do
process resize_to_fill: [200, 133]
end
# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
def extension_white_list
%w[jpg jpeg 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
def cache_dir
"#{Rails.root}/tmp/uploads"
end
end

View file

@ -0,0 +1,55 @@
# frozen_string_literal: true
# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later
# encoding: utf-8
class NonprofitBackgroundUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
# include CarrierWave::RMagick
include CarrierWave::MiniMagick
# Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility:
# include Sprockets::Helpers::RailsHelper
# include Sprockets::Helpers::IsolatedHelper
# 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
"uploads/npo/#{mounted_as}/#{model.id}"
end
# Provide a default URL as a default if there hasn't been a file uploaded:
def default_url
# For Rails 3.1+ asset pipeline compatibility:
Image::DefaultNonprofitUrl
end
# Process files as they are uploaded:
# process :scale => [200, 300]
#
# def scale(width, height)
# # do something
# end
# Create different versions of your uploaded files:
version :normal do
process resize_to_fill: [1000, 600]
end
# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
def extension_white_list
%w[jpg jpeg 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
def cache_dir
"#{Rails.root}/tmp/uploads"
end
end

View file

@ -0,0 +1,51 @@
# frozen_string_literal: true
# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later
# encoding: utf-8
class NonprofitLogoUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
# include CarrierWave::RMagick
include CarrierWave::MiniMagick
# Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility:
# include Sprockets::Helpers::RailsHelper
# include Sprockets::Helpers::IsolatedHelper
# 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
"uploads/npo/#{mounted_as}/#{model.id}"
end
# Provide a default URL as a default if there hasn't been a file uploaded:
def default_url
# For Rails 3.1+ asset pipeline compatibility:
Image::DefaultProfileUrl
end
# Create different versions of your uploaded files:
version :large do
process resize_to_fit: [180, 180]
end
version :normal do
process resize_to_fit: [100, 100]
end
version :small do
process resize_to_fit: [30, 30]
end
def extension_white_list
%w[jpg jpeg png gif]
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
def cache_dir
"#{Rails.root}/tmp/uploads"
end
end

View file

@ -0,0 +1,61 @@
# frozen_string_literal: true
# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later
# encoding: utf-8
class NonprofitUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
# include CarrierWave::RMagick
include CarrierWave::MiniMagick
# Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility:
# include Sprockets::Helpers::RailsHelper
# include Sprockets::Helpers::IsolatedHelper
# 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
"uploads/npo/#{mounted_as}/#{model.id}"
end
# Provide a default URL as a default if there hasn't been a file uploaded:
def default_url
# For Rails 3.1+ asset pipeline compatibility:
Image::DefaultProfileUrl
end
# Process files as they are uploaded:
# process :scale => [200, 300]
#
# def scale(width, height)
# # do something
# end
# Create different versions of your uploaded files:
version :nonprofit_carousel do
process resize_to_fill: [590, 338]
end
version :thumb do
process resize_to_fill: [188, 120]
end
# slightly smaller than the normal thumb
version :thumb_explore do
process resize_to_fill: [100, 100]
end
# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
def extension_white_list
%w[jpg jpeg 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
def cache_dir
"#{Rails.root}/tmp/uploads"
end
end

View file

@ -0,0 +1,60 @@
# frozen_string_literal: true
# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later
# encoding: utf-8
class ProfileUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
# include CarrierWave::RMagick
include CarrierWave::MiniMagick
# Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility:
# include Sprockets::Helpers::RailsHelper
# include Sprockets::Helpers::IsolatedHelper
# 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
"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
# For Rails 3.1+ asset pipeline compatibility:
Image::DefaultProfileUrl
end
# Process files as they are uploaded:
# process :scale => [200, 300]
#
# def scale(width, height)
# # do something
# end
# Create different versions of your uploaded files:
version :normal do
process resize_to_fill: [150, 150]
end
version :medium do
process resize_to_fill: [100, 100]
end
version :tiny do
process resize_to_fill: [50, 50]
end
# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
def extension_white_list
%w[jpg jpeg 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
def cache_dir
"#{Rails.root}/tmp/uploads"
end
end