// BEGIN-SNIPPET fruit-model
import DS from 'ember-data';

import { memberAction, collectionAction, serializeAndPush } from 'ember-api-actions';
import { assign } from '@ember/polyfills';

const { attr, Model } = DS;

function mergeAttributes(attributes) {
  let payload = this.serialize();
  payload.data.attributes = assign(payload.data.attributes, attributes);
  return payload;
}

// This is an example of how to extract JSON API responses and push
// them into the store.
// TODO extract and export as part of this addon
function serializeAndPush(response) {
  const recordClass = this.constructor;
  const modelName = recordClass.modelName;
  const { store } = this;
  const serializer = store.serializerFor(modelName);
  const normalized = isArray(response.data) ? serializer.normalizeArrayResponse(store, recordClass, response) :
        serializer.normalizeSingleResponse(store, recordClass, response);
  return this.store.push(normalized);
}

export default Model.extend({
  name: attr('string'),
  ripen: memberAction({ path: 'doRipen' }),
  info: memberAction({ path: 'info', type: 'get' }),
  ripenAll: collectionAction({ path: 'ripenEverything' }),
  getFresh: collectionAction({ path: 'fresh', type: 'get' }),
  eat: memberAction({
    path: 'doEat',
    before: mergeAttributes,
    after: serializeAndPush
  }),
  eatAll: collectionAction({
    path: 'doEatAll',
    before: mergeAttributes,
    after: serializeAndPush })
});
// END-SNIPPET

@@ -382,576 +382,8 @@
 %0A%7D%0A%0A
-// This is an example of how to extract JSON API responses and push%0A// them into the store.%0A// TODO extract and export as part of this addon%0Afunction serializeAndPush(response) %7B%0A  const recordClass = this.constructor;%0A  const modelName = recordClass.modelName;%0A  const %7B store %7D = this;%0A  const serializer = store.serializerFor(modelName);%0A  const normalized = isArray(response.data) ? serializer.normalizeArrayResponse(store, recordClass, response) :%0A        serializer.normalizeSingleResponse(store, recordClass, response);%0A  return this.store.push(normalized);%0A%7D%0A%0A
 expo
@@ -826,32 +826,32 @@
 ergeAttributes,%0A
-
     after: seria
@@ -861,16 +861,18 @@
 eAndPush
+%0A 
  %7D)%0A%7D);%0A

merge in upstream changes