Class: Workarea::Admin::ContentViewModel

Inherits:
ApplicationViewModel
  • Object
show all
Includes:
CommentableViewModel
Defined in:
app/view_models/workarea/admin/content_view_model.rb

Instance Method Summary collapse

Methods included from CommentableViewModel

#comment_count, #comments, #has_comments?, #new_comments_for?, #subscribed_users

Instance Method Details

#ambiguous_new_block_position?Boolean

HACK

This method returns whether we can reliably determine the position of the new block being added. This can be very difficult to generate due to possible resorting of blocks within a release combined with release previewing based on publishing time.

The only time this has caused an issue in use is adding a new first block, so this is used in combination with the above ‘new_block?` to decide whether to render the first block.

Returns:

  • (Boolean)


101
102
103
104
105
106
# File 'app/view_models/workarea/admin/content_view_model.rb', line 101

def ambiguous_new_block_position?
  return false unless options[:new_block].present?

  known_positions = [0] + current_blocks.map { |b| b.position + 1 }
  known_positions.exclude?(new_block.position)
end

#area_optionsArray<Array<String, String>>

Options for use in the area select tag.

Returns:

  • (Array<Array<String, String>>)


58
59
60
# File 'app/view_models/workarea/admin/content_view_model.rb', line 58

def area_options
  areas.map { |area| [area.titleize, area] }
end

#areasArray<String>

The list of available areas blocks can be added to. Specific to this type of content. Intended to be configured as part of customizing the system.

Returns:

  • (Array<String>)


30
31
32
33
34
# File 'app/view_models/workarea/admin/content_view_model.rb', line 30

def areas
  Workarea.config.content_areas[model.slug] ||
    Workarea.config.content_areas[contentable_template] ||
    Workarea.config.content_areas['generic']
end

#contentableObject



6
7
8
9
10
11
12
13
# File 'app/view_models/workarea/admin/content_view_model.rb', line 6

def contentable
  return if system?

  @contentable ||= ApplicationController.wrap_in_view_model(
    model.contentable,
    options
  )
end

#contentable_templateObject



147
148
149
# File 'app/view_models/workarea/admin/content_view_model.rb', line 147

def contentable_template
  contentable.try(:template)
end

#current_areaString

The area currently editing blocks for.

Returns:

  • (String)


66
67
68
# File 'app/view_models/workarea/admin/content_view_model.rb', line 66

def current_area
  options[:area_id].presence || areas.first
end

#current_blocksArray<Workarea::Content::Block>

The current set of blocks to display for editing. Matching the #current_area and active.

Returns:

  • (Array<Workarea::Content::Block>)


75
76
77
# File 'app/view_models/workarea/admin/content_view_model.rb', line 75

def current_blocks
  model.blocks_for(current_area).select(&:active?).select(&:persisted?)
end

#new_blockWorkarea::Content::Block

An instance of the new block being created.

Returns:

  • (Workarea::Content::Block)


112
113
114
115
116
# File 'app/view_models/workarea/admin/content_view_model.rb', line 112

def new_block
  @new_block ||= model.blocks.build(
    options.fetch(:new_block, {}).merge(data: new_block_defaults)
  )
end

#new_block?(at: nil) ⇒ Boolean

Whether there is a new block being created.

Returns:

  • (Boolean)


83
84
85
86
# File 'app/view_models/workarea/admin/content_view_model.rb', line 83

def new_block?(at: nil)
  return false unless options[:new_block].present?
  at.blank? || new_block.position == at
end

#new_block_defaultsObject



133
134
135
136
137
138
139
140
141
# File 'app/view_models/workarea/admin/content_view_model.rb', line 133

def new_block_defaults
  if options[:preset_id].present?
    Content::Preset.find(options[:preset_id]).data
  else
    Content::BlockType.find(
      options.dig(:new_block, :type_id).to_sym
    ).defaults
  end
end

#new_block_draftWorkarea::Content::BlockDraft

A block draft instance that represents the supplied state of the new block. Used for rendering the initial preview.

Returns:

  • (Workarea::Content::BlockDraft)


123
124
125
126
127
128
129
130
131
# File 'app/view_models/workarea/admin/content_view_model.rb', line 123

def new_block_draft
  @new_block_draft ||=
    Content::BlockDraft.create!(
      content_id: model.id,
      type_id: new_block.type_id,
      data: new_block.data,
      area: current_area
    )
end

#open_graph_assetObject



151
152
153
154
155
156
157
158
159
160
161
162
# File 'app/view_models/workarea/admin/content_view_model.rb', line 151

def open_graph_asset
  @open_graph_asset ||=
    if model.open_graph_asset_id.present?
      Content::Asset.find(model.open_graph_asset_id)
    elsif (og_default = Content::Asset.open_graph_default).present?
      og_default
    else
      Content::Asset.open_graph_placeholder
    end
rescue Mongoid::Errors::DocumentNotFound
  @open_graph_asset = Content::Asset.open_graph_placeholder
end

#presetsArray<Content::Preset>

List of customized content blocks available for reuse in a content area.

Returns:

  • (Array<Content::Preset>)


41
42
43
# File 'app/view_models/workarea/admin/content_view_model.rb', line 41

def presets
  @presets ||= Content::Preset.all.to_a
end

#show_advanced?Boolean

Whether to show fields that are used in the <head> of the page on which the content is rendered, like title and meta tags.

Returns:

  • (Boolean)


20
21
22
# File 'app/view_models/workarea/admin/content_view_model.rb', line 20

def show_advanced?
  (!system? && !contentable.is_a?(Navigation::Menu)) || home_page?
end

#show_areas?Boolean

Whether to show area selection for this content, not necessary if there is only one area.

Returns:

  • (Boolean)


50
51
52
# File 'app/view_models/workarea/admin/content_view_model.rb', line 50

def show_areas?
  areas.many?
end

#timelineObject



143
144
145
# File 'app/view_models/workarea/admin/content_view_model.rb', line 143

def timeline
  @timeline ||= TimelineViewModel.wrap(model)
end