Module: Alchemy::StorageAdapter::Dragonfly

Extended by:
Dragonfly
Included in:
Dragonfly
Defined in:
app/models/alchemy/storage_adapter/dragonfly.rb

Defined Under Namespace

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

Constant Summary collapse

CONVERTIBLE_FILE_FORMATS =
%w[gif jpg jpeg png webp].freeze

Instance Method Summary collapse

Instance Method Details

#attachment_url_classClass

Returns the class used to generate attachment urls

Returns:

  • (Class)
    • defaults to Alchemy::StorageAdapter::Dragonfly::AttachmentUrl



56
57
58
# File 'app/models/alchemy/storage_adapter/dragonfly.rb', line 56

def attachment_url_class
  @_attachment_url_class ||= AttachmentUrl
end

#attachment_url_class=(klass) ⇒ Object

Allows to set a custom Attachment Url class



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

def attachment_url_class=(klass)
  @_attachment_url_class = klass
end

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

Parameters:

  • (String)

Returns:

  • (Alchemy::Picture::ActiveRecord_Relation)


108
109
110
# File 'app/models/alchemy/storage_adapter/dragonfly.rb', line 108

def by_file_format_scope(file_format)
  Picture.where(image_file_format: file_format)
end

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

Parameters:

  • (String)

Returns:

  • (Alchemy::Attachment::ActiveRecord_Relation)


114
115
116
# File 'app/models/alchemy/storage_adapter/dragonfly.rb', line 114

def by_file_type_scope(file_type)
  Attachment.where(file_mime_type: file_type)
end

#file_extension(attachment) ⇒ String

Parameters:

Returns:

  • (String)


138
139
140
141
# File 'app/models/alchemy/storage_adapter/dragonfly.rb', line 138

def file_extension(attachment)
  content_type = file_mime_type(attachment)
  Marcel::Magic.new(content_type).extensions.first if content_type
end

#file_formats(class_name, scope:) ⇒ Object



75
76
77
78
79
80
81
82
# File 'app/models/alchemy/storage_adapter/dragonfly.rb', line 75

def file_formats(class_name, scope:)
  mime_type_column = case class_name
  when "Alchemy::Attachment" then :file_mime_type
  when "Alchemy::Picture" then :image_file_format
  end

  scope.reorder(mime_type_column).distinct.pluck(mime_type_column).compact.presence || []
end

#file_mime_type(attachment) ⇒ String

Parameters:

Returns:

  • (String)


132
133
134
# File 'app/models/alchemy/storage_adapter/dragonfly.rb', line 132

def file_mime_type(attachment)
  attachment.read_attribute(:file_mime_type)
end

#file_name(attachment) ⇒ String

Parameters:

Returns:

  • (String)


120
121
122
# File 'app/models/alchemy/storage_adapter/dragonfly.rb', line 120

def file_name(attachment)
  attachment.read_attribute(:file_name)
end

#file_size(attachment) ⇒ Integer

Parameters:

Returns:

  • (Integer)


126
127
128
# File 'app/models/alchemy/storage_adapter/dragonfly.rb', line 126

def file_size(attachment)
  attachment.read_attribute(:file_size)
end

#has_convertible_format?(picture) ⇒ TrueClass, FalseClass

Parameters:

Returns:

  • (TrueClass, FalseClass)


145
146
147
# File 'app/models/alchemy/storage_adapter/dragonfly.rb', line 145

def has_convertible_format?(picture)
  image_file_extension(picture).in?(CONVERTIBLE_FILE_FORMATS)
end

#image_file_extension(picture) ⇒ String

Parameters:

Returns:

  • (String)


182
183
184
# File 'app/models/alchemy/storage_adapter/dragonfly.rb', line 182

def image_file_extension(picture)
  picture.read_attribute(:image_file_format)
end

#image_file_format(picture) ⇒ String

Parameters:

Returns:

  • (String)


157
158
159
160
# File 'app/models/alchemy/storage_adapter/dragonfly.rb', line 157

def image_file_format(picture)
  ext = picture.read_attribute(:image_file_format)
  Marcel::MimeType.for(extension: ext) if ext
end

#image_file_height(picture) ⇒ Integer

Parameters:

Returns:

  • (Integer)


176
177
178
# File 'app/models/alchemy/storage_adapter/dragonfly.rb', line 176

def image_file_height(picture)
  picture.read_attribute(:image_file_height)
end

#image_file_name(picture) ⇒ String

Parameters:

Returns:

  • (String)


151
152
153
# File 'app/models/alchemy/storage_adapter/dragonfly.rb', line 151

def image_file_name(picture)
  picture.read_attribute(:image_file_name)
end

#image_file_present?(picture) ⇒ TrueClass, FalseClass

Parameters:

Returns:

  • (TrueClass, FalseClass)


188
189
190
# File 'app/models/alchemy/storage_adapter/dragonfly.rb', line 188

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

#image_file_size(picture) ⇒ Integer

Parameters:

Returns:

  • (Integer)


164
165
166
# File 'app/models/alchemy/storage_adapter/dragonfly.rb', line 164

def image_file_size(picture)
  picture.read_attribute(:image_file_size)
end

#image_file_width(picture) ⇒ Integer

Parameters:

Returns:

  • (Integer)


170
171
172
# File 'app/models/alchemy/storage_adapter/dragonfly.rb', line 170

def image_file_width(picture)
  picture.read_attribute(:image_file_width)
end

#picture_url_classClass

Returns the class used to generate picture urls

Returns:

  • (Class)
    • defaults to Alchemy::StorageAdapter::Dragonfly::PictureUrl



71
72
73
# File 'app/models/alchemy/storage_adapter/dragonfly.rb', line 71

def picture_url_class
  @_picture_url_class ||= PictureUrl
end

#picture_url_class=(klass) ⇒ Object

Allows to set a custom Picture Url class



65
66
67
# File 'app/models/alchemy/storage_adapter/dragonfly.rb', line 65

def picture_url_class=(klass)
  @_picture_url_class = klass
end

#preloaded_pictures(pictures) ⇒ Object

Returns Alchemy::Picture::ActiveRecord_Relation.

Parameters:

  • Alchemy::Picture::ActiveRecord_Relation

Returns:

  • Alchemy::Picture::ActiveRecord_Relation



194
195
196
# File 'app/models/alchemy/storage_adapter/dragonfly.rb', line 194

def preloaded_pictures(pictures)
  pictures.includes(:thumbs)
end

#preprocessor_classObject



60
61
62
# File 'app/models/alchemy/storage_adapter/dragonfly.rb', line 60

def preprocessor_class
  Preprocessor
end

#ransackable_associations(_class_name) ⇒ Array<String>

Parameters:

  • (String)

Returns:

  • (Array<String>)


98
99
100
# File 'app/models/alchemy/storage_adapter/dragonfly.rb', line 98

def ransackable_associations(_class_name)
  %w[]
end

#rescuable_errorsObject



102
103
104
# File 'app/models/alchemy/storage_adapter/dragonfly.rb', line 102

def rescuable_errors
  ::Dragonfly::Job::Fetch::NotFound
end

#searchable_alchemy_resource_attributes(class_name) ⇒ Array<String> Also known as: ransackable_attributes

Parameters:

  • (String)

Returns:

  • (Array<String>)


86
87
88
89
90
91
92
93
# File 'app/models/alchemy/storage_adapter/dragonfly.rb', line 86

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

#set_attachment_name?(attachment) ⇒ TrueClass, FalseClass

Parameters:

Returns:

  • (TrueClass, FalseClass)


200
201
202
# File 'app/models/alchemy/storage_adapter/dragonfly.rb', line 200

def set_attachment_name?(attachment)
  attachment.file_name_changed?
end