Class: Alchemy::Picture
- Inherits:
-
BaseRecord
- Object
- ActiveRecord::Base
- BaseRecord
- Alchemy::Picture
- 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).freeze
- TRANSFORMATION_OPTIONS =
[ :crop, :crop_from, :crop_size, :flatten, :format, :quality, :size, :upsample, ]
Class Method Summary collapse
- .allowed_filetypes ⇒ Object
- .filtered_by(filter = "") ⇒ Object
- .last_upload ⇒ Object
-
.preprocessor_class ⇒ Object
Image preprocessing class.
-
.preprocessor_class=(klass) ⇒ Object
Set a image preprocessing class.
- .search_by(params, query, per_page = 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.
-
#deletable? ⇒ Boolean
Returns true if picture is not assigned to any EssencePicture.
-
#has_convertible_format? ⇒ Boolean
Returns true if the image can be converted into other formats.
-
#humanized_name ⇒ Object
Returns a humanized, readable name from image filename.
-
#image_file_dimensions ⇒ Object
A size String from original image file values.
- #next(params = {}) ⇒ Object
- #previous(params = {}) ⇒ Object
-
#restricted? ⇒ Boolean
Checks if the picture is restricted.
-
#suffix ⇒ Object
Returns the suffix of the filename.
-
#to_jq_upload ⇒ Object
Returns a Hash suitable for jquery fileupload json.
-
#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 Calculations
#can_be_cropped_to?, #image_size, #is_bigger_than?, #is_smaller_than?, #sizes_from_string
Methods included from TouchElements
Methods included from Taggable
Methods included from NameConversions
#convert_to_humanized_name, #convert_to_urlname
Methods included from Logger
Class Method Details
.allowed_filetypes ⇒ Object
98 99 100 |
# File 'app/models/alchemy/picture.rb', line 98 def allowed_filetypes Config.get(:uploader).fetch("allowed_filetypes", {}).fetch("alchemy/pictures", []) end |
.filtered_by(filter = "") ⇒ Object
164 165 166 167 168 169 170 171 172 |
# File 'app/models/alchemy/picture.rb', line 164 def filtered_by(filter = "") case filter when "recent" then recent when "last_upload" then last_upload when "without_tag" then without_tag else all end end |
.last_upload ⇒ Object
139 140 141 142 143 144 |
# File 'app/models/alchemy/picture.rb', line 139 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
72 73 74 |
# File 'app/models/alchemy/picture.rb', line 72 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
81 82 83 |
# File 'app/models/alchemy/picture.rb', line 81 def self.preprocessor_class=(klass) @_preprocessor_class = klass end |
.search_by(params, query, per_page = nil) ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'app/models/alchemy/picture.rb', line 146 def search_by(params, query, per_page = nil) pictures = query.result if params[:tagged_with].present? pictures = pictures.tagged_with(params[:tagged_with]) end if params[:filter].present? pictures = pictures.filtered_by(params[:filter]) end if per_page pictures = pictures.page(params[:page] || 1).per(per_page) end pictures.order(:name) end |
.searchable_alchemy_resource_attributes ⇒ Object
135 136 137 |
# File 'app/models/alchemy/picture.rb', line 135 def searchable_alchemy_resource_attributes %w(name image_file_name) end |
.url_class ⇒ Object
The class used to generate URLs for pictures
124 125 126 |
# File 'app/models/alchemy/picture.rb', line 124 def url_class @_url_class ||= Alchemy::Picture::Url end |
.url_class=(klass) ⇒ Object
Set a different picture url class
131 132 133 |
# File 'app/models/alchemy/picture.rb', line 131 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
280 281 282 283 284 |
# File 'app/models/alchemy/picture.rb', line 280 def convertible? Config.get(:image_output_format) && Config.get(: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.
267 268 269 270 271 272 273 |
# File 'app/models/alchemy/picture.rb', line 267 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 EssencePicture.
306 307 308 |
# File 'app/models/alchemy/picture.rb', line 306 def deletable? essence_pictures.empty? end |
#has_convertible_format? ⇒ Boolean
Returns true if the image can be converted into other formats
288 289 290 |
# File 'app/models/alchemy/picture.rb', line 288 def has_convertible_format? image_file_format.in?(CONVERTIBLE_FILE_FORMATS) end |
#humanized_name ⇒ Object
Returns a humanized, readable name from image filename.
256 257 258 259 260 |
# File 'app/models/alchemy/picture.rb', line 256 def humanized_name return "" if image_file_name.blank? convert_to_humanized_name(image_file_name, suffix) end |
#image_file_dimensions ⇒ Object
A size String from original image file values.
Example
200 x 100
316 317 318 |
# File 'app/models/alchemy/picture.rb', line 316 def image_file_dimensions "#{image_file_width}x#{image_file_height}" end |
#next(params = {}) ⇒ Object
209 210 211 212 |
# File 'app/models/alchemy/picture.rb', line 209 def next(params = {}) query = Picture.ransack(params[:q]) Picture.search_by(params, query).where("name > ?", name).first end |
#previous(params = {}) ⇒ Object
204 205 206 207 |
# File 'app/models/alchemy/picture.rb', line 204 def previous(params = {}) query = Picture.ransack(params[:q]) Picture.search_by(params, query).where("name < ?", name).last 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.
300 301 302 |
# File 'app/models/alchemy/picture.rb', line 300 def restricted? pages.any? && pages.not_restricted.blank? end |
#suffix ⇒ Object
Returns the suffix of the filename.
250 251 252 |
# File 'app/models/alchemy/picture.rb', line 250 def suffix image_file.ext end |
#to_jq_upload ⇒ Object
Returns a Hash suitable for jquery fileupload json.
230 231 232 233 234 235 236 |
# File 'app/models/alchemy/picture.rb', line 230 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.
220 221 222 223 224 225 226 |
# File 'app/models/alchemy/picture.rb', line 220 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') %>
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'app/models/alchemy/picture.rb', line 188 def url( = {}) return unless image_file variant = PictureVariant.new(self, .slice(*TRANSFORMATION_OPTIONS)) self.class.url_class.new(variant).call( .except(*TRANSFORMATION_OPTIONS).merge( basename: name, ext: variant.render_format, name: name, ) ) rescue ::Dragonfly::Job::Fetch::NotFound => e log_warning(e.) nil end |
#urlname ⇒ Object
Returns an uri escaped name.
240 241 242 243 244 245 246 |
# File 'app/models/alchemy/picture.rb', line 240 def urlname if name.blank? "image_#{id}" else ::CGI.escape(name.gsub(/\.(gif|png|jpe?g|tiff?)/i, "").tr(".", " ")) end end |