Sha256: 5e75365d8ce4fe685f88d4df8b1641a1c748ea2a5d8d58b395550f7327be2d55

Contents?: true

Size: 1.88 KB

Versions: 15

Compression:

Stored size: 1.88 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 ComfortableMexicanSofa::Content::Tag::Fragment < ComfortableMexicanSofa::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 [Comfy::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 < ComfortableMexicanSofa::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

15 entries across 15 versions & 6 rubygems

Version Path
comfortable_mexican_sofa-2.0.19 lib/comfortable_mexican_sofa/content/tags/fragment.rb
PixelForce_ETS-0.0.2 lib/comfortable_mexican_sofa/content/tags/fragment.rb
PixelForce_ETS-0.0.1 lib/comfortable_mexican_sofa/content/tags/fragment.rb
kcyEtsTest2-0.0.4 lib/comfortable_mexican_sofa/content/tags/fragment.rb
kcyEtsTest2-0.0.3 lib/comfortable_mexican_sofa/content/tags/fragment.rb
kcyEtsTest2-0.0.2 lib/comfortable_mexican_sofa/content/tags/fragment.rb
kcyEtsTest2-0.0.1 lib/comfortable_mexican_sofa/content/tags/fragment.rb
kcyEtsTest-0.0.1 lib/comfortable_mexican_sofa/content/tags/fragment.rb
kcyEtsTest-2.2.19 lib/comfortable_mexican_sofa/content/tags/fragment.rb
ComfortableMexicanSofa-2.0.18 lib/comfortable_mexican_sofa/content/tags/fragment.rb
kcy1-1.0 lib/comfortable_mexican_sofa/content/tags/fragment.rb
comfortable_mexican_sofa-2.0.18 lib/comfortable_mexican_sofa/content/tags/fragment.rb
comfortable_mexican_sofa-2.0.17 lib/comfortable_mexican_sofa/content/tags/fragment.rb
comfortable_mexican_sofa-2.0.16 lib/comfortable_mexican_sofa/content/tags/fragment.rb
comfortable_mexican_sofa-2.0.15 lib/comfortable_mexican_sofa/content/tags/fragment.rb