Sha256: 24b6cf0072042b5a928d418779e87710f6ff71d04e6bdb5c446bf79b529a36de

Contents?: true

Size: 1.56 KB

Versions: 7

Compression:

Stored size: 1.56 KB

Contents

require "simple_form"

# Create an input-text-field with a date-time-picker widget attached. Also makes sure the given date-time-value is being put in the correct format.
class BbDatePickerInput < SimpleForm::Inputs::Base
  # The method that should return the widgets HTML based on the sat arguments.
  def input(_wrapper_options)
    # Parse the value.
    if input_html_options[:value]
      date = input_html_options[:value]
    elsif @builder.object.present?
      date = @builder.object.send(attribute_name)
    else
      date = nil
    end

    if date.present?
      val = date.strftime("%Y-%m-%d")

      year = date.year
      month = date.month
      day = date.day
    end

    # Parse classes.
    classes = ["form-control"]

    class_arg = input_html_options[:class]

    if class_arg
      if class_arg.is_a?(Array)
        classes += class_arg
      else
        classes << class_arg
      end
    end

    # Generate and return HTML for the widget.
    content_tag = template.content_tag(:div, class: "bb-date-picker") do
      html = ""
      html << @builder.hidden_field("#{attribute_name}(1i)", value: year, class: "bb-date-picker-input-year")
      html << @builder.hidden_field("#{attribute_name}(2i)", value: month, class: "bb-date-picker-input-month")
      html << @builder.hidden_field("#{attribute_name}(3i)", value: day, class: "bb-date-picker-input-day")

      html << template.text_field_tag("", val, class: classes, data: {date_format: "yyyy-mm-dd", provide: "datepicker"})

      html.html_safe # rubocop:disable Rails/OutputSafety
    end

    content_tag
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
bootstrap_builders-1.0.4 lib/bb_date_picker_input.rb
bootstrap_builders-0.0.63 lib/bb_date_picker_input.rb
bootstrap_builders-1.0.3 lib/bb_date_picker_input.rb
bootstrap_builders-1.0.2 lib/bb_date_picker_input.rb
bootstrap_builders-1.0.1 lib/bb_date_picker_input.rb
bootstrap_builders-1.0.0 lib/bb_date_picker_input.rb
bootstrap_builders-0.0.62 lib/bb_date_picker_input.rb