Created
April 11, 2012 10:24
-
-
Save tobiashm/2358486 to your computer and use it in GitHub Desktop.
This file contains 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
module ModelSerializer | |
def self.serialize(object) | |
object.to_json include: nil # don't include any associations - only serialize shallow object | |
end | |
def self.deserialize(json) | |
hash = ActiveSupport::JSON.decode(json) # => { "section": { "id": 42, ... } } | |
model = hash.keys.first.camelize.constantize # => Section | |
attributes = hash.values.first | |
object = model.new | |
object.assign_attributes attributes, without_protection: true | |
object | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment