class CckForms::ParameterTypeClass::DateRange

Represents a pair of date SELECTs (date range).

Public Instance Methods

build_form(form_builder, options) click to toggle source

2 date SELECTs

# File lib/cck_forms/parameter_type_class/date_range.rb, line 24
def build_form(form_builder, options)
  result = []
  set_value_in_hash options

  [:from, :till].each do |type|
    result << CckForms::ParameterTypeClass::Date.build_date_form(form_builder, options, type)
  end

  result.join.html_safe
end
mongoize() click to toggle source

Converts input hash of type {from: …, till: …} to MongoDB format

# File lib/cck_forms/parameter_type_class/date_range.rb, line 7
def mongoize
  value_from_form = value
  return nil if value_from_form.blank?
  db_representation = {}

  %w(from till).each do |type|
   type_hash = {}
    %w((1i) (2i) (3i)).each do |field|
      type_hash.merge!("#{field}" => value_from_form.try(:[], "#{type + field}"))
    end
   db_representation[type] =  CckForms::ParameterTypeClass::Time::date_object_from_what_stored_in_database(type_hash)
  end

  db_representation
end
to_s() click to toggle source

“from 21.12.2012” “till 22.12.2012” “21.12.2012 - 22.12.2012”

# File lib/cck_forms/parameter_type_class/date_range.rb, line 38
def to_s
  return '' unless value.present? && value.is_a?(Hash)
  types = {}
  [:from, :till].each { |type| types[type] = value[type].strftime('%d.%m.%Y') if value[type].is_a?(Time) }
  from, till = types[:from], types[:till]

  if from.blank?
    [I18n.t('cck_forms.date_range.till'), till].join(' ')
  elsif till.blank?
    [I18n.t('cck_forms.date_range.from'), from].join(' ')
  elsif from == till
    from.to_s
  else
    [from, till].join(' - ')
  end

end