Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

be able to override type in serializer #2131

Open
InteNs opened this issue May 12, 2017 · 5 comments
Open

be able to override type in serializer #2131

InteNs opened this issue May 12, 2017 · 5 comments

Comments

@InteNs
Copy link

InteNs commented May 12, 2017

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

@bf4
Copy link
Member

bf4 commented May 12, 2017

@InteNs You can use the instance method json_key and/or a root option as described in https://github.com/rails-api/active_model_serializers/blob/0-10-stable/lib/active_model/serializer.rb#L371-L374

@InteNs
Copy link
Author

InteNs commented May 12, 2017

@bf4 Thanks for the quick response, unfortunately I tried that already, the method never got called.

is it correct that the adapter only calls this method if no explicit serializer was given as described here?

allow me to explain my problem further:

this is my controller code:

  def show
    render json: individual,
      **serializer_options,
      include: params[:include],
      fields: params[:fields]
  end

 # individual.pbt_type = DelayRegistration
  def serializer_options
    {
      serializer: "#{individual.pbt_type}Serializer".constantize
    }
  end

snappable type serializer:

class DelayRegistrationSerializer < SnapshotSerializer

  type 'delay-registrations'

  attributes :reason

  has_one :causing_party
  has_one :causing_party_role
  has_one :delay_code_main
  has_one :delay_code_sub

end

snapshot serializer:

# BaseSerializer directly extends ActiveModel::Serializer
class SnapshotSerializer < BaseSerializer

  attributes :name,
             :description,
             :created_at,
             :updated_at

  has_one :user

  has_many :activities
end

now what I would want is to define type in the snapshot serializer dynamically instead of in each implementation of the polymorphic relationship
I tried this in the snapshotserializer but this code wasn't called:

class SnapshotSerializer < BaseSerializer

  def json_key
    'test'
  end

  attributes :name,
             :description,
             :created_at,
             :updated_at

  has_one :user

  has_many :activities
end

@bf4
Copy link
Member

bf4 commented May 12, 2017

You linked to the collection serializer. is the snapshot an association?

@InteNs
Copy link
Author

InteNs commented May 12, 2017

my apologies, that link was incorrect

the json_key method in the snapshot serializer doesn't get called and the jsonapi type stays 'snapshots'

the association between snapshot and delay-registration is only on database level, both serializers need to "join together" to create a single endpoint

that's with the type 'delay-registrations' commented out of course.

@bf4
Copy link
Member

bf4 commented Oct 30, 2017

@InteNs the collection serializer is meant to facilitate serialization items in a collection. It doesn't itself have a type. The type comes from the items being serialized.

I'd advise not trying to use the collection serializer directly (unless maybe if you're following how it works in the source code)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants