Class: CckForms::ParameterTypeClass::WorkHours

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

Overview

Represents a set of weekly based work hours: essentially a set of SELECTs one for each week day.

Defined Under Namespace

Classes: WorkHoursDay

Constant Summary collapse

DAYS =
%w{ mon tue wed thu fri sat sun }

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.day_to_short(day) ⇒ Object

mon -> Mon



9
10
11
# File 'lib/cck_forms/parameter_type_class/work_hours.rb', line 9

def self.day_to_short(day)
  I18n.t "cck_forms.work_hours.day_short.#{day}"
end

.demongoize_value(value, parameter_type_class = nil) ⇒ Object

Makes a Hash of WorkHoursDay (key - day name of form :mon, see DAYS)



24
25
26
27
28
29
30
31
# File 'lib/cck_forms/parameter_type_class/work_hours.rb', line 24

def self.demongoize_value(value, parameter_type_class=nil)
  return {} unless value.is_a? Hash
  value.reduce({}) do |r, (day_name, day_row)|
    day_row = CckForms::ParameterTypeClass::WorkHours::WorkHoursDay.demongoize(day_row.merge(day: day_name)) if day_row.is_a? Hash
    r[day_name] = day_row
    r
  end
end

.grouped_days_string(days) ⇒ Object

%wtue, wed, sat -> “Mon—Wed, Sat”



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/cck_forms/parameter_type_class/work_hours.rb', line 113

def self.grouped_days_string(days)

  # split by continuous blocks: [%w{mon tue wed}, %w{sat}]
  days.sort! { |a, b| DAYS.index(a) <=> DAYS.index(b) }
  prev_index = -2
  groups = []
  days.each do |day|
    index = DAYS.index(day)
    if prev_index + 1 != index
      groups << []
    end

    groups.last << day_to_short(day)
    prev_index = index
  end

  # convert to string and join
  groups.map do |group|
    if group.length == 1
      group[0]
    elsif group.length == 2
      group.join ', '
    else
      sprintf '%s–%s', group.first, group.last
    end
  end.join ', '
end

Instance Method Details

#build_form(form_builder, options) ⇒ Object

Builds HTML form. 1 row — 1 day



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/cck_forms/parameter_type_class/work_hours.rb', line 34

def build_form(form_builder, options)
  set_value_in_hash options

  options = {
      value: {}
  }.merge options

  value = options[:value]
  value = {} unless value.is_a? Hash

  result = []
  met_days = Hash[ CckForms::ParameterTypeClass::WorkHours::DAYS.zip(CckForms::ParameterTypeClass::WorkHours::DAYS.dup.fill(false)) ]

  value.each_value do |day|
    day = CckForms::ParameterTypeClass::WorkHours::WorkHoursDay.new day unless day.is_a? CckForms::ParameterTypeClass::WorkHours::WorkHoursDay
    form_builder.fields_for(:value, index: day.day) { |day_builder| result << day.build_form(day_builder, false, options) }
    met_days[day.day] = true
  end

  met_days.reject! { |_, value| value }

  met_days.keys.each do |day_name|
    form_builder.fields_for(:value, index: day_name) { |day_builder| result << CckForms::ParameterTypeClass::WorkHours::WorkHoursDay.new(day: day_name, open_24_hours: true).build_form(day_builder) }
  end

  form_builder.fields_for(:template) do |day_builder|
    result << CckForms::ParameterTypeClass::WorkHours::WorkHoursDay.new({}).build_form(day_builder, true, options)
  end

  sprintf '<div class="work-hours" id="%1$s">%2$s</div><script type="text/javascript">$(function() {$("#%1$s").workhours()})</script>', form_builder_name_to_id(form_builder), result.join
end

#mongoizeObject

mon: …, open_24_hours: …, tue: …, … -> MongoDB Hash



14
15
16
17
18
19
20
21
# File 'lib/cck_forms/parameter_type_class/work_hours.rb', line 14

def mongoize
  return {} unless value.is_a? Hash

  value.reduce({}) do |r, (day_name, day_data)|
    r[day_name] = CckForms::ParameterTypeClass::WorkHours::WorkHoursDay.new(day_data).mongoize if day_name.in? CckForms::ParameterTypeClass::WorkHours::DAYS
    r
  end
end

#to_html(options = nil) ⇒ Object

Makes a string: “Mon—Wed 10:00—23:00; Thu-Sat 24h”



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/cck_forms/parameter_type_class/work_hours.rb', line 67

def to_html(options = nil)
  value = self.value
  return value.to_s unless value.respond_to? :each

  with_tags = options && !options.try(:[], :with_tags).nil? ? options[:with_tags] : true

  value = value.deep_stringify_keys if value.respond_to? :deep_stringify_keys

  # split by groups with equal value: {'25h' => %w{mon tue wed}, ...}
  groups = {}
  value.send(value.respond_to?(:each_value) ? :each_value : :each) do |day|
    day = CckForms::ParameterTypeClass::WorkHours::WorkHoursDay.new(day) unless day.is_a? CckForms::ParameterTypeClass::WorkHours::WorkHoursDay
    hash = day.to_s_without_day
    groups[hash] = [] unless groups[hash]
    groups[hash] << day.day
  end

  # make string for each group
  result = []
  groups.each_pair do |hours_description, days|
    if hours_description.present?
      if days.length == 7
        template = with_tags ? %Q{<span class="workhours-group">%s, <span class="workhours-group-novac">#{I18n.t 'cck_forms.work_hours.no_vacations'}</span></span>} : "%s, #{I18n.t 'cck_forms.work_hours.no_vacations'}"
        result << sprintf(template, hours_description)
      else
        if days == %w{ mon tue wed thu fri }
          days_description = I18n.t 'cck_forms.work_hours.work_days'
        elsif days == %w{ sat sun }
          days_description = I18n.t 'cck_forms.work_hours.sat_sun'
        else
          days_description = CckForms::ParameterTypeClass::WorkHours.grouped_days_string(days).mb_chars.downcase
        end
        template = with_tags ? '<span class="workhours-group">%s <span class="workhours-group-days">(%s)</span></span>' : '%s (%s)'
        result << sprintf(template, hours_description, days_description)
      end
    end
  end

  result.join('; ').html_safe
end

#to_s(_options = nil) ⇒ Object



108
109
110
# File 'lib/cck_forms/parameter_type_class/work_hours.rb', line 108

def to_s(_options = nil)
  to_html with_tags: false
end