Class: Decidim::MetaImageUrlResolver

Inherits:
Object
  • Object
show all
Includes:
TranslatableAttributes
Defined in:
app/resolvers/decidim/meta_image_url_resolver.rb

Overview

Resolves the most appropriate image URL for meta tags from a given resource within an organization. Searches in the following order: attachment images, images in resource description, participatory space images, and default content block images. Ensures the selected image is resized to 1200x630 pixels.

Constant Summary collapse

IMAGE_FIELDS =
[:hero_image, :banner_image, :avatar].freeze
DESCRIPTION_FIELDS =
[:body, :description, :short_description, :content, :additional_info].freeze
CONTENT_BLOCK_TYPES =
{
  { manifest_name: :hero, scope_name: :homepage } => [:background_image]
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from TranslatableAttributes

#attachment?, #default_locale?

Constructor Details

#initialize(resource, organization) ⇒ MetaImageUrlResolver

Returns a new instance of MetaImageUrlResolver.



16
17
18
19
# File 'app/resolvers/decidim/meta_image_url_resolver.rb', line 16

def initialize(resource, organization)
  @resource = resource
  @organization = organization
end

Instance Attribute Details

#organizationObject (readonly)

Returns the value of attribute organization.



21
22
23
# File 'app/resolvers/decidim/meta_image_url_resolver.rb', line 21

def organization
  @organization
end

#resourceObject (readonly)

Returns the value of attribute resource.



21
22
23
# File 'app/resolvers/decidim/meta_image_url_resolver.rb', line 21

def resource
  @resource
end

Instance Method Details

#blobActiveStorage::Blob?

Determines the image blob to be used for meta tags by following a hierarchy.

Returns:

  • (ActiveStorage::Blob, nil)
    • The image blob or nil if no image is found.



37
38
39
# File 'app/resolvers/decidim/meta_image_url_resolver.rb', line 37

def blob
  @blob ||= blob_from_image_fields || blob_from_attachments || blob_from_description || blob_from_participatory_space || blob_from_content_blocks
end

#resolveString?

Resolves the image blob to be used for meta tags.

Returns:

  • (String, nil)
    • The resolved image blob or nil if no image is found.



26
27
28
29
30
31
32
# File 'app/resolvers/decidim/meta_image_url_resolver.rb', line 26

def resolve
  return unless blob
  return unless blob.service.exist?(blob.key)

  resized_variant = blob.variant(resize_to_limit: [1200, 630]).processed
  Rails.application.routes.url_helpers.rails_representation_url(resized_variant, only_path: true)
end