From 50546fa2d4c28d2ca711f2e716039d8a94d917f7 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Mon, 21 Sep 2015 00:19:45 +0200 Subject: [PATCH] Added business hours tests. --- .../_application_controller_form.js.coffee | 28 +- .../_ui_element/business_hours.js.coffee | 84 +++--- .../_ui_element/ticket_selector.js.coffee | 24 +- .../app/lib/app_post/business_hours.js.coffee | 4 +- .../app/views/generic/business_hours.jst.eco | 6 +- public/assets/tests/form-extended.js | 245 ++++++++++++++++-- public/assets/tests/form.js | 89 +++++-- 7 files changed, 377 insertions(+), 103 deletions(-) diff --git a/app/assets/javascripts/app/controllers/_application_controller_form.js.coffee b/app/assets/javascripts/app/controllers/_application_controller_form.js.coffee index 814710ae0..51da9b8fc 100644 --- a/app/assets/javascripts/app/controllers/_application_controller_form.js.coffee +++ b/app/assets/javascripts/app/controllers/_application_controller_form.js.coffee @@ -500,7 +500,7 @@ class App.ControllerForm extends App.Controller time = new Date( Date.parse( param[key] ) ) if time is 'Invalid Datetime' throw "Invalid Datetime #{param[key]}" - param[newKey] = time.toISOString().replace(/:\d\d\.\d\d\dZ$/, ':00Z') + param[newKey] = time.toISOString().replace(/:\d\d\.\d\d\dZ$/, ':00.000Z') catch err param[newKey] = "invalid #{param[key]}" console.log('ERR', err) @@ -527,6 +527,32 @@ class App.ControllerForm extends App.Controller for key of inputSelectObject param[ key ] = inputSelectObject[ key ] + # data type conversion + for key of param + + # get {business_hours} + if key.substr(0,16) is '{business_hours}' + newKey = key.substr(16, key.length) + if lookupForm.find("[data-name=\"#{newKey}\"]").hasClass('is-hidden') + param[newKey] = null + else if param[key] + newParams = {} + for day, value of param[key] + newParams[day] = {} + newParams[day].active = false + if value.active is 'true' + newParams[day].active = true + newParams[day].timeframes = [] + if _.isArray(value.start) + for pos of value.start + newParams[day].timeframes.push [ value.start[pos], value.end[pos] ] + else + newParams[day].timeframes.push [ value.start, value.end ] + param[newKey] = newParams + else + param[newKey] = undefined + delete param[key] + #App.Log.notice 'ControllerForm', 'formParam', form, param param diff --git a/app/assets/javascripts/app/controllers/_ui_element/business_hours.js.coffee b/app/assets/javascripts/app/controllers/_ui_element/business_hours.js.coffee index d14e42831..81b5fa95c 100644 --- a/app/assets/javascripts/app/controllers/_ui_element/business_hours.js.coffee +++ b/app/assets/javascripts/app/controllers/_ui_element/business_hours.js.coffee @@ -1,49 +1,55 @@ class App.UiElement.business_hours - @render: (attribute, params) -> + @render: (attributeOrig) -> + + attribute = _.clone(attributeOrig) + attribute.nameRaw = attribute.name + attribute.name = "{business_hours}#{attribute.name}" # Martin: our frontend doesn't create 24:00. # you have to check second values ('till') for 00:00 # and convert them to 24:00 - hours = - mon: - active: true - timeframes: [ - ['09:00','17:00'] - ] - tue: - active: true - timeframes: [ - ['00:00','24:00'] - ] - wed: - active: true - timeframes: [ - ['09:00','17:00'] - ] - thu: - active: true - timeframes: [ - ['09:00','12:00'] - ['13:00','17:00'] - ] - fri: - active: true - timeframes: [ - ['09:00','17:00'] - ] - sat: - active: false - timeframes: [ - ['10:00','14:00'] - ] - sun: - active: false - timeframes: [ - ['10:00','14:00'] - ] + if !attribute.value + attribute.value = + mon: + active: true + timeframes: [ + ['09:00','17:00'] + ] + tue: + active: true + timeframes: [ + ['00:00','22:00'] + ] + wed: + active: true + timeframes: [ + ['09:00','17:00'] + ] + thu: + active: true + timeframes: [ + ['09:00','12:00'], + ['13:00','17:00'] + ] + fri: + active: true + timeframes: [ + ['09:00','17:00'] + ] + sat: + active: false + timeframes: [ + ['10:00','14:00'] + ] + sun: + active: false + timeframes: [ + ['10:00','14:00'] + ] businessHours = new App.BusinessHours - hours: hours + attribute: attribute + hours: attribute.value businessHours.render() businessHours.el \ No newline at end of file diff --git a/app/assets/javascripts/app/controllers/_ui_element/ticket_selector.js.coffee b/app/assets/javascripts/app/controllers/_ui_element/ticket_selector.js.coffee index 99c55d157..c8b907fba 100644 --- a/app/assets/javascripts/app/controllers/_ui_element/ticket_selector.js.coffee +++ b/app/assets/javascripts/app/controllers/_ui_element/ticket_selector.js.coffee @@ -79,8 +79,8 @@ class App.UiElement.ticket_selector elementRow = $(e.target).closest('.js-filterElement') @rebuildAttributeSelectors(item, elementRow, groupAndAttribute) - @rebuildOperater(item, elementRow, groupAndAttribute, elements) - @buildValue(item, elementRow, groupAndAttribute, elements) + @rebuildOperater(item, elementRow, groupAndAttribute, elements, undefined, attribute) + @buildValue(item, elementRow, groupAndAttribute, elements, undefined, undefined, attribute) ) # change operator @@ -88,7 +88,7 @@ class App.UiElement.ticket_selector groupAndAttribute = $(e.target).find('.js-attributeSelector option:selected').attr('value') operator = $(e.target).find('option:selected').attr('value') elementRow = $(e.target).closest('.js-filterElement') - @buildValue(item, elementRow, groupAndAttribute, elements, undefined, operator) + @buildValue(item, elementRow, groupAndAttribute, elements, undefined, operator, attribute) ) # build inital params @@ -108,8 +108,8 @@ class App.UiElement.ticket_selector # clone, rebuild and append elementClone = elementFirst.clone(true) @rebuildAttributeSelectors(item, elementClone, groupAndAttribute) - @rebuildOperater(item, elementClone, groupAndAttribute, elements, operator) - @buildValue(item, elementClone, groupAndAttribute, elements, value, operator) + @rebuildOperater(item, elementClone, groupAndAttribute, elements, operator, attribute) + @buildValue(item, elementClone, groupAndAttribute, elements, value, operator, attribute) elementLast.after(elementClone) # remove first dummy row @@ -169,10 +169,10 @@ class App.UiElement.ticket_selector ticket_ids: ticket_ids ) - @buildValue: (elementFull, elementRow, groupAndAttribute, elements, value, operator) -> + @buildValue: (elementFull, elementRow, groupAndAttribute, elements, value, operator, attribute) -> # do nothing if item already exists - name = "condition::#{groupAndAttribute}::value" + name = "#{attribute.name}::#{groupAndAttribute}::value" return if elementRow.find("[name=\"#{name}\"]").get(0) return if elementRow.find("[data-name=\"#{name}\"]").get(0) @@ -237,8 +237,8 @@ class App.UiElement.ticket_selector if groupAndAttribute elementRow.find('.js-attributeSelector select').val(groupAndAttribute) - @buildOperator: (elementFull, elementRow, groupAndAttribute, elements, current_operator) -> - selection = $("") + @buildOperator: (elementFull, elementRow, groupAndAttribute, elements, current_operator, attribute) -> + selection = $("") attributeConfig = elements[groupAndAttribute] if attributeConfig.operator @@ -250,15 +250,15 @@ class App.UiElement.ticket_selector selection.append("") selection - @rebuildOperater: (elementFull, elementRow, groupAndAttribute, elements, current_operator) -> + @rebuildOperater: (elementFull, elementRow, groupAndAttribute, elements, current_operator, attribute) -> return if !groupAndAttribute # do nothing if item already exists - name = "condition::#{groupAndAttribute}::operator" + name = "#{attribute.name}::#{groupAndAttribute}::operator" return if elementRow.find("[name=\"#{name}\"]").get(0) # render new operator - operator = @buildOperator(elementFull, elementRow, groupAndAttribute, elements, current_operator) + operator = @buildOperator(elementFull, elementRow, groupAndAttribute, elements, current_operator, attribute) elementRow.find('.js-operator select').replaceWith(operator) @humanText: (condition) -> diff --git a/app/assets/javascripts/app/lib/app_post/business_hours.js.coffee b/app/assets/javascripts/app/lib/app_post/business_hours.js.coffee index 13486b66c..75043ed29 100644 --- a/app/assets/javascripts/app/lib/app_post/business_hours.js.coffee +++ b/app/assets/javascripts/app/lib/app_post/business_hours.js.coffee @@ -24,6 +24,7 @@ class App.BusinessHours extends Spine.Controller @updateMaxTimeframes() html = App.view('generic/business_hours') + attribute: @attribute days: @days hours: @options.hours maxTimeframes: @maxTimeframes @@ -71,7 +72,8 @@ class App.BusinessHours extends Spine.Controller validate: => for day, hours of @options.hours - break if not hours.active + break if !hours.active + break if !hours.timeframes # edge case: full day if hours.timeframes[0][0] is '00:00' and hours.timeframes[hours.timeframes.length - 1][1] is '00:00' diff --git a/app/assets/javascripts/app/views/generic/business_hours.jst.eco b/app/assets/javascripts/app/views/generic/business_hours.jst.eco index a6333db92..58339a5d5 100644 --- a/app/assets/javascripts/app/views/generic/business_hours.jst.eco +++ b/app/assets/javascripts/app/views/generic/business_hours.jst.eco @@ -4,7 +4,7 @@