Sha256: d1038e5a372f0b7bc55a2c4c6b589f454dc4f5bdc352924837536393ee2cbfb1

Contents?: true

Size: 1.04 KB

Versions: 10

Compression:

Stored size: 1.04 KB

Contents

# frozen_string_literal: true

# 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
#
class Occams::Content::Tag::Datetime < Occams::Content::Tag::Fragment
  attr_reader :strftime

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

  def content
    fragment.datetime
  end

  def render
    return '' unless renderable

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

  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
end

Occams::Content::Renderer.register_tag(
  :datetime, Occams::Content::Tag::Datetime
)

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
occams-1.0.7.3 lib/occams/content/tags/datetime.rb
occams-1.0.7.2 lib/occams/content/tags/datetime.rb
occams-1.0.7.1 lib/occams/content/tags/datetime.rb
occams-1.0.7 lib/occams/content/tags/datetime.rb
occams-1.0.6.1 lib/occams/content/tags/datetime.rb
occams-1.0.6 lib/occams/content/tags/datetime.rb
occams-1.0.5 lib/occams/content/tags/datetime.rb
occams-1.0.4 lib/occams/content/tags/datetime.rb
occams-1.0.3 lib/occams/content/tags/datetime.rb
occams-1.0.2 lib/occams/content/tags/datetime.rb