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
- .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) ⇒ Object
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
.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
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_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.
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
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_dimensions ⇒ Object
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_extension ⇒ Object 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_format ⇒ Object
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_height ⇒ Object
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_name ⇒ Object
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_size ⇒ Object
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_width ⇒ Object
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.
238 239 240 |
# File 'app/models/alchemy/picture.rb', line 238 def restricted? pages.any? && pages.not_restricted.blank? end |
#svg? ⇒ 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
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') %>
147 148 149 150 151 152 153 154 |
# File 'app/models/alchemy/picture.rb', line 147 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.
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 |