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?

Instance Method Details

#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



126
127
128
# File 'app/view_models/workarea/admin/content_view_model.rb', line 126

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)


91
92
93
94
95
# File 'app/view_models/workarea/admin/content_view_model.rb', line 91

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

#new_block?Boolean

Whether there is a new block being created.

Returns:

  • (Boolean)


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

def new_block?
  options[:new_block].present?
end

#new_block_defaultsObject



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

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)


102
103
104
105
106
107
108
109
110
# File 'app/view_models/workarea/admin/content_view_model.rb', line 102

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



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

def open_graph_asset
  @open_graph_asset ||=
    if model.open_graph_asset_id.present?
      Content::Asset.find(model.open_graph_asset_id)
    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



122
123
124
# File 'app/view_models/workarea/admin/content_view_model.rb', line 122

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