class CckForms::ParameterTypeClass::WorkHours::WorkHoursDay

A utility model for one work day.

day - one of CckForms::ParameterTypeClass::WorkHours::DAYS. open_time & close_time are hashes: {hours: 10, minutes: 5}

Attributes

close_time[RW]
day[RW]
open_24_hours[RW]
open_time[RW]
open_until_last_client[RW]

Public Class Methods

demongoize(object) click to toggle source
# File lib/cck_forms/parameter_type_class/work_hours.rb, line 312
def demongoize(object)
  object = object.symbolize_keys
  WorkHoursDay.new(
      day: object[:day].to_s,
      open_time: self.demongoize_time(object[:open_time]),
      close_time: self.demongoize_time(object[:close_time]),
      open_24_hours: object[:open_24_hours],
      open_until_last_client: object[:open_until_last_client],
  )
end
demongoize_time(time) click to toggle source
# File lib/cck_forms/parameter_type_class/work_hours.rb, line 346
def demongoize_time(time)
  mongoize_time(time)
end
evolve(object) click to toggle source

TODO: make evolve

# File lib/cck_forms/parameter_type_class/work_hours.rb, line 332
def evolve(object)
  object
end
mongoize(object) click to toggle source
# File lib/cck_forms/parameter_type_class/work_hours.rb, line 323
def mongoize(object)
  case object
    when WorkHoursDay then object.mongoize
    when Hash then WorkHoursDay.new(object).mongoize
    else object
  end
end
mongoize_time(time) click to toggle source

Time/DateTime -> {hours: 10, minutes: 5}

# File lib/cck_forms/parameter_type_class/work_hours.rb, line 337
def mongoize_time(time)
  if time.is_a? Time or time.is_a? DateTime
    {'hours' => time.hour, 'minutes' => time.min}
  elsif time.is_a? Hash
    time = time.stringify_keys
    {'hours' => time['hours'].present? ? time['hours'].to_i : nil, 'minutes' => time['minutes'].present? ? time['minutes'].to_i : nil}
  end
end
new(other) click to toggle source
# File lib/cck_forms/parameter_type_class/work_hours.rb, line 151
def initialize(other)
  if other.is_a? Hash
    other = other.symbolize_keys
    @day = other[:day]
    @open_time = other[:open_time]
    @close_time = other[:close_time]
    @open_24_hours = form_to_boolean(other[:open_24_hours])
    @open_until_last_client = form_to_boolean(other[:open_until_last_client])
  elsif other.is_a? WorkHoursDay
    @day = other.day
    @open_time = other.open_time
    @close_time = other.close_time
    @open_24_hours = other.open_24_hours
    @open_until_last_client = other.open_until_last_client
  end
end

Public Instance Methods

==(other) click to toggle source

Are equal if all fields are equal

# File lib/cck_forms/parameter_type_class/work_hours.rb, line 169
def ==(other)
  other = self.class.new(other) unless other.is_a? self.class

  self.day == other.day and
      self.open_time == other.open_time and
      self.close_time == other.close_time and
      self.open_24_hours == other.open_24_hours and
      self.open_until_last_client == other.open_until_last_client
end
build_form(form_builder, template = false, options = {}) click to toggle source

Single day form HTML

# File lib/cck_forms/parameter_type_class/work_hours.rb, line 209
    def build_form(form_builder, template = false, options = {})
      form_builder.object = self

      open_time_form = form_builder.fields_for(:open_time) { |time_form| build_time_form(time_form, open_time) }
      close_time_form = form_builder.fields_for(:close_time) { |time_form| build_time_form(time_form, close_time) }

      input_multi_mark = if options[:multi_days]
                           "data-multi-days='true'"
                         end

      if template
        header = ['<ul class="nav nav-pills">']
        CckForms::ParameterTypeClass::WorkHours::DAYS.each { |day| header << '<li><a href="javascript:void(0)"><input name="' + form_builder.object_name + '[days]" type="checkbox" value="' + day + '" /> ' + CckForms::ParameterTypeClass::WorkHours.day_to_short(day) + '</a></li>' }
        header = header.push('</ul>').join
      else
        header = sprintf '<strong>%s</strong>:%s', CckForms::ParameterTypeClass::WorkHours::day_to_short(day), form_builder.hidden_field(:day)
      end

      open_until_last_client_html = unless options[:hide_open_until_last_client]
                                      <<HTML
                    <div class="checkbox">
                      <label class="form_work_hours_option">#{form_builder.check_box :open_until_last_client} <nobr>#{I18n.t 'cck_forms.work_hours.until_last_client'}</nobr></label>
                    </div>
HTML
                                    end

      <<HTML
        <div #{input_multi_mark} class="form_work_hours_day#{template ? ' form_work_hours_day_template" style="display: none' : ''}">
          <div class="form_work_hours_time">
            #{header}
          </div>
          <div class="form_work_hours_time">
            <table width="100%">
              <tr>
                <td width="60%" class="form-inline">
                  #{I18n.t 'cck_forms.work_hours.time_from'} #{open_time_form}
                  #{I18n.t 'cck_forms.work_hours.time_till'} #{close_time_form}
                </td>
                <td width="40%">
                    <div class="checkbox">
                      <label class="form_work_hours_option">#{form_builder.check_box :open_24_hours} #{I18n.t 'cck_forms.work_hours.24h'}</label>
                    </div>
                    #{open_until_last_client_html}
                </td>
              </tr>
            </table>
          </div>
        </div>
HTML
    end
hash_without_day() click to toggle source

Hash key for grouping (all fields except for day)

# File lib/cck_forms/parameter_type_class/work_hours.rb, line 180
def hash_without_day
  sprintf('%s:%s:%s:%s', open_time, close_time, open_24_hours, open_until_last_client).hash
end
mongoize() click to toggle source
# File lib/cck_forms/parameter_type_class/work_hours.rb, line 300
def mongoize
  {
      'day' => day.to_s,
      'open_time' => self.class.mongoize_time(open_time),
      'close_time' => self.class.mongoize_time(close_time),
      'open_24_hours' => open_24_hours,
      'open_until_last_client' => open_until_last_client,
  }
end
to_s_without_day() click to toggle source

“from 12:00 till last client”

# File lib/cck_forms/parameter_type_class/work_hours.rb, line 185
def to_s_without_day
  result = ''
  if open_24_hours
    return I18n.t 'cck_forms.work_hours.24h'
  elsif time_present?(open_time) or time_present?(close_time)
    ots, cts = time_to_s(open_time), time_to_s(close_time)
    if ots and cts
      result = sprintf('%s–%s', ots, cts)
    elsif ots
      result = sprintf("#{I18n.t 'cck_forms.work_hours.from'} %s", ots)
    else
      result = sprintf("#{I18n.t 'cck_forms.work_hours.till'} %s", cts)
    end
  end

  if open_until_last_client
    result += ' ' if result.present?
    result += I18n.t 'cck_forms.work_hours.until_last_client'
  end

  result
end

Private Instance Methods

build_time_form(form_builder, value) click to toggle source

SELECTs: [18]:

# File lib/cck_forms/parameter_type_class/work_hours.rb, line 282
def build_time_form(form_builder, value)
  hours = []
  24.times { |hour| hours << [hour.to_s.rjust(2, '0'), hour] }

  minutes = []
  (60/5).times { |minute| minutes << [(minute *= 5).to_s.rjust(2, '0'), minute] }

  sprintf(
      '%s : %s',
      form_builder.select(:hours, hours, {include_blank: true, selected: value.try(:[], 'hours')}, class: 'form-control input-sm', style: 'width: 60px'),
      form_builder.select(:minutes, minutes, {include_blank: true, selected: value.try(:[], 'minutes')}, class: 'form-control input-sm', style: 'width: 60px')
  ).html_safe
end
form_to_boolean(value) click to toggle source

HTML form to boolean: '1' -> true

# File lib/cck_forms/parameter_type_class/work_hours.rb, line 265
def form_to_boolean(value)
  return value == '1' if value.is_a? String
  !!value
end
time_present?(time) click to toggle source

Time present?

# File lib/cck_forms/parameter_type_class/work_hours.rb, line 277
def time_present?(time)
  return time.is_a?(Hash) && time['hours'].present? && time['minutes'].present?
end
time_to_s(time) click to toggle source

{hours: …, minutes: …} -> “10:42”

# File lib/cck_forms/parameter_type_class/work_hours.rb, line 271
def time_to_s(time)
  return nil unless time.is_a?(Hash) and time['hours'].present? and time['minutes'].present?
  sprintf '%s:%s', time['hours'].to_s.rjust(2, '0'), time['minutes'].to_s.rjust(2, '0')
end