module Attachs

Constants

VERSION

Public Class Methods

clear() click to toggle source
# File lib/attachs.rb, line 85
def clear
  storage.find_each do |path|
    unless exists?(path)
      Rails.logger.info "Deleting #{path}"
      storage.delete path
    end
  end
end
configuration() click to toggle source
# File lib/attachs.rb, line 33
def configuration
  @configuration ||= Configuration.new
end
configure() { |configuration| ... } click to toggle source
# File lib/attachs.rb, line 29
def configure
  yield configuration
end
each() { |send| ... } click to toggle source
# File lib/attachs.rb, line 54
def each
  models.each do |model|
    model.find_each do |record|
      model.attachments.each do |attribute, options|
        yield record.send(attribute)
      end
    end
  end
end
exists?(path) click to toggle source
# File lib/attachs.rb, line 64
def exists?(path)
  queries = models.map do |model|
    model.attachments.map do |attribute, options|
      joins = []
      if options[:multiple]
        joins << "LEFT JOIN JSONB_ARRAY_ELEMENTS(#{model.table_name}.#{attribute}) attachments ON TRUE"
        joins << "LEFT JOIN JSONB_EACH_TEXT(attachments->'paths') paths ON TRUE"
        joins << "LEFT JOIN JSONB_ARRAY_ELEMENTS_TEXT(attachments->'old_paths') old_paths ON TRUE"
      else
        joins << "LEFT JOIN JSONB_EACH_TEXT(#{model.table_name}.#{attribute}->'paths') paths ON TRUE"
        joins << "LEFT JOIN JSONB_ARRAY_ELEMENTS_TEXT(#{model.table_name}.#{attribute}->'old_paths') old_paths ON TRUE"
      end
      joins = joins.join(' ')
      conditions = ['paths.value = :path OR old_paths.value = :path', path: path]
      model.select('1 AS one').from(model.table_name).joins(joins).where(conditions).to_sql
    end
  end
  query = queries.flatten.join(' UNION ')
  ActiveRecord::Base.connection.execute(query).any?
end
fix_missings() click to toggle source
# File lib/attachs.rb, line 104
def fix_missings
  each do |attachment|
    class_name = attachment.record.class.name
    id = attachment.record.id
    attribute = attachment.record_attribute
    Rails.logger.info "Fix missings of #{class_name} ##{id} => #{attribute}"
    attachment.fix_missings
  end
end
interpolations() click to toggle source
# File lib/attachs.rb, line 41
def interpolations
  @interpolations ||= Interpolations.new
end
models() click to toggle source
# File lib/attachs.rb, line 45
def models
  if Rails.configuration.cache_classes == false
    Rails.application.eager_load!
  end
  ActiveRecord::Base.descendants.select do |model|
    model.included_modules.include?(Attachs::Concern) && model.descendants.none?
  end
end
reprocess() click to toggle source
# File lib/attachs.rb, line 94
def reprocess
  each do |attachment|
    class_name = attachment.record.class.name
    id = attachment.record.id
    attribute = attachment.record_attribute
    Rails.logger.info "Reprocessing #{class_name} ##{id} => #{attribute}"
    attachment.reprocess
  end
end
storage() click to toggle source
# File lib/attachs.rb, line 37
def storage
  @storage ||= Storages::S3.new
end