Module: Alchemy::StorageAdapter::ActiveStorage

Extended by:
ActiveStorage
Included in:
ActiveStorage
Defined in:
app/models/alchemy/storage_adapter/active_storage.rb

Defined Under Namespace

Modules: AttachmentClassMethods, PictureClassMethods Classes: AttachmentUrl, PictureUrl, Preprocessor, SanitizeSvgJob

Instance Method Summary collapse

Instance Method Details

#attachment_url_classObject



50
51
52
# File 'app/models/alchemy/storage_adapter/active_storage.rb', line 50

def attachment_url_class
  AttachmentUrl
end

#by_file_format_scope(file_format) ⇒ Alchemy::Picture::ActiveRecord_Relation



105
106
107
# File 'app/models/alchemy/storage_adapter/active_storage.rb', line 105

def by_file_format_scope(file_format)
  Picture.with_attached_image_file.joins(:image_file_blob).where(active_storage_blobs: {content_type: file_format})
end

#by_file_type_scope(file_type) ⇒ Alchemy::Atachment::ActiveRecord_Relation



111
112
113
# File 'app/models/alchemy/storage_adapter/active_storage.rb', line 111

def by_file_type_scope(file_type)
  Attachment.with_attached_file.joins(:file_blob).where(active_storage_blobs: {content_type: file_type})
end

#file_basename(attachment) ⇒ String



123
124
125
126
# File 'app/models/alchemy/storage_adapter/active_storage.rb', line 123

def file_basename(attachment)
  filename = attachment.file&.filename.to_s
  File.basename(filename, File.extname(filename))
end

#file_extension(attachment) ⇒ String



148
149
150
# File 'app/models/alchemy/storage_adapter/active_storage.rb', line 148

def file_extension(attachment)
  attachment.file&.filename&.extension
end

#file_formats(class_name, scope:) ⇒ Object



62
63
64
65
66
67
68
69
# File 'app/models/alchemy/storage_adapter/active_storage.rb', line 62

def file_formats(class_name, scope:)
  attachment_scope = case class_name
  when "Alchemy::Attachment" then scope.with_attached_file
  when "Alchemy::Picture" then scope.with_attached_image_file
  end

  attachment_scope.pluck("active_storage_blobs.content_type").uniq.tap(&:compact!).presence || []
end

#file_mime_type(attachment) ⇒ String



142
143
144
# File 'app/models/alchemy/storage_adapter/active_storage.rb', line 142

def file_mime_type(attachment)
  attachment.file&.content_type
end

#file_name(attachment) ⇒ String



130
131
132
# File 'app/models/alchemy/storage_adapter/active_storage.rb', line 130

def file_name(attachment)
  attachment.file&.filename&.to_s
end

#file_size(attachment) ⇒ Integer



136
137
138
# File 'app/models/alchemy/storage_adapter/active_storage.rb', line 136

def file_size(attachment)
  attachment.file&.byte_size
end

#has_convertible_format?(picture) ⇒ TrueClass, FalseClass



154
155
156
# File 'app/models/alchemy/storage_adapter/active_storage.rb', line 154

def has_convertible_format?(picture)
  picture.image_file&.variable?
end

#image_file_basename(picture) ⇒ String



160
161
162
163
# File 'app/models/alchemy/storage_adapter/active_storage.rb', line 160

def image_file_basename(picture)
  filename = picture.image_file&.filename.to_s
  File.basename(filename, File.extname(filename))
end

#image_file_extension(picture) ⇒ Integer



197
198
199
# File 'app/models/alchemy/storage_adapter/active_storage.rb', line 197

def image_file_extension(picture)
  picture.image_file&.filename&.extension&.downcase
end

#image_file_format(picture) ⇒ String



173
174
175
# File 'app/models/alchemy/storage_adapter/active_storage.rb', line 173

def image_file_format(picture)
  picture.image_file&.content_type
end

#image_file_height(picture) ⇒ Integer



191
192
193
# File 'app/models/alchemy/storage_adapter/active_storage.rb', line 191

def image_file_height(picture)
  picture.image_file&.&.fetch(:height, nil)
end

#image_file_name(picture) ⇒ String



167
168
169
# File 'app/models/alchemy/storage_adapter/active_storage.rb', line 167

def image_file_name(picture)
  picture.image_file&.filename&.to_s
end

#image_file_present?(picture) ⇒ TrueClass, FalseClass



203
204
205
# File 'app/models/alchemy/storage_adapter/active_storage.rb', line 203

def image_file_present?(picture)
  picture.image_file.attached?
end

#image_file_size(picture) ⇒ Integer



179
180
181
# File 'app/models/alchemy/storage_adapter/active_storage.rb', line 179

def image_file_size(picture)
  picture.image_file&.byte_size
end

#image_file_width(picture) ⇒ Integer



185
186
187
# File 'app/models/alchemy/storage_adapter/active_storage.rb', line 185

def image_file_width(picture)
  picture.image_file&.&.fetch(:width, nil)
end

#not_file_type_scope(file_type) ⇒ Alchemy::Atachment::ActiveRecord_Relation



117
118
119
# File 'app/models/alchemy/storage_adapter/active_storage.rb', line 117

def not_file_type_scope(file_type)
  Attachment.with_attached_file.joins(:file_blob).where.not(active_storage_blobs: {content_type: file_type})
end

#picture_url_classObject



58
59
60
# File 'app/models/alchemy/storage_adapter/active_storage.rb', line 58

def picture_url_class
  PictureUrl
end

#preload_picture_associations(pictures) ⇒ Object

Preload picture associations on already-loaded records



215
216
217
218
219
220
# File 'app/models/alchemy/storage_adapter/active_storage.rb', line 215

def preload_picture_associations(pictures)
  ActiveRecord::Associations::Preloader.new(
    records: pictures,
    associations: {image_file_attachment: :blob}
  ).call
end

#preloaded_pictures(pictures) ⇒ Object



209
210
211
# File 'app/models/alchemy/storage_adapter/active_storage.rb', line 209

def preloaded_pictures(pictures)
  pictures.with_attached_image_file
end

#preprocessor_classObject



54
55
56
# File 'app/models/alchemy/storage_adapter/active_storage.rb', line 54

def preprocessor_class
  Preprocessor
end

#ransackable_associations(class_name) ⇒ Array<String>



90
91
92
93
94
95
96
97
# File 'app/models/alchemy/storage_adapter/active_storage.rb', line 90

def ransackable_associations(class_name)
  case class_name
  when "Alchemy::Attachment"
    %w[file_blob]
  when "Alchemy::Picture"
    %w[image_file_blob]
  end
end

#ransackable_attributes(_class_name) ⇒ Array<String>



84
85
86
# File 'app/models/alchemy/storage_adapter/active_storage.rb', line 84

def ransackable_attributes(_class_name)
  %w[name]
end

#rescuable_errorsObject



99
100
101
# File 'app/models/alchemy/storage_adapter/active_storage.rb', line 99

def rescuable_errors
  ::ActiveStorage::Error
end

#searchable_alchemy_resource_attributes(class_name) ⇒ Array<String>



73
74
75
76
77
78
79
80
# File 'app/models/alchemy/storage_adapter/active_storage.rb', line 73

def searchable_alchemy_resource_attributes(class_name)
  case class_name
  when "Alchemy::Attachment"
    %w[name file_blob_filename]
  when "Alchemy::Picture"
    %w[name image_file_blob_filename]
  end
end

#set_attachment_name?(attachment) ⇒ TrueClass, FalseClass



224
225
226
# File 'app/models/alchemy/storage_adapter/active_storage.rb', line 224

def set_attachment_name?(attachment)
  attachment.file.changed?
end