Class: Alchemy::Picture

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

Constant Summary collapse

THUMBNAIL_SIZES =
{
  small: "80x60",
  medium: "160x120",
  large: "240x180"
}.with_indifferent_access.freeze

Constants included from SearchableResource

SearchableResource::SEARCHABLE_COLUMN_TYPES

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RelatableResource

#deletable?

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 ConfigMissing

#const_missing

Methods included from SearchableResource

#ransackable_associations, #ransackable_attributes, #ransackable_scopes, #ransortable_attributes

Class Method Details

.allowed_filetypesObject



70
71
72
# File 'app/models/alchemy/picture.rb', line 70

def allowed_filetypes
  Alchemy.config.uploader.allowed_filetypes.alchemy_pictures
end

.file_formats(scope = all) ⇒ Object



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

def file_formats(scope = all)
  Alchemy.storage_adapter.file_formats(name, scope:)
end

.last_uploadObject



121
122
123
124
125
126
# File 'app/models/alchemy/picture.rb', line 121

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



53
54
55
# File 'app/models/alchemy/picture.rb', line 53

def self.preprocessor_class
  @_preprocessor_class ||= Alchemy.storage_adapter.preprocessor_class
end

.preprocessor_class=(klass) ⇒ Object

Set a image preprocessing class

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


62
63
64
# File 'app/models/alchemy/picture.rb', line 62

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

.ransackable_associations(_auth_object = nil) ⇒ Object



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

def ransackable_associations(_auth_object = nil)
  Alchemy.storage_adapter.ransackable_associations(name)
end

.ransackable_attributes(_auth_object = nil) ⇒ Object



113
114
115
# File 'app/models/alchemy/picture.rb', line 113

def ransackable_attributes(_auth_object = nil)
  Alchemy.storage_adapter.ransackable_attributes(name)
end

.ransackable_scopes(_auth_object = nil) ⇒ Object



128
129
130
# File 'app/models/alchemy/picture.rb', line 128

def ransackable_scopes(_auth_object = nil)
  [:by_file_format, :recent, :last_upload, :without_tag, :deletable]
end

.searchable_alchemy_resource_attributesObject



109
110
111
# File 'app/models/alchemy/picture.rb', line 109

def searchable_alchemy_resource_attributes
  Alchemy.storage_adapter.searchable_alchemy_resource_attributes(name)
end

.url_classObject

The class used to generate URLs for pictures



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

def url_class
  @_url_class ||= Alchemy.storage_adapter.picture_url_class
end

.url_class=(klass) ⇒ Object

Set a different picture url class

See Also:

  • Url


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

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)


225
226
227
228
229
# File 'app/models/alchemy/picture.rb', line 225

def convertible?
  Alchemy.config.image_output_format &&
    Alchemy.config.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.



212
213
214
215
216
217
218
# File 'app/models/alchemy/picture.rb', line 212

def default_render_format
  if convertible?
    Alchemy.config.image_output_format
  else
    image_file_extension
  end
end

#description_for(language) ⇒ Object

Returns the picture description for a given language.



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

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)


233
234
235
# File 'app/models/alchemy/picture.rb', line 233

def has_convertible_format?
  Alchemy.storage_adapter.has_convertible_format?(self)
end

#humanized_nameObject

Returns a humanized, readable name from image filename.



201
202
203
204
205
# File 'app/models/alchemy/picture.rb', line 201

def humanized_name
  return "" if image_file_name.blank?

  convert_to_humanized_name(image_file_name, image_file_extension)
end

#image_file_dimensionsObject

A size String from original image file values.

Example

200 x 100



281
282
283
# File 'app/models/alchemy/picture.rb', line 281

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

#image_file_extensionObject Also known as: suffix



269
270
271
# File 'app/models/alchemy/picture.rb', line 269

def image_file_extension
  Alchemy.storage_adapter.image_file_extension(self)
end

#image_file_formatObject



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

def image_file_format
  Alchemy.storage_adapter.image_file_format(self)
end

#image_file_heightObject



265
266
267
# File 'app/models/alchemy/picture.rb', line 265

def image_file_height
  Alchemy.storage_adapter.image_file_height(self)
end

#image_file_nameObject



249
250
251
# File 'app/models/alchemy/picture.rb', line 249

def image_file_name
  Alchemy.storage_adapter.image_file_name(self)
end

#image_file_sizeObject



257
258
259
# File 'app/models/alchemy/picture.rb', line 257

def image_file_size
  Alchemy.storage_adapter.image_file_size(self)
end

#image_file_widthObject



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

def image_file_width
  Alchemy.storage_adapter.image_file_width(self)
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)


245
246
247
# File 'app/models/alchemy/picture.rb', line 245

def restricted?
  pages.any? && pages.not_restricted.blank?
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)


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

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

  url(
    flatten: true,
    format: image_file_extension || "jpg",
    size: size
  )
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.



176
177
178
179
180
181
182
# File 'app/models/alchemy/picture.rb', line 176

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.

Example:

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

Returns:

  • (String|Nil)


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

def url(options = {})
  return unless image_file

  self.class.url_class.new(self).call(options)
rescue Alchemy.storage_adapter.rescuable_errors => e
  log_warning(e.message)
  nil
end

#urlnameObject

Returns an uri escaped name.



191
192
193
194
195
196
197
# File 'app/models/alchemy/picture.rb', line 191

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