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/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.2'

Class Method Summary collapse

Class Method Details

.clearObject



83
84
85
86
87
88
89
90
# File 'lib/attachs.rb', line 83

def clear
  storage.find_each do |path|
    unless exists?(path)
      Rails.logger.info "Deleting #{path}"
      storage.delete path
    end
  end
end

.configurationObject



31
32
33
# File 'lib/attachs.rb', line 31

def configuration
  @configuration ||= Configuration.new
end

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

Yields:



27
28
29
# File 'lib/attachs.rb', line 27

def configure
  yield configuration
end

.eachObject



52
53
54
55
56
57
58
59
60
# File 'lib/attachs.rb', line 52

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)


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/attachs.rb', line 62

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



102
103
104
105
106
107
108
109
110
# File 'lib/attachs.rb', line 102

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



39
40
41
# File 'lib/attachs.rb', line 39

def interpolations
  @interpolations ||= Interpolations.new
end

.modelsObject



43
44
45
46
47
48
49
50
# File 'lib/attachs.rb', line 43

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



92
93
94
95
96
97
98
99
100
# File 'lib/attachs.rb', line 92

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



35
36
37
# File 'lib/attachs.rb', line 35

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