Class: ContentLink
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- ContentLink
- Defined in:
- app/models/content_link.rb
Overview
== Schema Information
Table name: content_links
Database name: primary
id :bigint not null, primary key
context :text
created_by_type :string default("manual"), not null
link_type :string not null
position :integer
source_type :string not null
target_type :string not null
created_at :datetime not null
updated_at :datetime not null
source_id :bigint not null
target_id :bigint not null
Indexes
idx_content_links_unique_pair (source_type,source_id,target_type,target_id,link_type) UNIQUE
index_content_links_on_source_type_and_source_id_and_link_type (source_type,source_id,link_type)
index_content_links_on_target_type_and_target_id (target_type,target_id)
Constant Summary collapse
- LINK_TYPES =
Recognised link types.
%w[related_post related_video related_showcase related_faq related_publication see_also].freeze
- CREATED_BY_TYPES =
Recognised created by types.
%w[manual seo_recommendation sunny].freeze
- TARGET_TYPE_CONFIG =
Registry of supported target types — drives the CRM editor dropdowns and lookup URLs.
Keys match polymorphic type as stored by Rails (base class for STI models).
Add entries here as new content types become linkable. { 'Article' => { label: 'Blog Post', link_type: 'related_post', display_field: :subject, search_class: 'Post' }, 'DigitalAsset' => { label: 'Video', link_type: 'related_video', display_field: :title, search_class: 'Video' }, 'Showcase' => { label: 'Showcase', link_type: 'related_showcase', display_field: :name }, 'Item' => { label: 'Publication', link_type: 'related_publication', display_field: :name, base_scope: :publications } }.freeze
Constants included from Schedulable
Schedulable::SIMPLE_FORM_OPTIONS
Instance Attribute Summary collapse
- #created_by_type ⇒ Object readonly
- #link_type ⇒ Object readonly
- #target_id ⇒ Object readonly
Belongs to collapse
Class Method Summary collapse
-
.from_source ⇒ ActiveRecord::Relation<ContentLink>
A relation of ContentLinks that are from source.
-
.of_type ⇒ ActiveRecord::Relation<ContentLink>
A relation of ContentLinks that are of type.
-
.ordered ⇒ ActiveRecord::Relation<ContentLink>
A relation of ContentLinks that are ordered.
-
.targeting ⇒ ActiveRecord::Relation<ContentLink>
A relation of ContentLinks that are targeting.
Instance Method Summary collapse
Methods inherited from ApplicationRecord
ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation
Methods included from Schedulable
Methods included from Models::AfterCommittable
Methods included from Models::EventPublishable
Instance Attribute Details
#created_by_type ⇒ Object (readonly)
46 |
# File 'app/models/content_link.rb', line 46 validates :created_by_type, inclusion: { in: CREATED_BY_TYPES } |
#link_type ⇒ Object (readonly)
45 |
# File 'app/models/content_link.rb', line 45 validates :link_type, presence: true, inclusion: { in: LINK_TYPES } |
#target_id ⇒ Object (readonly)
47 48 |
# File 'app/models/content_link.rb', line 47 validates :target_id, uniqueness: { scope: %i[source_type source_id target_type link_type], message: 'is already linked with this type' } |
Class Method Details
.from_source ⇒ ActiveRecord::Relation<ContentLink>
A relation of ContentLinks that are from source. Active Record Scope
53 |
# File 'app/models/content_link.rb', line 53 scope :from_source, ->(record) { where(source: record) } |
.of_type ⇒ ActiveRecord::Relation<ContentLink>
A relation of ContentLinks that are of type. Active Record Scope
50 |
# File 'app/models/content_link.rb', line 50 scope :of_type, ->(type) { where(link_type: type) } |
.ordered ⇒ ActiveRecord::Relation<ContentLink>
A relation of ContentLinks that are ordered. Active Record Scope
51 |
# File 'app/models/content_link.rb', line 51 scope :ordered, -> { order(Arel.sql('position ASC NULLS LAST, created_at ASC')) } |
.targeting ⇒ ActiveRecord::Relation<ContentLink>
A relation of ContentLinks that are targeting. Active Record Scope
52 |
# File 'app/models/content_link.rb', line 52 scope :targeting, ->(klass) { where(target_type: klass.to_s) } |
Instance Method Details
#created_by_badge_class ⇒ Object
70 71 72 73 74 75 76 |
# File 'app/models/content_link.rb', line 70 def created_by_badge_class case created_by_type when 'seo_recommendation' then 'bg-info' when 'sunny' then 'bg-warning text-dark' else 'bg-secondary' end end |
#created_by_label ⇒ Object
62 63 64 65 66 67 68 |
# File 'app/models/content_link.rb', line 62 def created_by_label case created_by_type when 'seo_recommendation' then 'SEO' when 'sunny' then 'Sunny' else 'Manual' end end |
#source ⇒ Source
42 |
# File 'app/models/content_link.rb', line 42 belongs_to :source, polymorphic: true |
#target ⇒ Target
43 |
# File 'app/models/content_link.rb', line 43 belongs_to :target, polymorphic: true |
#target_display_name ⇒ Object
55 56 57 58 59 60 |
# File 'app/models/content_link.rb', line 55 def target_display_name return 'Unknown' unless target field = TARGET_TYPE_CONFIG.dig(target_type, :display_field) field ? target.public_send(field) : target.to_s end |