Sha256: ffbd4c0dbe9a5895aab0474588c6a6eb94e9ab06a5d93729a94eeb0efcbd5c9e

Contents?: true

Size: 1.83 KB

Versions: 10

Compression:

Stored size: 1.83 KB

Contents

# frozen_string_literal: true

# Base Tag class that other fragment tags depend on.
# Tag handles following options:
#   `render`: true (default) | false
#     do we want to render this content on the page, or manually access it via
#     helpers. Good example would be content for meta tags.
#   `namespace`:
#     Just a string that allows grouping of form elements in the admin area
#
class Occams::Content::Tag::Fragment < Occams::Content::Tag
  attr_accessor :renderable
  attr_reader :identifier, :namespace

  # @type [{String => String}]
  attr_reader :options

  def initialize(context:, params: [], source: nil)
    super

    @options    = params.extract_options!
    @identifier = params[0]

    unless @identifier.present?
      raise Error, "Missing identifier for fragment tag: #{source}"
    end

    @namespace  = @options['namespace'] || 'default'
    @renderable = @options['render'].to_s.downcase != 'false'
  end

  # Grabs existing fragment record or spins up a new instance if there's none
  # @return [Occams::Cms::Fragment]
  def fragment
    context.fragments.detect { |f| f.identifier == identifier } ||
      context.fragments.build(identifier: identifier)
  end

  def content
    fragment.content
  end

  # If `render: false` was passed in we won't render anything. Assuming that
  # that fragment content will be rendered manually
  def render
    renderable ? content : ''
  end

  # Tag renders its own form inputs via `form_field(template, index)`
  # For example:
  #   class MyTag < Occams::Content::Tag::Fragment
  #     def form_field(view, index, &block)
  #       # omit yield if you don't want default wrapper
  #       yield view.text_area "input_name", "value"
  #     end
  #   end
  def form_field
    raise 'Form field rendering not implemented for this Tag'
  end

  def form_field_id
    "fragment-#{@identifier}"
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
occams-1.0.7.3 lib/occams/content/tags/fragment.rb
occams-1.0.7.2 lib/occams/content/tags/fragment.rb
occams-1.0.7.1 lib/occams/content/tags/fragment.rb
occams-1.0.7 lib/occams/content/tags/fragment.rb
occams-1.0.6.1 lib/occams/content/tags/fragment.rb
occams-1.0.6 lib/occams/content/tags/fragment.rb
occams-1.0.5 lib/occams/content/tags/fragment.rb
occams-1.0.4 lib/occams/content/tags/fragment.rb
occams-1.0.3 lib/occams/content/tags/fragment.rb
occams-1.0.2 lib/occams/content/tags/fragment.rb