Created
June 18, 2012 22:35
-
-
Save brundage/2951192 to your computer and use it in GitHub Desktop.
Replaces ActiveModel::Serializers with ActiveModel::Serializer
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 JsonSerializationUnifier | |
# ActiveModel::Serializer only works in the controller's render method | |
# This modue replaces ActiveModel::Serializers with ActiveModel::Serializer | |
# yup, a little confusing. Include in in your model classes so to_json and | |
# as_json work as expected in the console and elsewhere. | |
extend ActiveSupport::Concern | |
def as_json(value=nil, use_options = true) | |
return active_model_serializer.as_json if active_model_serializer | |
super | |
end | |
def to_json(options=nil) | |
return active_model_serializer.to_json if active_model_serializer | |
super | |
end | |
private | |
def active_model_serializer_class | |
self.class.respond_to?(:active_model_serializer) && self.class.active_model_serializer | |
end | |
def active_model_serializer | |
@_serializer ||= active_model_serializer_class.new(self) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment