Class: Alchemy::EssencePicture

Inherits:
BaseRecord
  • Object
show all
Includes:
Picture::Transformations
Defined in:
app/models/alchemy/essence_picture.rb

Constant Summary

Constants included from Picture::Transformations

Picture::Transformations::THUMBNAIL_HEIGHT, Picture::Transformations::THUMBNAIL_WIDTH

Instance Method Summary collapse

Methods included from Picture::Transformations

#crop, #crop_size?, #default_mask, #landscape_format?, #portrait_format?, #render_size?, #resize, #square_format?, #thumbnail_size

Instance Method Details

#allow_image_cropping?Boolean

Show image cropping link for content

Returns:

  • (Boolean)

141
142
143
144
145
146
147
# File 'app/models/alchemy/essence_picture.rb', line 141

def allow_image_cropping?
  content && content.settings[:crop] && picture &&
    picture.can_be_cropped_to?(
      content.settings[:size],
      content.settings[:upsample],
    ) && !!picture.image_file
end

#crop_values_present?Boolean

Returns:

  • (Boolean)

149
150
151
# File 'app/models/alchemy/essence_picture.rb', line 149

def crop_values_present?
  crop_from.present? && crop_size.present?
end

#cropping_maskHash

A Hash of coordinates suitable for the graphical image cropper.

Returns:

  • (Hash)

124
125
126
127
128
129
130
131
# File 'app/models/alchemy/essence_picture.rb', line 124

def cropping_mask
  return if crop_from.blank? || crop_size.blank?

  crop_from = point_from_string(read_attribute(:crop_from))
  crop_size = sizes_from_string(read_attribute(:crop_size))

  point_and_mask_to_points(crop_from, crop_size)
end

#picture_url(options = {}) ⇒ String

The url to show the picture.

Takes all values like name and crop sizes (crop_from, crop_size from the build in graphical image cropper) and also adds the security token.

You typically want to set the size the picture should be resized to.

Example:

essence_picture.picture_url(size: '200x300', crop: true, format: 'gif')
# '/pictures/1/show/200x300/crop/cats.gif?sh=765rfghj'

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • size (String)

    The size the picture should be resized to.

  • format (String)

    The format the picture should be rendered in. Defaults to the image_output_format from the Alchemy::Config.

  • crop (Boolean)

    If set to true the picture will be cropped to fit the size value.

Returns:

  • (String)

62
63
64
65
66
# File 'app/models/alchemy/essence_picture.rb', line 62

def picture_url(options = {})
  return if picture.nil?

  picture.url(picture_url_options.merge(options)) || "missing-image.png"
end

#picture_url_optionsHashWithIndifferentAccess

Picture rendering options

Returns the default_render_format of the associated Alchemy::Picture together with the crop_from and crop_size values

Returns:

  • (HashWithIndifferentAccess)

74
75
76
77
78
79
80
81
82
83
# File 'app/models/alchemy/essence_picture.rb', line 74

def picture_url_options
  return {} if picture.nil?

  {
    format: picture.default_render_format,
    crop_from: crop_from.presence,
    crop_size: crop_size.presence,
    size: content.settings[:size],
  }.with_indifferent_access
end

#preview_text(max = 30) ⇒ String

The name of the picture used as preview text in element editor views.

Parameters:

  • max (Integer) (defaults to: 30)

    The maximum length of the text returned.

Returns:

  • (String)

115
116
117
118
119
# File 'app/models/alchemy/essence_picture.rb', line 115

def preview_text(max = 30)
  return "" if picture.nil?

  picture.name.to_s[0..max - 1]
end

#serialized_ingredientString

Returns a serialized ingredient value for json api

Returns:

  • (String)

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

def serialized_ingredient
  picture_url(content.settings)
end

#thumbnail_urlString

Returns an url for the thumbnail representation of the assigned picture

It takes cropping values into account, so it always represents the current image displayed in the frontend.

Returns:

  • (String)

91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'app/models/alchemy/essence_picture.rb', line 91

def thumbnail_url
  return if picture.nil?

  crop = crop_values_present? || content.settings[:crop]
  size = render_size || content.settings[:size]

  options = {
    size: thumbnail_size(size, crop),
    crop: !!crop,
    crop_from: crop_from.presence,
    crop_size: crop_size.presence,
    flatten: true,
    format: picture.image_file_format,
  }

  picture.url(options) || "alchemy/missing-image.svg"
end