Sha256: 483842d7889419e8fdab9f815060edacfd30d4c7cf6d7168b2e0ae530e7d9072

Contents?: true

Size: 1.87 KB

Versions: 18

Compression:

Stored size: 1.87 KB

Contents

# frozen_string_literal: true

class DatePickerInput < SimpleForm::Inputs::StringInput
  # Note that where we use `wrapper: :clockpicker` or `wrapper: horizontal_clockpicker`,
  # #input is not called and we call #prefix_column and #input_column directly.
  def input(_wrapper_options)
    template.tag.div(class: "row collapse #{class_name}-wrapper") do
      template.concat prefix_column
      template.concat input_column
    end
  end

  def prefix_column(_wrapper_options = {})
    template.tag.div(class: "small-2 columns") do
      template.concat icon_calendar
    end
  end

  def input_column(_wrapper_options = {})
    html_options = input_html_options
    html_options[:class] ||= []
    html_options[:class] << class_name
    template.tag.div(class: "small-10 columns") do
      datestamp = @builder.object.public_send(attribute_name)
      value = format_date(datestamp)
      template.concat @builder.text_field(attribute_name, html_options.merge(value: value))
    end
  end

  def icon_calendar
    "<span class='prefix'><i class='far fa-calendar'></i></span>".html_safe
  end

  def input_type
    :string
  end

  def class_name
    "datepicker"
  end

  private

  # Note that datestamp will be some kind of Date object *unless* its an invalid date string
  # e.g. "12-00-2010" that has for example been imported from another system, and its one we
  # happen to store in Rw2 in jsonb where there is no database type checking.
  # We have seen this in LowClerance::Profile.document.
  # So if we get an error converting an invalid date string, return it as is and let the
  # user correct it in the UI. Note that without this error handling, the user will
  # not be able to view the form to correct the data.
  def format_date(datestamp)
    return "" if datestamp.blank?

    I18n.l(datestamp)
  rescue I18n::ArgumentError # e.g. its a string containing an invalid date
    datestamp
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
renalware-core-2.1.1 app/inputs/date_picker_input.rb
renalware-core-2.1.0 app/inputs/date_picker_input.rb
renalware-core-2.0.167 app/inputs/date_picker_input.rb
renalware-core-2.0.166 app/inputs/date_picker_input.rb
renalware-core-2.0.165 app/inputs/date_picker_input.rb
renalware-core-2.0.164 app/inputs/date_picker_input.rb
renalware-core-2.0.163 app/inputs/date_picker_input.rb
renalware-core-2.0.162 app/inputs/date_picker_input.rb
renalware-core-2.0.161 app/inputs/date_picker_input.rb
renalware-core-2.0.160 app/inputs/date_picker_input.rb
renalware-core-2.0.159 app/inputs/date_picker_input.rb
renalware-core-2.0.158 app/inputs/date_picker_input.rb
renalware-core-2.0.157 app/inputs/date_picker_input.rb
renalware-core-2.0.156 app/inputs/date_picker_input.rb
renalware-core-2.0.155 app/inputs/date_picker_input.rb
renalware-core-2.0.153 app/inputs/date_picker_input.rb
renalware-core-2.0.152 app/inputs/date_picker_input.rb
renalware-core-2.0.151 app/inputs/date_picker_input.rb