added ability to assign a lambda to the root for a serializer #500

Closed
wants to merge 2 commits into from

2 participants

@krenzke

Trying to give a little more flexibility to the root name for a serializer. In addition to allowing a true/false or symbol, I've added the ability to use a lambda as the root which will be called with the object to be serialized. Was useful on a project I was working on, might be useful to others as well.

@stevenharman stevenharman and 1 other commented on an outdated diff Feb 3, 2014
lib/active_model/serializer.rb
@@ -307,6 +307,8 @@ def root_name
if self._root == true
class_name
+ elsif self._root.is_a? Proc

Rather than relying on type checking, what about just checking for #respond_to?(:call), opening this up to any call-able object?

@krenzke
krenzke added a note Feb 3, 2014

good call, with change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@krenzke krenzke check for callable object in root attribute, not just Proc
bc50632
@steveklabnik

I'm not interested in changing the DSL in 0.9, but we'll see if we can't get some of this kind of flexibility into 0.10.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment