Skip to content

Commit

Permalink
🎨 overrides as paramter
Browse files Browse the repository at this point in the history
  • Loading branch information
scottnath committed Aug 31, 2016
1 parent 441b6d2 commit c60ace1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
10 changes: 5 additions & 5 deletions lib/content-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,29 +58,29 @@ const merged = (content, config) => {
* Returns only one content type, merged with input plugins and values
*
* @param {string} type - id of a content type
* @param {object} vals - contains values for inputs
* @param {object} overrides - contains values for inputs
* @param {array} loadedTypes - array of merged content type objects
* @param {object} config - configuration object
*
* @returns {promise} resolves with promise from `only` function
*/
const onlyCT = (type, vals, loadedTypes, config) => {
const values = vals || {};
const onlyCT = (type, overrides, loadedTypes, config) => {
const overs = overrides || {};

let onlyTypes = _.cloneDeep(loadedTypes);

if (loadedTypes) {
const ct = findCT(type, onlyTypes);

return only(ct, values, config);
return only(ct, overs, config);
}

return merged(null, config).then(types => {
onlyTypes = _.cloneDeep(types);

const ct = findCT(type, onlyTypes);

return only(ct, values, config);
return only(ct, overs, config);
});
};

Expand Down
2 changes: 1 addition & 1 deletion lib/content-types/load.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ const load = (config) => {
return a.id === type.identifier;
});

if (attr === undefined) {
if (typeof attr === undefined) {
reject(new Error(`Identifier '${type.identifier}' is not an attribute in content type '${type.name}'.`));
}

Expand Down
22 changes: 11 additions & 11 deletions lib/content-types/only.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ const utils = require('../utils');
* Retrieves one content type's config object, merged with input plugins and a form's values
*
* @param {object} type merged content type object
* @param {object} vals values for inputs
* @param {object} overrides values for inputs
*
* @returns {object} content type object merged with input plugins and values
* @returns {object} content type object merged with input plugins and overrides
*/
const only = (type, vals) => {
const only = (type, overrides) => {
return new Promise((resolve) => {
const mergedType = type;
const values = vals;
const configured = Object.keys(values);
const overs = overrides;
const configured = Object.keys(overs);
const attrs = mergedType.attributes.map(attribute => {
const attr = attribute;
let inputs;
Expand All @@ -33,13 +33,13 @@ const only = (type, vals) => {
}

// check if repeatable but not an array
if (attr.hasOwnProperty('repeatable') && typeof attr.repeatable === 'object' && !Array.isArray(values[attr.id])) {
values[attr.id] = [values[attr.id]];
if (attr.hasOwnProperty('repeatable') && typeof attr.repeatable === 'object' && !Array.isArray(overs[attr.id])) {
overs[attr.id] = [overs[attr.id]];
}

// Flattens multiple instances and updates ids and names
if (Array.isArray(values[attr.id])) {
attr.inputs = values[attr.id].map((data, index) => {
if (Array.isArray(overs[attr.id])) {
attr.inputs = overs[attr.id].map((data, index) => {
const instance = merge(attr.inputs[0], data);
inputs.forEach(input => {
instance[input].validation = attribute.inputs[0][input].validation;
Expand All @@ -52,8 +52,8 @@ const only = (type, vals) => {
});
}
else {
// Merge configuration for the inputs and the values
attr.inputs = merge(attr.inputs, values[attr.id]);
// Merge configuration for the inputs and the overs
attr.inputs = merge(attr.inputs, overs[attr.id]);

// Override the validation, type, and ID for all inputs that matter
inputs.forEach(input => {
Expand Down

0 comments on commit c60ace1

Please sign in to comment.