add creation of ActiveStorage tables
This commit is contained in:
parent
c92c172f2d
commit
cdd28b3dac
2 changed files with 40 additions and 17 deletions
|
@ -2,6 +2,10 @@
|
|||
class CwToActivestorageGenerator < Rails::Generators::Base
|
||||
source_root File.expand_path('templates', __dir__)
|
||||
|
||||
def install_activestorage_tables
|
||||
rake 'active_storage:install'
|
||||
end
|
||||
|
||||
def copy_uploaders
|
||||
if (!File.exists?('app/uploaders'))
|
||||
directory 'uploaders', 'app/uploaders'
|
||||
|
|
|
@ -1,28 +1,47 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later
|
||||
|
||||
|
||||
|
||||
namespace :houdini do
|
||||
namespace :migration do
|
||||
UPLOADERS_TO_MIGRATE = {
|
||||
'Nonprofit': [
|
||||
:main_image,
|
||||
:second_image,
|
||||
:third_image,
|
||||
:background_image,
|
||||
:logo
|
||||
],
|
||||
'Campaign': [
|
||||
:main_image,
|
||||
:background_image,
|
||||
:banner_image
|
||||
],
|
||||
'Event': [
|
||||
:main_image,
|
||||
:background_image
|
||||
],
|
||||
'ImageAttachment': [
|
||||
:file
|
||||
],
|
||||
'Profile':[
|
||||
:picture
|
||||
]
|
||||
}
|
||||
|
||||
desc "Migrate your CarrierWave uploads to activestorage"
|
||||
task :cw_to_activestorage, [:simulate, :write_out_to_files] => [:environment] do |t, args|
|
||||
task :cw_to_activestorage, [:old_carrierwave_root, :simulate, :write_out_to_files] => [:environment] do |t, args|
|
||||
results = []
|
||||
Rails.application.eager_load!
|
||||
# find activerecord descendents
|
||||
classes_with_uploaders = ActiveRecord::Base.descendants.select do |cls|
|
||||
cls.uploaders&.any?
|
||||
end
|
||||
classes_with_uploaders.each do |cls|
|
||||
cls.find_each do |i|
|
||||
cls.uploaders.keys.each do |uploader_name|
|
||||
upload_url = i.send("#{uploader_name.to_s}_url")
|
||||
result = process({upload_url: upload_url,
|
||||
uploader_name: uploader_name,
|
||||
record: i,
|
||||
})
|
||||
if (result)
|
||||
result[:value] = [cls.name].concat(result[:value])
|
||||
results << result
|
||||
end
|
||||
UPLOADERS_TO_MIGRATE.each do |k,attributes|
|
||||
klass = Object.get_const(k)
|
||||
klass.where(attributes.map{|i| i.to_s + "IS NOT NULL"}.join(" OR "))
|
||||
.find_each do |record|
|
||||
attributes.each do |attrib|
|
||||
cw_path = record.read_attribute_before_type_cast(attrib.to_s)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue