Class: RelatedImage

Inherits:
ApplicationRecord show all
Defined in:
app/models/related_image.rb

Overview

== Schema Information

Table name: related_images
Database name: primary

id :bigint not null, primary key
metadata :jsonb
relationship_type :string default("variant"), not null
created_at :datetime not null
updated_at :datetime not null
image_id :bigint not null
related_image_id :bigint not null

Indexes

idx_related_images_by_type (image_id,relationship_type)
idx_related_images_reverse (related_image_id,relationship_type)
idx_related_images_unique_relationship (image_id,related_image_id,relationship_type) UNIQUE

Foreign Keys

fk_rails_... (image_id => digital_assets.id)
fk_rails_... (related_image_id => digital_assets.id)

Constant Summary collapse

UPSCALED =

Relationship types

'upscaled'
DUPLICATE =
'duplicate'
VARIANT =
'variant'
RESIZED =
'resized'
RELATIONSHIP_TYPES =
[UPSCALED, DUPLICATE, VARIANT, RESIZED].freeze

Instance Attribute Summary collapse

Belongs to collapse

Class Method Summary collapse

Methods inherited from ApplicationRecord

ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation

Methods included from Models::EventPublishable

#publish_event

Instance Attribute Details



39
40
# File 'app/models/related_image.rb', line 39

validates :related_image_id, uniqueness: { scope: %i[image_id relationship_type],
message: 'already has this relationship with the image' }

#relationship_typeObject (readonly)



38
# File 'app/models/related_image.rb', line 38

validates :relationship_type, presence: true, inclusion: { in: RELATIONSHIP_TYPES }

Class Method Details

.create_bidirectional(image1, image2, relationship_type:, metadata: {}) ⇒ Array<RelatedImage>

Create a bidirectional relationship between two images
For some relationship types (like duplicates), we want both directions

Parameters:

  • image1 (Image)

    First image

  • image2 (Image)

    Second image

  • relationship_type (String)

    Type of relationship

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

    Optional metadata

Returns:



59
60
61
62
63
64
65
66
# File 'app/models/related_image.rb', line 59

def self.create_bidirectional(image1, image2, relationship_type:, metadata: {})
  transaction do
    [
      create!(image: image1, related_image: image2, relationship_type: relationship_type, metadata: ),
      create!(image: image2, related_image: image1, relationship_type: relationship_type, metadata: )
    ]
  end
end

.duplicatesActiveRecord::Relation<RelatedImage>

A relation of RelatedImages that are duplicates. Active Record Scope

Returns:

See Also:



46
# File 'app/models/related_image.rb', line 46

scope :duplicates, -> { where(relationship_type: DUPLICATE) }

Link an upscaled image to its original

Parameters:

  • original (Image)

    The original image

  • upscaled (Image)

    The upscaled version

  • engine (String)

    The upscaling engine used (e.g., 'imagekit', 'topaz')

  • model (String, nil) (defaults to: nil)

    The model used (for Topaz)

Returns:



76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/models/related_image.rb', line 76

def self.link_upscaled(original:, upscaled:, engine:, model: nil)
  create!(
    image: original,
    related_image: upscaled,
    relationship_type: UPSCALED,
    metadata: {
      engine: engine,
      model: model,
      original_dimensions: "#{original.attachment_width}x#{original.attachment_height}",
      upscaled_dimensions: "#{upscaled.attachment_width}x#{upscaled.attachment_height}",
      created_at: Time.current.iso8601
    }
  )
end

.resizedActiveRecord::Relation<RelatedImage>

A relation of RelatedImages that are resized. Active Record Scope

Returns:

See Also:



48
# File 'app/models/related_image.rb', line 48

scope :resized, -> { where(relationship_type: RESIZED) }

.upscaledActiveRecord::Relation<RelatedImage>

A relation of RelatedImages that are upscaled. Active Record Scope

Returns:

See Also:



45
# File 'app/models/related_image.rb', line 45

scope :upscaled, -> { where(relationship_type: UPSCALED) }

.variantsActiveRecord::Relation<RelatedImage>

A relation of RelatedImages that are variants. Active Record Scope

Returns:

See Also:



47
# File 'app/models/related_image.rb', line 47

scope :variants, -> { where(relationship_type: VARIANT) }

Instance Method Details

#imageImage

Returns:

See Also:



35
# File 'app/models/related_image.rb', line 35

belongs_to :image, class_name: 'Image'

Returns:

See Also:



36
# File 'app/models/related_image.rb', line 36

belongs_to :related_image, class_name: 'Image'