Class: CckForms::ParameterTypeClass::WorkHours::WorkHoursDay

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

Overview

A utility model for one work day.

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(other) ⇒ WorkHoursDay

Returns a new instance of WorkHoursDay.



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# 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

Instance Attribute Details

#close_timeObject

Returns the value of attribute close_time.



149
150
151
# File 'lib/cck_forms/parameter_type_class/work_hours.rb', line 149

def close_time
  @close_time
end

#dayObject

Returns the value of attribute day.



149
150
151
# File 'lib/cck_forms/parameter_type_class/work_hours.rb', line 149

def day
  @day
end

#open_24_hoursObject

Returns the value of attribute open_24_hours.



149
150
151
# File 'lib/cck_forms/parameter_type_class/work_hours.rb', line 149

def open_24_hours
  @open_24_hours
end

#open_timeObject

Returns the value of attribute open_time.



149
150
151
# File 'lib/cck_forms/parameter_type_class/work_hours.rb', line 149

def open_time
  @open_time
end

#open_until_last_clientObject

Returns the value of attribute open_until_last_client.



149
150
151
# File 'lib/cck_forms/parameter_type_class/work_hours.rb', line 149

def open_until_last_client
  @open_until_last_client
end

Class Method Details

.demongoize(object) ⇒ Object



312
313
314
315
316
317
318
319
320
321
# 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) ⇒ Object



346
347
348
# File 'lib/cck_forms/parameter_type_class/work_hours.rb', line 346

def demongoize_time(time)
  mongoize_time(time)
end

.evolve(object) ⇒ Object

TODO: make evolve



332
333
334
# File 'lib/cck_forms/parameter_type_class/work_hours.rb', line 332

def evolve(object)
  object
end

.mongoize(object) ⇒ Object



323
324
325
326
327
328
329
# 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) ⇒ Object

Time/DateTime -> 10, minutes: 5



337
338
339
340
341
342
343
344
# 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

Instance Method Details

#==(other) ⇒ Object

Are equal if all fields are equal



169
170
171
172
173
174
175
176
177
# 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 = {}) ⇒ Object

Single day form HTML



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# 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 class="nav-item"><a class="nav-link" 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_dayObject

Hash key for grouping (all fields except for day)



180
181
182
# 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

#mongoizeObject



300
301
302
303
304
305
306
307
308
# 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_dayObject

“from 12:00 till last client”



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# 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