Class: Alchemy::Picture

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
NameConversions, Sweeping, Transformations, Touching
Defined in:
app/models/alchemy/picture.rb

Defined Under Namespace

Modules: Sweeping, Transformations

Class Method Summary collapse

Instance Method Summary collapse

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

#touch

Methods included from NameConversions

#convert_to_humanized_name, #convert_to_urlname

Class Method Details

.filtered_by(filter = '') ⇒ Object



110
111
112
113
114
115
116
117
118
# File 'app/models/alchemy/picture.rb', line 110

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

.find_paginated(params, per_page) ⇒ Object

Returns filtered, paginated and ordered picture collection.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/models/alchemy/picture.rb', line 88

def find_paginated(params, per_page)
  @pictures = Picture.all

  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 params[:query].present?
    @pictures = @pictures.named(params[:query])
  end

  @pictures.page(params[:page] || 1).per(per_page).order(:name)
end

.last_uploadObject



104
105
106
107
108
# File 'app/models/alchemy/picture.rb', line 104

def last_upload
  last_picture = Picture.last
  return Picture.all unless last_picture
  Picture.where(upload_hash: last_picture.upload_hash)
end

Instance Method Details

#deletable?Boolean

Returns true if picture is not assigned to any EssencePicture.

Returns:

  • (Boolean)


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

def deletable?
  !essence_pictures.any?
end

#humanized_nameObject

Returns a humanized, readable name from image filename.



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

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



194
195
196
# File 'app/models/alchemy/picture.rb', line 194

def image_file_dimensions
  "#{image_file_width} x #{image_file_height}"
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)


178
179
180
# File 'app/models/alchemy/picture.rb', line 178

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.



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

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

#suffixObject

Returns the suffix of the filename.



159
160
161
# File 'app/models/alchemy/picture.rb', line 159

def suffix
  image_file.ext
end

#to_jq_uploadObject

Returns a Hash suitable for jquery fileupload json.



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

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.



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

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]
  self.save!
end

#urlnameObject

Returns an uri escaped name.



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

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