Class: ComfortableMexicanSofa::Content::Tag::Fragment

Inherits:
ComfortableMexicanSofa::Content::Tag show all
Defined in:
lib/comfortable_mexican_sofa/content/tags/fragment.rb

Overview

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

Direct Known Subclasses

Checkbox, Datetime, File, Markdown, Number, Text, TextArea, Wysiwyg

Instance Attribute Summary collapse

Attributes inherited from ComfortableMexicanSofa::Content::Tag

#context, #params, #source

Instance Method Summary collapse

Methods inherited from ComfortableMexicanSofa::Content::Tag

#allow_erb?, #nodes

Constructor Details

#initialize(context:, params: [], source: nil) ⇒ Fragment

Returns a new instance of Fragment.



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/comfortable_mexican_sofa/content/tags/fragment.rb', line 19

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

Instance Attribute Details

#identifierObject (readonly)

Returns the value of attribute identifier.



14
15
16
# File 'lib/comfortable_mexican_sofa/content/tags/fragment.rb', line 14

def identifier
  @identifier
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



14
15
16
# File 'lib/comfortable_mexican_sofa/content/tags/fragment.rb', line 14

def namespace
  @namespace
end

#optionsObject (readonly)

Returns the value of attribute options.



17
18
19
# File 'lib/comfortable_mexican_sofa/content/tags/fragment.rb', line 17

def options
  @options
end

#renderableObject

Returns the value of attribute renderable.



13
14
15
# File 'lib/comfortable_mexican_sofa/content/tags/fragment.rb', line 13

def renderable
  @renderable
end

Instance Method Details

#contentObject



40
41
42
# File 'lib/comfortable_mexican_sofa/content/tags/fragment.rb', line 40

def content
  fragment.content
end

#form_fieldObject

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


58
59
60
# File 'lib/comfortable_mexican_sofa/content/tags/fragment.rb', line 58

def form_field
  raise "Form field rendering not implemented for this Tag"
end

#form_field_idObject



62
63
64
# File 'lib/comfortable_mexican_sofa/content/tags/fragment.rb', line 62

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

#fragmentComfy::Cms::Fragment

Grabs existing fragment record or spins up a new instance if there’s none



35
36
37
38
# File 'lib/comfortable_mexican_sofa/content/tags/fragment.rb', line 35

def fragment
  context.fragments.detect { |f| f.identifier == identifier } ||
    context.fragments.build(identifier: identifier)
end

#renderObject

If ‘render: false` was passed in we won’t render anything. Assuming that that fragment content will be rendered manually



46
47
48
# File 'lib/comfortable_mexican_sofa/content/tags/fragment.rb', line 46

def render
  renderable ? content : ""
end