You can subclass serializers and then any attributes in the subclass get
added to the attributes defined in the super class.

class WidgetSerializer < ActiveModel::Serializer
  attributes :id, :name, :sub_widget
end

class WidgetWithOption < WidgetSerializer
  has_one :sub_widget
end

If you really don't want a separate class you can override
include_associations to change the logic for how associations are included.

Thanks,
Bradly


On Tue, Sep 23, 2014 at 3:23 PM, Kevin Baker <[email protected]> wrote:

> I would like to have my Custom Active Model Serializer optionally include
> an object based on arguments I pass it.
>
> How would I go about doing this w/o having to write a sep Serializer.
>
>
>
> class WidgetSerializer < ActiveModel::Serializer
>   attributes :id, :name, :sub_widget
>
>   def sub_widget
>
>     # total guess, just an example
>
>     if(sub_widget_option)
>
>       object.sub_widget
>
>     end
>
>   end
>   has_one sub_widget
> end
>
>
> class WidgetController < ActionController::Base
>
>   def index
>     @widgets = Widget.all()
>     render :json=>@widgets, each_serializer:
> WidgetSerializer(sub_widget_option: true)
>   end
>
> end
>
>
>
>  --
> --
> SD Ruby mailing list
> [email protected]
> http://groups.google.com/group/sdruby
> ---
> You received this message because you are subscribed to the Google Groups
> "SD Ruby" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
>

-- 
-- 
SD Ruby mailing list
[email protected]
http://groups.google.com/group/sdruby
--- 
You received this message because you are subscribed to the Google Groups "SD 
Ruby" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to