Module: Attachs

Defined in:
lib/attachs.rb,
lib/attachs/builder.rb,
lib/attachs/concern.rb,
lib/attachs/console.rb,
lib/attachs/railtie.rb,
lib/attachs/version.rb,
lib/attachs/jobs/base.rb,
lib/attachs/attachment.rb,
lib/attachs/collection.rb,
lib/attachs/storages/s3.rb,
lib/attachs/configuration.rb,
lib/attachs/storages/base.rb,
lib/attachs/interpolations.rb,
lib/attachs/jobs/delete_job.rb,
lib/attachs/jobs/update_job.rb,
lib/attachs/processors/base.rb,
lib/attachs/processors/image.rb,
lib/attachs/attachment/attributes.rb,
lib/attachs/attachment/processing.rb,
lib/attachs/extensions/active_record/base.rb,
lib/generators/attachs/upload/upload_generator.rb,
lib/generators/attachs/install/install_generator.rb,
lib/attachs/extensions/active_record/validations/attachment_validator.rb,
lib/attachs/extensions/active_record/validations/attachment_size_validator.rb,
lib/attachs/extensions/active_record/validations/attachment_presence_validator.rb,
lib/attachs/extensions/active_record/validations/attachment_content_type_validator.rb

Defined Under Namespace

Modules: Concern, Extensions, Generators, Jobs, Processors, Storages Classes: Attachment, Builder, Collection, Configuration, Console, Interpolations, Railtie

Constant Summary collapse

VERSION =
'4.0.0.5'

Class Method Summary collapse

Class Method Details

.clearObject



85
86
87
88
89
90
91
92
# 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

.configurationObject



33
34
35
# File 'lib/attachs.rb', line 33

def configuration
  @configuration ||= Configuration.new
end

.configure {|configuration| ... } ⇒ Object

Yields:



29
30
31
# File 'lib/attachs.rb', line 29

def configure
  yield configuration
end

.eachObject



54
55
56
57
58
59
60
61
62
# 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) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# 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_missingsObject



104
105
106
107
108
109
110
111
112
# 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

.interpolationsObject



41
42
43
# File 'lib/attachs.rb', line 41

def interpolations
  @interpolations ||= Interpolations.new
end

.modelsObject



45
46
47
48
49
50
51
52
# 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

.reprocessObject



94
95
96
97
98
99
100
101
102
# 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

.storageObject



37
38
39
# File 'lib/attachs.rb', line 37

def storage
  @storage ||= Storages::S3.new
end