Class: ComfortableMexicanSofa::Content::Tag::Datetime

Inherits:
Fragment show all
Defined in:
lib/comfortable_mexican_sofa/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 ComfortableMexicanSofa::Content::Tag

#context, #params, #source

Instance Method Summary collapse

Methods inherited from Fragment

#form_field_id, #fragment

Methods inherited from ComfortableMexicanSofa::Content::Tag

#allow_erb?, #nodes

Constructor Details

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

Returns a new instance of Datetime.



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

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

Instance Attribute Details

#strftimeObject (readonly)

Returns the value of attribute strftime.



10
11
12
# File 'lib/comfortable_mexican_sofa/content/tags/datetime.rb', line 10

def strftime
  @strftime
end

Instance Method Details

#contentObject



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

def content
  fragment.datetime
end

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

Yields:

  • (input)


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

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



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

def render
  return "" unless renderable

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