Class: Alchemy::Picture
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Alchemy::Picture
- Includes:
- NameConversions, Sweeping, Transformations, Touching
- Defined in:
- app/models/alchemy/picture.rb
Defined Under Namespace
Modules: Sweeping, Transformations
Constant Summary collapse
- CONVERTIBLE_FILE_FORMATS =
%w(gif jpg jpeg png).freeze
Class Method Summary collapse
- .allowed_filetypes ⇒ Object
- .filtered_by(filter = '') ⇒ Object
- .last_upload ⇒ Object
- .search_by(params, query, per_page = nil) ⇒ Object
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
Instance methods.
-
#restricted? ⇒ Boolean
Checks if the picture is restricted.
-
#security_token(params = {}) ⇒ Object
Returns a security token for signed picture rendering requests.
-
#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.
-
#urlname ⇒ Object
Returns an uri escaped name.
Methods included from Transformations
#can_be_cropped_to, #crop, #crop_size?, #default_mask, #image_size, #landscape_format?, #portrait_format?, #render_size?, #resize, #sizes_from_string, #square_format?, #thumbnail_size
Methods included from Touching
Methods included from NameConversions
#convert_to_humanized_name, #convert_to_urlname
Class Method Details
.allowed_filetypes ⇒ Object
57 58 59 |
# File 'app/models/alchemy/picture.rb', line 57 def allowed_filetypes Config.get(:uploader).fetch('allowed_filetypes', {}).fetch('alchemy/pictures', []) end |
.filtered_by(filter = '') ⇒ Object
119 120 121 122 123 124 125 126 127 |
# File 'app/models/alchemy/picture.rb', line 119 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
95 96 97 98 99 |
# File 'app/models/alchemy/picture.rb', line 95 def last_upload last_picture = Picture.last return Picture.all unless last_picture Picture.where(upload_hash: last_picture.upload_hash) end |
.search_by(params, query, per_page = nil) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'app/models/alchemy/picture.rb', line 101 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 |
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
207 208 209 210 211 |
# File 'app/models/alchemy/picture.rb', line 207 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.
194 195 196 197 198 199 200 |
# File 'app/models/alchemy/picture.rb', line 194 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.
233 234 235 |
# File 'app/models/alchemy/picture.rb', line 233 def deletable? !essence_pictures.any? end |
#has_convertible_format? ⇒ Boolean
Returns true if the image can be converted into other formats
215 216 217 |
# File 'app/models/alchemy/picture.rb', line 215 def has_convertible_format? image_file_format.in?(CONVERTIBLE_FILE_FORMATS) end |
#humanized_name ⇒ Object
Returns a humanized, readable name from image filename.
184 185 186 187 |
# File 'app/models/alchemy/picture.rb', line 184 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
243 244 245 |
# File 'app/models/alchemy/picture.rb', line 243 def image_file_dimensions "#{image_file_width}x#{image_file_height}" end |
#next(params = {}) ⇒ Object
137 138 139 140 |
# File 'app/models/alchemy/picture.rb', line 137 def next(params = {}) query = Picture.ransack(params[:q]) Picture.search_by(params, query).where("name > ?", name).first end |
#previous(params = {}) ⇒ Object
Instance methods
132 133 134 135 |
# File 'app/models/alchemy/picture.rb', line 132 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.
227 228 229 |
# File 'app/models/alchemy/picture.rb', line 227 def restricted? pages.any? && pages.not_restricted.blank? end |
#security_token(params = {}) ⇒ Object
Returns a security token for signed picture rendering requests.
Pass a params hash containing:
size [String] (Optional)
crop [Boolean] (Optional)
crop_from [String] (Optional)
crop_size [String] (Optional)
quality [Integer] (Optional)
to sign them.
259 260 261 262 263 264 265 266 |
# File 'app/models/alchemy/picture.rb', line 259 def security_token(params = {}) params = params.dup.stringify_keys params.update({ 'crop' => params['crop'] ? 'crop' : nil, 'id' => id }) PictureAttributes.secure(params) end |
#suffix ⇒ Object
Returns the suffix of the filename.
178 179 180 |
# File 'app/models/alchemy/picture.rb', line 178 def suffix image_file.ext end |
#to_jq_upload ⇒ Object
Returns a Hash suitable for jquery fileupload json.
158 159 160 161 162 163 164 |
# File 'app/models/alchemy/picture.rb', line 158 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.
148 149 150 151 152 153 154 |
# File 'app/models/alchemy/picture.rb', line 148 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 |
#urlname ⇒ Object
Returns an uri escaped name.
168 169 170 171 172 173 174 |
# File 'app/models/alchemy/picture.rb', line 168 def urlname if name.blank? "image_#{id}" else ::CGI.escape(name.gsub(/\.(gif|png|jpe?g|tiff?)/i, '').tr('.', ' ')) end end |