Class: RelatedPublication

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

Overview

Typed links between publication Items — the publication analogue of
RelatedImage. A translated PDF is its own publication Item (own SKU, own
literature, publication_locales set on import); this records that it was
derived FROM a source publication so the CRM can cross-link language variants
and www can hreflang them. Provenance (engine, model, provider task id,
source/target locale) lives in metadata.

Constant Summary collapse

TRANSLATION =

Relationship types.

'translation'
REVISION =

Revision (SKU -A → -B) — linked between consecutive revisions of the same
document in the same language.

'revision'
RELATIONSHIP_TYPES =

Recognised relationship types.

[TRANSLATION, REVISION].freeze

Constants included from Models::Schedulable

Models::Schedulable::SIMPLE_FORM_OPTIONS

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::Schedulable

config

Methods included from Models::AfterCommittable

#after_commit

Methods included from Models::EventPublishable

#publish_event

Instance Attribute Details

Returns Foreign key to the related publication item.

Returns:

  • (Integer)

    Foreign key to the related publication item.



54
55
# File 'app/models/related_publication.rb', line 54

validates :related_publication_id, uniqueness: { scope: %i[publication_id relationship_type],
message: 'already has this relationship with the publication' }

#relationship_typeString

Returns Kind of link (translation or revision).

Returns:

  • (String)

    Kind of link (translation or revision).



51
# File 'app/models/related_publication.rb', line 51

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

Class Method Details

.create_bidirectional(publication1, publication2, relationship_type:, metadata: {}) ⇒ Array<RelatedPublication>

Create a bidirectional relationship between two publications.
Translations read naturally in both directions (EN ↔ FR).

Parameters:

  • publication1 (Item)

    first publication

  • publication2 (Item)

    second publication

  • relationship_type (String)

    type of relationship

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

    optional provenance

Returns:



71
72
73
74
75
76
77
78
# File 'app/models/related_publication.rb', line 71

def self.create_bidirectional(publication1, publication2, relationship_type:, metadata: {})
  transaction do
    [
      create!(publication: publication1, related_publication: publication2, relationship_type:, metadata:),
      create!(publication: publication2, related_publication: publication1, relationship_type:, metadata:)
    ]
  end
end

Link two consecutive revisions of the same document (older → newer)

Parameters:

  • older (Item)

    the superseded publication

  • newer (Item)

    the revision that replaces it

Returns:



108
109
110
111
112
113
114
115
116
117
118
119
# File 'app/models/related_publication.rb', line 108

def self.link_revision(older:, newer:)
  create_bidirectional(
    older, newer,
    relationship_type: REVISION,
    metadata: {
      from_sku:     older.sku,
      to_sku:       newer.sku,
      superseded:   older.is_discontinued,
      created_at:   Time.current.iso8601
    }
  )
end

Link a translated publication to its source

Parameters:

  • source (Item)

    the original publication

  • translated (Item)

    the translated variant

  • engine (String)

    the translation engine (e.g. 'deepl')

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

    the translation LLM

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

    the engine-side task id (for debugging)

Returns:



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

def self.link_translation(source:, translated:, engine:, model: nil, task_id: nil)
  create_bidirectional(
    source, translated,
    relationship_type: TRANSLATION,
    metadata: {
      engine:,
      model:,
      task_id:,
      source_locales: source.publication_locales,
      target_locales: translated.publication_locales,
      created_at: Time.current.iso8601
    }
  )
end

.revisionsActiveRecord::Relation<RelatedPublication>

A relation of RelatedPublications that are revisions. Active Record Scope

Returns:

See Also:



61
# File 'app/models/related_publication.rb', line 61

scope :revisions, -> { where(relationship_type: REVISION) }

.translationsActiveRecord::Relation<RelatedPublication>

A relation of RelatedPublications that are translations. Active Record Scope

Returns:

See Also:



60
# File 'app/models/related_publication.rb', line 60

scope :translations, -> { where(relationship_type: TRANSLATION) }

Instance Method Details

#publicationItem

Returns:



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

belongs_to :publication,         class_name: 'Item'

Returns:



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

belongs_to :related_publication, class_name: 'Item'