diff --git a/app/serializers/application.js b/app/serializers/application.js index e20e6a5..3af4362 100644 --- a/app/serializers/application.js +++ b/app/serializers/application.js @@ -46,6 +46,11 @@ export default DS.RESTSerializer.extend({ } } }, this); + + // run legacy support transformation specified in model serializer + if (typeof this.legacySupport === 'function') { + resourceHash = this.legacySupport(resourceHash); + } return this._super(modelClass, resourceHash, prop); }, diff --git a/app/serializers/poll.js b/app/serializers/poll.js index 40b149d..c21e2eb 100644 --- a/app/serializers/poll.js +++ b/app/serializers/poll.js @@ -1,10 +1,27 @@ import DS from "ember-data"; import ApplicationAdapter from './application'; +import Ember from "ember"; export default ApplicationAdapter.extend(DS.EmbeddedRecordsMixin, { attrs: { users: { deserialize: 'records' } + }, + + legacySupport(resourceHash) { + // croodle <= 0.3.0 + // property "type" of answers was named "id" + if ( + resourceHash.answers.length > 0 && + !Ember.isEmpty(resourceHash.answers[0].id) + ) { + resourceHash.answers.forEach((answer, index) => { + resourceHash.answers[index].type = answer.id; + delete resourceHash.answers[index].id; + }); + } + + return resourceHash; } }); diff --git a/app/serializers/user.js b/app/serializers/user.js new file mode 100644 index 0000000..a3be521 --- /dev/null +++ b/app/serializers/user.js @@ -0,0 +1,33 @@ +import ApplicationAdapter from './application'; +import Ember from 'ember'; + +export default ApplicationAdapter.extend({ + legacySupport: function(resourceHash) { + /* + * Croodle <= 0.3.0: + * * for answer type "freetext": + * selections where a string containing label + * * all other answer types ("YesNo", "YesNoMaybe"): + * selections where stored as child object of "value" property + * and selection property "type" where named "id" + */ + if (!Ember.isEmpty(resourceHash.selections[0].value)) { + resourceHash.selections.forEach(function(selection, index) { + if (typeof selection.value === 'string') { + resourceHash.selections[index] = { + label: selection.value + }; + } else { + resourceHash.selections[index] = { + icon: selection.value.icon, + label: selection.value.label, + labelTranslation: selection.value.labelTranslation, + type: selection.value.id + }; + } + }); + } + + return resourceHash; + } +});