Skip to content

be able to override type in serializer #2131

Open
@InteNs

Description

@InteNs

Expected behavior vs actual behavior

expected the following to work:

class SnapshotSerializer < BaseSerializer
  type do
    # semi-code
    if object.poly?
      object.poly_relation_type
    else
       object.type
    end
  end

  attributes :name, :description
end

I can only place a string/symbol right now, and when I try to place lambda the object isn't available in that scope

What I'm really searching for is to be able to dynamically assign type, based on a condition on the object that is being serialized.

Environment

ActiveModelSerializers Version (commit ref if not on tag): 0.10.5

Output of ruby -e "puts RUBY_DESCRIPTION": ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-darwin16]

OS Type & Version: Mac OS 10.12.4

Integrated application and version (e.g., Rails, Grape, etc): Rails

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions