Skip to content

Instantly share code, notes, and snippets.

@vala
Created July 4, 2017 21:52
Show Gist options
  • Save vala/2eb26c4b057c46551230247b5dc6d788 to your computer and use it in GitHub Desktop.
Save vala/2eb26c4b057c46551230247b5dc6d788 to your computer and use it in GitHub Desktop.
Reprocess all attachments from all models of the app and engines
class PaperclipGlobalReprocessor
def run
models.each do |model|
attachments = model.try(:attachment_definitions).try(:keys)
next unless attachments
model.find_each do |resource|
attachments.each do |attachment|
resource.send(attachment).reprocess! if resource.send(:"#{ attachment }?")
end
end
end
end
private
# Get all engines to ensure that we lookup all standard places where models
# can live
def engines
[Rails.application] + Rails::Engine.subclasses.map(&:instance)
end
# Find all "app/models" folder in the engines and fetch all model files.
def models
engines.flat_map do |app|
(app.paths['app/models'].to_a + app.config.autoload_paths).map do |load_path|
Dir.glob(app.root.join(load_path)).map do |load_dir|
Dir.glob(load_dir + '/**/*.rb').map do |filename|
next unless filename.match(/\/models\//)
# app/models/module/class.rb => module/class.rb
model_name = filename.to_s.reverse.chomp("#{app.root.join(load_dir)}/".reverse).reverse
# module/class.rb => module/class => Module::Class
begin
model_name.chomp('.rb').camelize.constantize
rescue NameError, LoadError
nil
end
end
end
end
end.flatten.compact.uniq
end
end
PaperclipGlobalReprocessor.new.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment