Class: Occams::Content::Tag::Fragment

Inherits:
Occams::Content::Tag show all
Defined in:
lib/occams/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 Occams::Content::Tag

#context, #params, #source

Instance Method Summary collapse

Methods inherited from Occams::Content::Tag

#allow_erb?, #nodes

Constructor Details

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

Returns a new instance of Fragment.



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

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.



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

def identifier
  @identifier
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



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

def namespace
  @namespace
end

#optionsObject (readonly)

Returns the value of attribute options.



16
17
18
# File 'lib/occams/content/tags/fragment.rb', line 16

def options
  @options
end

#renderableObject

Returns the value of attribute renderable.



12
13
14
# File 'lib/occams/content/tags/fragment.rb', line 12

def renderable
  @renderable
end

Instance Method Details

#contentObject



39
40
41
# File 'lib/occams/content/tags/fragment.rb', line 39

def content
  fragment.content
end

#form_fieldObject

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


57
58
59
# File 'lib/occams/content/tags/fragment.rb', line 57

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

#form_field_idObject



61
62
63
# File 'lib/occams/content/tags/fragment.rb', line 61

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

#fragmentOccams::Cms::Fragment

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



34
35
36
37
# File 'lib/occams/content/tags/fragment.rb', line 34

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



45
46
47
# File 'lib/occams/content/tags/fragment.rb', line 45

def render
  renderable ? content : ''
end