Class: Occams::Content::Tag::Datetime

Inherits:
Fragment show all
Defined in:
lib/occams/content/tags/datetime.rb

Overview

Tag for text content that is going to be rendered using text input with datetime widget

{{ cms:datetime identifier, strftime: "at %I:%M%p" }}

‘strftime` - Format datetime string during rendering

Direct Known Subclasses

Date

Instance Attribute Summary collapse

Attributes inherited from Fragment

#identifier, #namespace, #options, #renderable

Attributes inherited from Occams::Content::Tag

#context, #params, #source

Instance Method Summary collapse

Methods inherited from Fragment

#form_field_id, #fragment

Methods inherited from Occams::Content::Tag

#allow_erb?, #nodes

Constructor Details

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

Returns a new instance of Datetime.



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

def initialize(context:, params: [], source: nil)
  super
  @strftime = options['strftime']
end

Instance Attribute Details

#strftimeObject (readonly)

Returns the value of attribute strftime.



9
10
11
# File 'lib/occams/content/tags/datetime.rb', line 9

def strftime
  @strftime
end

Instance Method Details

#contentObject



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

def content
  fragment.datetime
end

#form_field(object_name, view, index) {|input| ... } ⇒ Object

Yields:

  • (input)


30
31
32
33
34
35
36
37
# File 'lib/occams/content/tags/datetime.rb', line 30

def form_field(object_name, view, index)
  name    = "#{object_name}[fragments_attributes][#{index}][datetime]"
  options = { id: form_field_id, class: 'form-control', data: { 'cms-datetime' => true } }
  value   = content.present? ? content.to_s(:db) : ''
  input   = view.send(:text_field_tag, name, value, options)

  yield input
end

#renderObject



20
21
22
23
24
25
26
27
28
# File 'lib/occams/content/tags/datetime.rb', line 20

def render
  return '' unless renderable

  if strftime.present?
    content.strftime(strftime)
  else
    content.to_s
  end
end