Class: RelatedPublication
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- RelatedPublication
- 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
-
#related_publication_id ⇒ Integer
Foreign key to the related publication item.
-
#relationship_type ⇒ String
Kind of link (
translationorrevision).
Belongs to collapse
Class Method Summary collapse
-
.create_bidirectional(publication1, publication2, relationship_type:, metadata: {}) ⇒ Array<RelatedPublication>
Create a bidirectional relationship between two publications.
-
.link_revision(older:, newer:) ⇒ Array<RelatedPublication>
Link two consecutive revisions of the same document (older → newer).
-
.link_translation(source:, translated:, engine:, model: nil, task_id: nil) ⇒ Array<RelatedPublication>
Link a translated publication to its source.
-
.revisions ⇒ ActiveRecord::Relation<RelatedPublication>
A relation of RelatedPublications that are revisions.
-
.translations ⇒ ActiveRecord::Relation<RelatedPublication>
A relation of RelatedPublications that are translations.
Methods inherited from ApplicationRecord
ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation
Methods included from Models::Schedulable
Methods included from Models::AfterCommittable
Methods included from Models::EventPublishable
Instance Attribute Details
#related_publication_id ⇒ Integer
Returns 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_type ⇒ String
Returns 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).
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_revision(older:, newer:) ⇒ Array<RelatedPublication>
Link two consecutive revisions of the same document (older → newer)
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_translation(source:, translated:, engine:, model: nil, task_id: nil) ⇒ Array<RelatedPublication>
Link a translated publication to its source
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 |
.revisions ⇒ ActiveRecord::Relation<RelatedPublication>
A relation of RelatedPublications that are revisions. Active Record Scope
61 |
# File 'app/models/related_publication.rb', line 61 scope :revisions, -> { where(relationship_type: REVISION) } |
.translations ⇒ ActiveRecord::Relation<RelatedPublication>
A relation of RelatedPublications that are translations. Active Record Scope
60 |
# File 'app/models/related_publication.rb', line 60 scope :translations, -> { where(relationship_type: TRANSLATION) } |