Class: Alchemy::Picture
- Inherits:
-
BaseRecord
- Object
- ActiveRecord::Base
- BaseRecord
- Alchemy::Picture
- 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
-
.alchemy_element_preloads(pictures) ⇒ Object
Preload associations for element editor display.
- .allowed_filetypes ⇒ Object
- .file_formats(scope = all) ⇒ Object
- .last_upload ⇒ Object
-
.preprocessor_class ⇒ Object
Image preprocessing class.
-
.preprocessor_class=(klass) ⇒ Object
Set a image preprocessing class.
- .ransackable_associations(_auth_object = nil) ⇒ Object
- .ransackable_attributes(_auth_object = nil) ⇒ Object
- .ransackable_scopes(_auth_object = nil) ⇒ Object
- .searchable_alchemy_resource_attributes ⇒ Object
-
.url_class ⇒ Object
The class used to generate URLs for pictures.
-
.url_class=(klass) ⇒ Object
Set a different picture url class.
Instance Method Summary collapse
-
#convertible? ⇒ Boolean
Returns true if the image can be converted.
-
#default_render_format ⇒ Object
Returns the format the image should be rendered with.
-
#description_for(language) ⇒ String?
Returns the picture description for a given language.
-
#has_convertible_format? ⇒ Boolean
Returns true if the image can be converted into other formats.
-
#image_file_dimensions ⇒ Object
A size String from original image file values.
- #image_file_extension ⇒ Object (also: #suffix)
- #image_file_format ⇒ Object
- #image_file_height ⇒ Object
- #image_file_name ⇒ Object
- #image_file_size ⇒ Object
- #image_file_width ⇒ Object
-
#restricted? ⇒ Boolean
Checks if the picture is restricted.
- #svg? ⇒ Boolean
-
#thumbnail_url(size: "160x120") ⇒ String
Returns an url for the thumbnail representation of the picture.
-
#update_name_and_tag_list!(params) ⇒ Object
Updates name and tag_list attributes.
-
#url(options = {}) ⇒ String|Nil
Returns an url (or relative path) to a processed image for use inside an image_tag helper.
-
#urlname ⇒ Object
Returns an uri escaped name.
Methods included from RelatableResource
Methods included from TouchElements
Methods included from Taggable
Methods included from NameConversions
#convert_to_urlname, #sanitized_filename
Methods included from ConfigMissing
Methods included from SearchableResource
#ransackable_associations, #ransackable_attributes, #ransackable_scopes, #ransortable_attributes
Class Method Details
.alchemy_element_preloads(pictures) ⇒ Object
Preload associations for element editor display
140 141 142 143 144 145 |
# File 'app/models/alchemy/picture.rb', line 140 def alchemy_element_preloads(pictures) return if pictures.blank? # Preload storage-specific associations to avoid N+1 when rendering thumbnails Alchemy.storage_adapter.preload_picture_associations(pictures) end |
.allowed_filetypes ⇒ Object
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_upload ⇒ Object
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_class ⇒ Object
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_attributes ⇒ Object
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_class ⇒ Object
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
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
235 236 237 238 239 |
# File 'app/models/alchemy/picture.rb', line 235 def convertible? Alchemy.config.image_output_format && Alchemy.config.image_output_format != "original" && has_convertible_format? end |
#default_render_format ⇒ Object
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.
222 223 224 225 226 227 228 |
# File 'app/models/alchemy/picture.rb', line 222 def default_render_format if convertible? Alchemy.config.image_output_format else image_file_extension end end |
#description_for(language) ⇒ String?
Returns the picture description for a given language.
199 200 201 202 203 204 205 |
# File 'app/models/alchemy/picture.rb', line 199 def description_for(language) if descriptions.loaded? descriptions.detect { _1.language == language }&.text else descriptions.find_by(language: language)&.text end end |
#has_convertible_format? ⇒ Boolean
Returns true if the image can be converted into other formats
243 244 245 |
# File 'app/models/alchemy/picture.rb', line 243 def has_convertible_format? Alchemy.storage_adapter.has_convertible_format?(self) end |
#image_file_dimensions ⇒ Object
A size String from original image file values.
Example
200 x 100
295 296 297 |
# File 'app/models/alchemy/picture.rb', line 295 def image_file_dimensions "#{image_file_width}x#{image_file_height}" end |
#image_file_extension ⇒ Object Also known as: suffix
279 280 281 |
# File 'app/models/alchemy/picture.rb', line 279 def image_file_extension Alchemy.storage_adapter.image_file_extension(self) end |
#image_file_format ⇒ Object
263 264 265 |
# File 'app/models/alchemy/picture.rb', line 263 def image_file_format Alchemy.storage_adapter.image_file_format(self) end |
#image_file_height ⇒ Object
275 276 277 |
# File 'app/models/alchemy/picture.rb', line 275 def image_file_height Alchemy.storage_adapter.image_file_height(self) end |
#image_file_name ⇒ Object
259 260 261 |
# File 'app/models/alchemy/picture.rb', line 259 def image_file_name Alchemy.storage_adapter.image_file_name(self) end |
#image_file_size ⇒ Object
267 268 269 |
# File 'app/models/alchemy/picture.rb', line 267 def image_file_size Alchemy.storage_adapter.image_file_size(self) end |
#image_file_width ⇒ Object
271 272 273 |
# File 'app/models/alchemy/picture.rb', line 271 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.
255 256 257 |
# File 'app/models/alchemy/picture.rb', line 255 def restricted? .any? && .not_restricted.blank? end |
#svg? ⇒ Boolean
285 286 287 |
# File 'app/models/alchemy/picture.rb', line 285 def svg? image_file_format == "image/svg+xml" end |
#thumbnail_url(size: "160x120") ⇒ String
Returns an url for the thumbnail representation of the picture
171 172 173 174 175 176 177 178 179 |
# File 'app/models/alchemy/picture.rb', line 171 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.
187 188 189 190 191 192 193 |
# File 'app/models/alchemy/picture.rb', line 187 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') %>
157 158 159 160 161 162 163 164 |
# File 'app/models/alchemy/picture.rb', line 157 def url( = {}) return unless image_file self.class.url_class.new(self).call() rescue Alchemy.storage_adapter.rescuable_errors => e Logger.warn(e.) nil end |
#urlname ⇒ Object
Returns an uri escaped name.
209 210 211 212 213 214 215 |
# File 'app/models/alchemy/picture.rb', line 209 def urlname if name.blank? "image_#{id}" else ::CGI.escape(name.gsub(/\.(gif|png|jpe?g|tiff?)/i, "").tr(".", " ")) end end |