Sha256: 2f16d9a78610a77320cd210f666708e058e15c6c0171dcbcf9783771b1318a0c

Contents?: true

Size: 1.77 KB

Versions: 2

Compression:

Stored size: 1.77 KB

Contents

# frozen_string_literal: true
class DateRangeCell < FormCellBase
  attribute :form, mandatory: true, description: 'A form object.'
  attribute :name, mandatory: true, description: 'The name attribute.'
  attribute :label, description: 'A label.'
  attribute :ranges, description: 'A hash of selectable ranges of the form ' \
                                  '`{ label => [from_date, to_date] }`'
  attribute :date_limit, description: 'The maximum span between the selected ' \
    'start and end dates. Can have any property you can add to a ' \
    '[moment](http://momentjs.com/docs/#/durations/creating/) object (i.e. ' \
    'days, months)'
  attribute :start_date, description: 'Default start date'
  attribute :end_date, description: 'Default end date'
  attribute :opens, description: 'Direction in which the dropdown opens. ' \
    "Accepts: 'right' (default), 'left', 'center'"
  attribute :submit_on_change, description: 'Whether the enclosing form should be ' \
    'automatically submitted on value change'

  def show
    [
      date_field(:from),
      date_field(:to),
      select_div
    ].join
  end

  private

  def date_field(type)
    options[:form].hidden_field(
      "#{options[:name]}_#{type}",
      id: "#{id}_#{type}",
      skip_label: true,
      style: 'display: none'
    )
  end

  def select_div
    options[:form].form_group(:daterange, label: label) do
      content_tag(:div, '', class: 'ui-components-date-range form-control', data: data)
    end
  end

  def data
    options.slice(:ranges, :date_limit, :opens, :submit_on_change).merge(
      start_date: start_date.to_s, end_date: end_date.to_s,
      start: "##{id}_from", end: "##{id}_to"
    )
  end

  def id
    "date_range_#{name_param}"
  end

  def label
    { text: options[:label] } if options[:label]
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ad2games-ui_components-2.3.0 app/cells/date_range/date_range_cell.rb
ad2games-ui_components-2.1.0 app/cells/date_range/date_range_cell.rb