Class: Alchemy::Picture

Inherits:
BaseRecord
  • Object
show all
Includes:
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_urlname, #sanitized_filename

Methods included from ConfigMissing

#const_missing

Methods included from SearchableResource

#ransackable_associations, #ransackable_attributes, #ransackable_scopes, #ransortable_attributes

Class Method Details

.allowed_filetypesObject



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

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

.file_formats(scope = all) ⇒ Object



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

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

.last_uploadObject



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

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



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

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


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

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

.ransackable_associations(_auth_object = nil) ⇒ Object



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

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

.ransackable_attributes(_auth_object = nil) ⇒ Object



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

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

.ransackable_scopes(_auth_object = nil) ⇒ Object



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

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

.searchable_alchemy_resource_attributesObject



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

def searchable_alchemy_resource_attributes
  Alchemy.storage_adapter.searchable_alchemy_resource_attributes(name)
end

.url_classObject

The class used to generate URLs for pictures



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

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


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

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)


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

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.



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

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.



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

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)


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

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

#image_file_dimensionsObject

A size String from original image file values.

Example

200 x 100



278
279
280
# File 'app/models/alchemy/picture.rb', line 278

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

#image_file_extensionObject Also known as: suffix



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

def image_file_extension
  Alchemy.storage_adapter.image_file_extension(self)
end

#image_file_formatObject



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

def image_file_format
  Alchemy.storage_adapter.image_file_format(self)
end

#image_file_heightObject



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

def image_file_height
  Alchemy.storage_adapter.image_file_height(self)
end

#image_file_nameObject



242
243
244
# File 'app/models/alchemy/picture.rb', line 242

def image_file_name
  Alchemy.storage_adapter.image_file_name(self)
end

#image_file_sizeObject



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

def image_file_size
  Alchemy.storage_adapter.image_file_size(self)
end

#image_file_widthObject



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

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)


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

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

#svg?Boolean

Returns:

  • (Boolean)


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

def svg?
  image_file_format == "image/svg+xml"
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)


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

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.



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

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)


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

def url(options = {})
  return unless image_file

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

#urlnameObject

Returns an uri escaped name.



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

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