Created
March 2, 2017 22:15
-
-
Save ericdfields/f3068dd4754e38581e694524b3491978 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import reduce from 'lodash/reduce' | |
import isArray from 'lodash/isArray' | |
const prepareModelForRails = (model) => { | |
if (typeof model !== 'object') { | |
return model | |
} else { | |
if (isArray(model)) { | |
return model.map( m => prepareModelForRails(m) ) | |
} else { | |
return reduce(model, (result, value, key) => { | |
if (isArray(value)) { | |
result[`${key}_attributes`] = prepareModelForRails(value) | |
} else { | |
result[key] = prepareModelForRails(value) | |
} | |
return result | |
}, {}) | |
} | |
} | |
} | |
export default prepareModelForRails |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment