Class: Alchemy::Picture

Inherits:
BaseRecord
  • Object
show all
Includes:
ContentTouching, NameConversions, Transformations, Url, Taggable
Defined in:
app/models/alchemy/picture.rb

Defined Under Namespace

Modules: Transformations, Url

Constant Summary collapse

CONVERTIBLE_FILE_FORMATS =
%w(gif jpg jpeg png).freeze

Constants included from Url

Url::TRANSFORMATION_OPTIONS

Constants included from Transformations

Transformations::THUMBNAIL_HEIGHT, Transformations::THUMBNAIL_WIDTH

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Url

#url

Methods included from Logger

#log_warning, warn

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 ContentTouching

included

Methods included from Taggable

included, #tag_list=

Methods included from NameConversions

#convert_to_humanized_name, #convert_to_urlname

Methods inherited from BaseRecord

#active_record_5_1?

Class Method Details

.allowed_filetypesObject



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

def allowed_filetypes
  Config.get(:uploader).fetch('allowed_filetypes', {}).fetch('alchemy/pictures', [])
end

.filtered_by(filter = '') ⇒ Object



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

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_uploadObject



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

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



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'app/models/alchemy/picture.rb', line 104

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_attributesObject



94
95
96
# File 'app/models/alchemy/picture.rb', line 94

def searchable_alchemy_resource_attributes
  %w(name image_file_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

Returns:

  • (Boolean)


210
211
212
213
214
# File 'app/models/alchemy/picture.rb', line 210

def convertible?
  Config.get(:image_output_format) &&
    Config.get(: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.



197
198
199
200
201
202
203
# File 'app/models/alchemy/picture.rb', line 197

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.

Returns:

  • (Boolean)


236
237
238
# File 'app/models/alchemy/picture.rb', line 236

def deletable?
  essence_pictures.empty?
end

#has_convertible_format?Boolean

Returns true if the image can be converted into other formats

Returns:

  • (Boolean)


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

def has_convertible_format?
  image_file_format.in?(CONVERTIBLE_FILE_FORMATS)
end

#humanized_nameObject

Returns a humanized, readable name from image filename.



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

def humanized_name
  return "" if image_file_name.blank?
  convert_to_humanized_name(image_file_name, suffix)
end

#image_file_dimensionsObject

A size String from original image file values.

Example

200 x 100



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

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

#next(params = {}) ⇒ Object



140
141
142
143
# File 'app/models/alchemy/picture.rb', line 140

def next(params = {})
  query = Picture.ransack(params[:q])
  Picture.search_by(params, query).where("name > ?", name).first
end

#previous(params = {}) ⇒ Object

Instance methods



135
136
137
138
# File 'app/models/alchemy/picture.rb', line 135

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.

Returns:

  • (Boolean)


230
231
232
# File 'app/models/alchemy/picture.rb', line 230

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.



262
263
264
265
266
267
268
269
# File 'app/models/alchemy/picture.rb', line 262

def security_token(params = {})
  params = params.dup.stringify_keys
  params.update({
    'crop' => params['crop'] ? 'crop' : nil,
    'id' => id
  })
  PictureAttributes.secure(params)
end

#suffixObject

Returns the suffix of the filename.



181
182
183
# File 'app/models/alchemy/picture.rb', line 181

def suffix
  image_file.ext
end

#to_jq_uploadObject

Returns a Hash suitable for jquery fileupload json.



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

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.



151
152
153
154
155
156
157
# File 'app/models/alchemy/picture.rb', line 151

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

#urlnameObject

Returns an uri escaped name.



171
172
173
174
175
176
177
# File 'app/models/alchemy/picture.rb', line 171

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