Class: Workarea::Content::Block

Inherits:
Object
  • Object
show all
Includes:
ApplicationDocument, Ordering, Releasable
Defined in:
app/models/workarea/content/block.rb

Overview

This class represents a single block of content. These are grouped by their area field for display in the storefront.

The actual HTML rendered is defined by the type field. This field is used to decide which partial is used to display for storefront. The data field is passed to the partial as local assigns.

This flexibility makes it very easy to add a new type to the system: 1) Define the block type using the DSL (Workarea.define_content_block_types) 2) Add the storefront partial, e.g. workarea/storefront/content_blocks/types/_foo.html.haml

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Ordering

#higher_siblings, #lower_siblings

Methods included from Releasable

#changesets_with_children, #destroy, #in_release, #release_changes, #release_originals, #save_changeset, #skip_changeset, #without_release

Methods included from Segmentable

#active?, #active_segment_ids_with_children, #segmented?, #segments

Methods included from Release::Activation

#activate_with?, #create_activation_changeset, #save_activate_with, #was_active?

Methods included from ApplicationDocument

#releasable?

Methods included from Sidekiq::Callbacks

add_worker, assert_valid_config!, async, caching_classes?, disable, enable, inline, #run_callbacks, workers, workers_list

Methods included from Mongoid::Document

#embedded_children

Instance Attribute Details

#areaString

Returns what area on the page this block belongs to.

Returns:

  • (String)

    what area on the page this block belongs to



27
# File 'app/models/workarea/content/block.rb', line 27

field :area, type: String, default: 'default'

#contentContent

Returns:



45
46
47
# File 'app/models/workarea/content/block.rb', line 45

embedded_in :content,
class_name: 'Workarea::Content',
inverse_of: :blocks

#dataHashWithIndifferentAccess

The bag of data used to render this content block on the storefront. Any relevant data for rendering can be stored here. All data will be typecasted and validated by the Field system.

Returns:

  • (HashWithIndifferentAccess)


32
33
34
35
# File 'app/models/workarea/content/block.rb', line 32

field :data,
type: HashWithIndifferentAccess,
default: HashWithIndifferentAccess.new,
localize: true

#hidden_breakpointsArray

Returns the breakpoints for which the block will be hidden.

Returns:

  • (Array)

    the breakpoints for which the block will be hidden



40
# File 'app/models/workarea/content/block.rb', line 40

field :hidden_breakpoints, type: Array, default: []

#type_idSymbol

Returns the content block type id.

Returns:

  • (Symbol)

    the content block type id



22
# File 'app/models/workarea/content/block.rb', line 22

field :type_id, type: Symbol

Instance Method Details

#at_bottom?Boolean

Whether this block is at the top of the blocks list

Returns:

  • (Boolean)


126
127
128
# File 'app/models/workarea/content/block.rb', line 126

def at_bottom?
  self == siblings.select(&:active?).last
end

#at_top?Boolean

Whether this block is at the top of the blocks list

Returns:

  • (Boolean)


118
119
120
# File 'app/models/workarea/content/block.rb', line 118

def at_top?
  self == siblings.select(&:active?).first
end

#configHashWithIndifferentAccess

Any miscellaneous config set on the #type by the content block DSL.

Returns:

  • (HashWithIndifferentAccess)


110
111
112
# File 'app/models/workarea/content/block.rb', line 110

def config
  type.config.with_indifferent_access
end

#nameString

Tries to find a unique name for this block relative to the other blocks in the content area.

Returns:



63
64
65
# File 'app/models/workarea/content/block.rb', line 63

def name
  BlockName.new(self).to_s
end

#to_draftObject



130
131
132
133
134
# File 'app/models/workarea/content/block.rb', line 130

def to_draft
  result = Content::BlockDraft.instantiate(as_document.merge('content_id' => _parent.id))
  result.new_record = true
  result
end

#typeWorkarea::Content::BlockType

The Workarea::Content::BlockType that this block is. See documentation for Workarea.define_content_block_types for info how to define block types.



98
99
100
# File 'app/models/workarea/content/block.rb', line 98

def type
  Configuration::ContentBlocks.types.detect { |bt| bt.id == type_id }
end

#type=(value) ⇒ Object



102
103
104
# File 'app/models/workarea/content/block.rb', line 102

def type=(value)
  self.type_id = value.is_a?(BlockType) ? value.id : value
end