Class: Alchemy::Picture

Inherits:
BaseRecord
  • Object
show all
Includes:
Logger, NameConversions, Calculations, Taggable, TouchElements
Defined in:
app/models/alchemy/picture.rb,
app/models/alchemy/picture/url.rb,
app/models/alchemy/picture/calculations.rb,
app/models/alchemy/picture/preprocessor.rb

Defined Under Namespace

Modules: Calculations, Transformations Classes: Preprocessor, Url

Constant Summary collapse

THUMBNAIL_SIZES =
{
  small: "80x60",
  medium: "160x120",
  large: "240x180"
}.with_indifferent_access.freeze
CONVERTIBLE_FILE_FORMATS =
%w[gif jpg jpeg png webp].freeze
TRANSFORMATION_OPTIONS =
[
  :crop,
  :crop_from,
  :crop_size,
  :flatten,
  :format,
  :quality,
  :size,
  :upsample
]

Constants included from SearchableResource

SearchableResource::SEARCHABLE_COLUMN_TYPES

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Calculations

#can_be_cropped_to?, #image_size, #is_bigger_than?, #is_smaller_than?, #sizes_from_string

Methods included from TouchElements

included

Methods included from Taggable

included, #tag_list=

Methods included from NameConversions

#convert_to_humanized_name, #convert_to_urlname

Methods included from Logger

#log_warning, warn

Methods included from SearchableResource

#ransackable_associations, #ransackable_attributes, #ransortable_attributes

Class Method Details

.alchemy_resource_filtersObject



143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'app/models/alchemy/picture.rb', line 143

def alchemy_resource_filters
  @_file_formats ||= distinct.pluck(:image_file_format).compact.presence || []
  [
    {
      name: :by_file_format,
      values: @_file_formats
    },
    {
      name: :misc,
      values: %w[recent last_upload without_tag deletable]
    }
  ]
end

.allowed_filetypesObject



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

def allowed_filetypes
  Config.get(:uploader).fetch("allowed_filetypes", {}).fetch("alchemy/pictures", [])
end

.last_uploadObject



161
162
163
164
165
166
# File 'app/models/alchemy/picture.rb', line 161

def last_upload
  last_picture = Picture.last
  return Picture.all unless last_picture

  Picture.where(upload_hash: last_picture.upload_hash)
end

.preprocessor_classObject

Image preprocessing class



74
75
76
# File 'app/models/alchemy/picture.rb', line 74

def self.preprocessor_class
  @_preprocessor_class ||= Preprocessor
end

.preprocessor_class=(klass) ⇒ Object

Set a image preprocessing class

# config/initializers/alchemy.rb
Alchemy::Picture.preprocessor_class = My::ImagePreprocessor


83
84
85
# File 'app/models/alchemy/picture.rb', line 83

def self.preprocessor_class=(klass)
  @_preprocessor_class = klass
end

.searchable_alchemy_resource_attributesObject



157
158
159
# File 'app/models/alchemy/picture.rb', line 157

def searchable_alchemy_resource_attributes
  %w[name image_file_name]
end

.url_classObject

The class used to generate URLs for pictures

See Also:



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

def url_class
  @_url_class ||= Alchemy::Picture::Url
end

.url_class=(klass) ⇒ Object

Set a different picture url class

See Also:



139
140
141
# File 'app/models/alchemy/picture.rb', line 139

def url_class=(klass)
  @_url_class = klass
end

Instance Method Details

#convertible?Boolean

Returns true if the image can be converted

If the image_output_format is set to nil or original or the image has not a convertible file format (i.e. SVG) this returns false

Returns:

  • (Boolean)


284
285
286
287
288
# File 'app/models/alchemy/picture.rb', line 284

def convertible?
  Config.get(:image_output_format) &&
    Config.get(:image_output_format) != "original" &&
    has_convertible_format?
end

#default_render_formatObject

Returns the format the image should be rendered with

Only returns a format differing from original if an image_output_format is set in config and the image has a convertible file format.



271
272
273
274
275
276
277
# File 'app/models/alchemy/picture.rb', line 271

def default_render_format
  if convertible?
    Config.get(:image_output_format)
  else
    image_file_format
  end
end

#deletable?Boolean

Returns true if picture is not assigned to any Picture ingredient.

Returns:

  • (Boolean)


310
311
312
# File 'app/models/alchemy/picture.rb', line 310

def deletable?
  picture_ingredients.empty?
end

#description_for(language) ⇒ Object

Returns the picture description for a given language.



238
239
240
# File 'app/models/alchemy/picture.rb', line 238

def description_for(language)
  descriptions.find_by(language: language)&.text
end

#has_convertible_format?Boolean

Returns true if the image can be converted into other formats

Returns:

  • (Boolean)


292
293
294
# File 'app/models/alchemy/picture.rb', line 292

def has_convertible_format?
  image_file_format.in?(CONVERTIBLE_FILE_FORMATS)
end

#humanized_nameObject

Returns a humanized, readable name from image filename.



260
261
262
263
264
# File 'app/models/alchemy/picture.rb', line 260

def humanized_name
  return "" if image_file_name.blank?

  convert_to_humanized_name(image_file_name, suffix)
end

#image_file_dimensionsObject

A size String from original image file values.

Example

200 x 100



320
321
322
# File 'app/models/alchemy/picture.rb', line 320

def image_file_dimensions
  "#{image_file_width}x#{image_file_height}"
end

#restricted?Boolean

Checks if the picture is restricted.

A picture is only restricted if it’s assigned on restricted pages only.

Once a picture is assigned on a not restricted page, it is considered public and therefore not restricted any more, even if it is also assigned on a restricted page.

Returns:

  • (Boolean)


304
305
306
# File 'app/models/alchemy/picture.rb', line 304

def restricted?
  pages.any? && pages.not_restricted.blank?
end

#suffixObject

Returns the suffix of the filename.



254
255
256
# File 'app/models/alchemy/picture.rb', line 254

def suffix
  image_file.ext
end

#thumbnail_url(size: "160x120") ⇒ String

Returns an url for the thumbnail representation of the picture

Parameters:

  • size (String) (defaults to: "160x120")
    • The size of the thumbnail

Returns:

  • (String)


203
204
205
206
207
208
209
210
211
# File 'app/models/alchemy/picture.rb', line 203

def thumbnail_url(size: "160x120")
  return if image_file.nil?

  url(
    flatten: true,
    format: image_file_format || "jpg",
    size: size
  )
end

#to_jq_uploadObject

Returns a Hash suitable for jquery fileupload json.



229
230
231
232
233
234
235
# File 'app/models/alchemy/picture.rb', line 229

def to_jq_upload
  {
    name: image_file_name,
    size: image_file_size,
    error: errors[:image_file].join
  }
end

#update_name_and_tag_list!(params) ⇒ Object

Updates name and tag_list attributes.

Used by Admin::PicturesController#update_multiple

Note: Does not delete name value, if the form field is blank.



219
220
221
222
223
224
225
# File 'app/models/alchemy/picture.rb', line 219

def update_name_and_tag_list!(params)
  if params[:pictures_name].present?
    self.name = params[:pictures_name]
  end
  self.tag_list = params[:pictures_tag_list]
  save!
end

#url(options = {}) ⇒ String|Nil

Returns an url (or relative path) to a processed image for use inside an image_tag helper.

Any additional options are passed to the url method, so you can add params to your url.

Example:

<%= image_tag picture.url(size: '320x200', format: 'png') %>

Returns:

  • (String|Nil)

See Also:



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'app/models/alchemy/picture.rb', line 182

def url(options = {})
  return unless image_file

  variant = PictureVariant.new(self, options.slice(*TRANSFORMATION_OPTIONS))
  self.class.url_class.new(variant).call(
    options.except(*TRANSFORMATION_OPTIONS).merge(
      basename: name,
      ext: variant.render_format,
      name: name
    )
  )
rescue ::Dragonfly::Job::Fetch::NotFound => e
  log_warning(e.message)
  nil
end

#urlnameObject

Returns an uri escaped name.



244
245
246
247
248
249
250
# File 'app/models/alchemy/picture.rb', line 244

def urlname
  if name.blank?
    "image_#{id}"
  else
    ::CGI.escape(name.gsub(/\.(gif|png|jpe?g|tiff?)/i, "").tr(".", " "))
  end
end