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_classObject



41
42
43
# File 'app/models/alchemy/storage_adapter/dragonfly.rb', line 41

def attachment_url_class
  AttachmentUrl
end

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

Parameters:

  • (String)

Returns:

  • (Alchemy::Picture::ActiveRecord_Relation)


86
87
88
# File 'app/models/alchemy/storage_adapter/dragonfly.rb', line 86

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)


92
93
94
# File 'app/models/alchemy/storage_adapter/dragonfly.rb', line 92

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

#file_extension(attachment) ⇒ String

Parameters:

Returns:

  • (String)


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

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



53
54
55
56
57
58
59
60
# File 'app/models/alchemy/storage_adapter/dragonfly.rb', line 53

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)


110
111
112
# File 'app/models/alchemy/storage_adapter/dragonfly.rb', line 110

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

#file_name(attachment) ⇒ String

Parameters:

Returns:

  • (String)


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

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

#file_size(attachment) ⇒ Integer

Parameters:

Returns:

  • (Integer)


104
105
106
# File 'app/models/alchemy/storage_adapter/dragonfly.rb', line 104

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

#has_convertible_format?(picture) ⇒ TrueClass, FalseClass

Parameters:

Returns:

  • (TrueClass, FalseClass)


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

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

#image_file_extension(picture) ⇒ String

Parameters:

Returns:

  • (String)


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

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

#image_file_format(picture) ⇒ String

Parameters:

Returns:

  • (String)


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

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)


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

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

#image_file_name(picture) ⇒ String

Parameters:

Returns:

  • (String)


129
130
131
# File 'app/models/alchemy/storage_adapter/dragonfly.rb', line 129

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

#image_file_present?(picture) ⇒ TrueClass, FalseClass

Parameters:

Returns:

  • (TrueClass, FalseClass)


166
167
168
# File 'app/models/alchemy/storage_adapter/dragonfly.rb', line 166

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

#image_file_size(picture) ⇒ Integer

Parameters:

Returns:

  • (Integer)


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

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

#image_file_width(picture) ⇒ Integer

Parameters:

Returns:

  • (Integer)


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

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

#picture_url_classObject



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

def picture_url_class
  PictureUrl
end

#preloaded_pictures(pictures) ⇒ Object

Returns Alchemy::Picture::ActiveRecord_Relation.

Parameters:

  • Alchemy::Picture::ActiveRecord_Relation

Returns:

  • Alchemy::Picture::ActiveRecord_Relation



172
173
174
# File 'app/models/alchemy/storage_adapter/dragonfly.rb', line 172

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

#preprocessor_classObject



45
46
47
# File 'app/models/alchemy/storage_adapter/dragonfly.rb', line 45

def preprocessor_class
  Preprocessor
end

#ransackable_associations(_class_name) ⇒ Array<String>

Parameters:

  • (String)

Returns:

  • (Array<String>)


76
77
78
# File 'app/models/alchemy/storage_adapter/dragonfly.rb', line 76

def ransackable_associations(_class_name)
  %w[]
end

#rescuable_errorsObject



80
81
82
# File 'app/models/alchemy/storage_adapter/dragonfly.rb', line 80

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>)


64
65
66
67
68
69
70
71
# File 'app/models/alchemy/storage_adapter/dragonfly.rb', line 64

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)


178
179
180
# File 'app/models/alchemy/storage_adapter/dragonfly.rb', line 178

def set_attachment_name?(attachment)
  attachment.file_name_changed?
end