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



64
65
66
# File 'app/models/alchemy/picture.rb', line 64

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

.filtered_by(filter = '') ⇒ Object



126
127
128
129
130
131
132
133
134
# File 'app/models/alchemy/picture.rb', line 126

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



102
103
104
105
106
# File 'app/models/alchemy/picture.rb', line 102

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



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'app/models/alchemy/picture.rb', line 108

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



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

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)


214
215
216
217
218
# File 'app/models/alchemy/picture.rb', line 214

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.



201
202
203
204
205
206
207
# File 'app/models/alchemy/picture.rb', line 201

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)


240
241
242
# File 'app/models/alchemy/picture.rb', line 240

def deletable?
  essence_pictures.empty?
end

#has_convertible_format?Boolean

Returns true if the image can be converted into other formats

Returns:

  • (Boolean)


222
223
224
# File 'app/models/alchemy/picture.rb', line 222

def has_convertible_format?
  image_file_format.in?(CONVERTIBLE_FILE_FORMATS)
end

#humanized_nameObject

Returns a humanized, readable name from image filename.



191
192
193
194
# File 'app/models/alchemy/picture.rb', line 191

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



250
251
252
# File 'app/models/alchemy/picture.rb', line 250

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

#next(params = {}) ⇒ Object



144
145
146
147
# File 'app/models/alchemy/picture.rb', line 144

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

#previous(params = {}) ⇒ Object

Instance methods



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

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)


234
235
236
# File 'app/models/alchemy/picture.rb', line 234

def restricted?
  pages.any? && pages.not_restricted.blank?
end

#suffixObject

Returns the suffix of the filename.



185
186
187
# File 'app/models/alchemy/picture.rb', line 185

def suffix
  image_file.ext
end

#to_jq_uploadObject

Returns a Hash suitable for jquery fileupload json.



165
166
167
168
169
170
171
# File 'app/models/alchemy/picture.rb', line 165

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.



155
156
157
158
159
160
161
# File 'app/models/alchemy/picture.rb', line 155

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.



175
176
177
178
179
180
181
# File 'app/models/alchemy/picture.rb', line 175

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