Class: Workarea::Admin::ContentViewModel
- Inherits:
-
ApplicationViewModel
- Object
- ApplicationViewModel
- Workarea::Admin::ContentViewModel
- Includes:
- CommentableViewModel
- Defined in:
- app/view_models/workarea/admin/content_view_model.rb
Instance Method Summary collapse
-
#ambiguous_new_block_position? ⇒ Boolean
HACK.
-
#area_options ⇒ Array<Array<String, String>>
Options for use in the area select tag.
-
#areas ⇒ Array<String>
The list of available areas blocks can be added to.
- #contentable ⇒ Object
- #contentable_template ⇒ Object
-
#current_area ⇒ String
The area currently editing blocks for.
-
#current_blocks ⇒ Array<Workarea::Content::Block>
The current set of blocks to display for editing.
-
#new_block ⇒ Workarea::Content::Block
An instance of the new block being created.
-
#new_block?(at: nil) ⇒ Boolean
Whether there is a new block being created.
- #new_block_defaults ⇒ Object
-
#new_block_draft ⇒ Workarea::Content::BlockDraft
A block draft instance that represents the supplied state of the new block.
- #open_graph_asset ⇒ Object
-
#presets ⇒ Array<Content::Preset>
List of customized content blocks available for reuse in a content area.
-
#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.
-
#show_areas? ⇒ Boolean
Whether to show area selection for this content, not necessary if there is only one area.
- #timeline ⇒ Object
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.
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 [:new_block].present? known_positions = [0] + current_blocks.map { |b| b.position + 1 } known_positions.exclude?(new_block.position) end |
#area_options ⇒ Array<Array<String, String>>
Options for use in the area select tag.
58 59 60 |
# File 'app/view_models/workarea/admin/content_view_model.rb', line 58 def areas.map { |area| [area.titleize, area] } end |
#areas ⇒ Array<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.
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 |
#contentable ⇒ Object
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, ) end |
#contentable_template ⇒ Object
147 148 149 |
# File 'app/view_models/workarea/admin/content_view_model.rb', line 147 def contentable_template contentable.try(:template) end |
#current_area ⇒ String
The area currently editing blocks for.
66 67 68 |
# File 'app/view_models/workarea/admin/content_view_model.rb', line 66 def current_area [:area_id].presence || areas.first end |
#current_blocks ⇒ Array<Workarea::Content::Block>
The current set of blocks to display for editing. Matching the #current_area and active.
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_block ⇒ Workarea::Content::Block
An instance of the new block being created.
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( .fetch(:new_block, {}).merge(data: new_block_defaults) ) end |
#new_block?(at: nil) ⇒ Boolean
Whether there is a new block being created.
83 84 85 86 |
# File 'app/view_models/workarea/admin/content_view_model.rb', line 83 def new_block?(at: nil) return false unless [:new_block].present? at.blank? || new_block.position == at end |
#new_block_defaults ⇒ Object
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 [:preset_id].present? Content::Preset.find([:preset_id]).data else Content::BlockType.find( .dig(:new_block, :type_id).to_sym ).defaults end end |
#new_block_draft ⇒ Workarea::Content::BlockDraft
A block draft instance that represents the supplied state of the new block. Used for rendering the initial preview.
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_asset ⇒ Object
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 |
#presets ⇒ Array<Content::Preset>
List of customized content blocks available for reuse in a content area.
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.
20 21 22 |
# File 'app/view_models/workarea/admin/content_view_model.rb', line 20 def show_advanced? (!system? && !contentable.is_a?(::Menu)) || home_page? end |
#show_areas? ⇒ Boolean
Whether to show area selection for this content, not necessary if there is only one area.
50 51 52 |
# File 'app/view_models/workarea/admin/content_view_model.rb', line 50 def show_areas? areas.many? end |
#timeline ⇒ Object
143 144 145 |
# File 'app/view_models/workarea/admin/content_view_model.rb', line 143 def timeline @timeline ||= TimelineViewModel.wrap(model) end |