class CckForms::ParameterTypeClass::WorkHours

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

Constants

DAYS

Public Class Methods

day_to_short(day) click to toggle source

mon -> Mon

# 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) click to toggle source

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

# 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) click to toggle source

%w{mon, tue, wed, sat} -> “Mon—Wed, Sat”

# 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

Public Instance Methods

build_form(form_builder, options) click to toggle source

Builds HTML form. 1 row — 1 day

# 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
mongoize() click to toggle source

mon: {open_time: …, open_24_hours: …}, tue: {…}, … -> MongoDB Hash

# 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) click to toggle source

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

# 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) click to toggle source
# File lib/cck_forms/parameter_type_class/work_hours.rb, line 108
def to_s(_options = nil)
  to_html with_tags: false
end