Class: EmbeddedAsset

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

Overview

== Schema Information

Table name: embedded_assets
Database name: primary

id :bigint not null, primary key
asset_type :string
options :jsonb not null
parent_type :string not null
type :string
uuid :uuid not null
created_at :datetime not null
updated_at :datetime not null
asset_id :bigint
parent_id :bigint not null

Indexes

idx_embedded_assets_on_asset_and_type (asset_id,asset_type,type)
idx_embedded_assets_parent_asset (parent_type,parent_id,asset_type,asset_id)
index_embedded_assets_on_asset (asset_type,asset_id)
index_embedded_assets_on_type (type)
index_embedded_assets_on_uuid (uuid)

Instance Attribute Summary collapse

Belongs to collapse

Class Method Summary collapse

Instance 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

#uuidObject (readonly)



33
# File 'app/models/embedded_asset.rb', line 33

validates :uuid, presence: true

Class Method Details

.faqsActiveRecord::Relation<EmbeddedAsset>

A relation of EmbeddedAssets that are faqs. Active Record Scope

Returns:

See Also:



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

scope :faqs, -> { where(type: 'EmbeddedFaqAsset') }

.for_asset_type(asset_type) ⇒ Object

Factory method to create the appropriate subclass



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

def self.for_asset_type(asset_type)
  case asset_type.to_s
  when 'Image'   then EmbeddedImageAsset
  when 'Video'   then EmbeddedVideoAsset
  when 'Faq'     then EmbeddedFaqAsset
  when 'Item', 'Product' then EmbeddedProductAsset
  else self
  end
end

.for_parentActiveRecord::Relation<EmbeddedAsset>

A relation of EmbeddedAssets that are for parent. Active Record Scope

Returns:

See Also:



42
# File 'app/models/embedded_asset.rb', line 42

scope :for_parent, ->(parent) { where(parent: parent) }

.imagesActiveRecord::Relation<EmbeddedAsset>

A relation of EmbeddedAssets that are images. Active Record Scope

Returns:

See Also:



43
# File 'app/models/embedded_asset.rb', line 43

scope :images, -> { where(type: 'EmbeddedImageAsset') }

.productsActiveRecord::Relation<EmbeddedAsset>

A relation of EmbeddedAssets that are products. Active Record Scope

Returns:

See Also:



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

scope :products, -> { where(type: 'EmbeddedProductAsset') }

.videosActiveRecord::Relation<EmbeddedAsset>

A relation of EmbeddedAssets that are videos. Active Record Scope

Returns:

See Also:



44
# File 'app/models/embedded_asset.rb', line 44

scope :videos, -> { where(type: 'EmbeddedVideoAsset') }

Instance Method Details

#assetAsset

Optional for FAQs (multiple IDs in options)

Returns:

  • (Asset)

See Also:

Validations:



28
# File 'app/models/embedded_asset.rb', line 28

belongs_to :asset, polymorphic: true, optional: true

#parentParent

Returns:

  • (Parent)

See Also:

Validations:



27
# File 'app/models/embedded_asset.rb', line 27

belongs_to :parent, polymorphic: true

#render_optionsObject

Subclasses override this to return asset-type-specific options



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

def render_options
  options.symbolize_keys
end

#to_paramObject

Use UUID as the URL parameter instead of ID



49
50
51
# File 'app/models/embedded_asset.rb', line 49

def to_param
  uuid
end