Class: CckForms::ParameterTypeClass::DateRange

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/cck_forms/parameter_type_class/date_range.rb

Overview

Represents a pair of date SELECTs (date range).

Instance Method Summary collapse

Instance Method Details

#build_form(form_builder, options) ⇒ Object

2 date SELECTs



24
25
26
27
28
29
30
31
32
33
# 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

#mongoizeObject

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



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 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_sObject

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



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# 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