class ContentBlockTools::Presenters::BasePresenter
def self.has_embedded_objects(*object_types)
def self.has_embedded_objects(*object_types) @embedded_objects = object_types object_types.each do |object_type| define_method(object_type) do embedded_objects_of_type(object_type) end end end
def base_tag
def base_tag field_names ? field_or_block_presenter::BASE_TAG_TYPE : self.class::BASE_TAG_TYPE end
def block_presenter
def block_presenter @block_presenter ||= field_names ? self.class::BLOCK_PRESENTERS[field_names.first] : nil end
def content
-
(string)
- A representation of the content block to be wrapped in the base_tag in
def content ContentBlockTools.logger.info("Getting content for content block #{content_block.content_id}") field_names.present? ? content_for_field_names : default_content end
def content_for_field_names
def content_for_field_names if field_or_block_content.blank? ContentBlockTools.logger.warn("Content not found for content block #{content_block.content_id} and fields #{field_names}") return content_block.embed_code end field_or_block_presenter.new(field_or_block_content, rendering_context: :field_names, content_block:).render end
def default_content
def default_content content_block.title end
def embedded_objects
def embedded_objects self.class.instance_variable_get("@embedded_objects") end
def embedded_objects_of_type(type)
def embedded_objects_of_type(type) content_block.details.fetch(type, {}).values end
def field_names
def field_names @field_names ||= begin embed_code_match = ContentBlockReference::EMBED_REGEX.match(content_block.embed_code) if embed_code_match.present? all_fields = embed_code_match[4]&.reverse&.chomp("/")&.reverse all_fields&.split("/")&.map do |item| is_number?(item) ? item.to_i : item.to_sym end end end end
def field_or_block_content
def field_or_block_content @field_or_block_content ||= field_names.any? ? content_block.details.deep_symbolize_keys.dig(*field_names) : nil end
def field_or_block_presenter
def field_or_block_presenter @field_or_block_presenter ||= if rendering_block? block_presenter || ContentBlockTools::Presenters::BlockPresenters::BasePresenter else field_presenter || ContentBlockTools::Presenters::FieldPresenters::BasePresenter end end
def field_presenter
def field_presenter @field_presenter ||= field_names ? self.class::FIELD_PRESENTERS[field_names.last] : nil end
def initialize(content_block)
-
({ContentBlockTools::Presenters::BasePresenter})
-
Parameters:
-
content_block
({ContentBlockTools::ContentBlock}
) -- A content block object
def initialize(content_block) @content_block = content_block end
def is_number?(item)
def is_number?(item) Float(item, exception: false) end
def render
-
(string)
- A HTML representation of the content block
def render content_tag( base_tag, content, class: %W[content-embed content-embed__#{content_block.document_type}], data: { content_block: "", document_type: content_block.document_type, content_id: content_block.content_id, embed_code: content_block.embed_code, }, ) end
def rendering_block?
def rendering_block? field_or_block_content.is_a?(Hash) end