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

#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 inherited from BaseRecord

#active_record_5_1?

Instance Method Details

#allow_image_cropping?(options = {}) ⇒ Boolean

Show image cropping link for content and options?

Returns:

  • (Boolean)


136
137
138
139
140
141
142
# File 'app/models/alchemy/essence_picture.rb', line 136

def allow_image_cropping?(options = {})
  content && content.settings_value(:crop, options) && picture &&
    picture.can_be_cropped_to(
      content.settings_value(:size, options),
      content.settings_value(:upsample, options)
    )
end

#crop_values_present?Boolean

Returns:

  • (Boolean)


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

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)


120
121
122
123
124
125
126
# File 'app/models/alchemy/essence_picture.rb', line 120

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)


60
61
62
63
64
# File 'app/models/alchemy/essence_picture.rb', line 60

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

  picture.url picture_url_options.merge(options)
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)


72
73
74
75
76
77
78
79
80
# File 'app/models/alchemy/essence_picture.rb', line 72

def picture_url_options
  return {} if picture.nil?

  {
    format: picture.default_render_format,
    crop_from: crop_from.presence,
    crop_size: crop_size.presence
  }.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)


112
113
114
115
# File 'app/models/alchemy/essence_picture.rb', line 112

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)


131
132
133
# File 'app/models/alchemy/essence_picture.rb', line 131

def serialized_ingredient
  picture_url(content.settings)
end

#thumbnail_url(options = {}) ⇒ String

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)


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

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

  crop = crop_values_present? || content.settings_value(:crop, options)
  size = render_size || content.settings_value(:size, options)

  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)
end