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

Ability To Provide Options Alongside Nested Serializes #41

Open
Seacant opened this issue Dec 20, 2021 · 3 comments
Open

Ability To Provide Options Alongside Nested Serializes #41

Seacant opened this issue Dec 20, 2021 · 3 comments

Comments

@Seacant
Copy link

Seacant commented Dec 20, 2021

When using nested serializers (serialize :thing, with: ThingSerializer), handling all the options that need to be passed in at the top level can quickly become tedious and prone to mistakes. It would be helpful if we could specify options alongside the nested attribute.

class ThingSerializer < CacheCrispies::Base
  ...

  # top-level options overrides what we specify here
  serialize :thing, with: ThingSerializer, with_options: { 
    | model, options |  ({
      foo: true,
      bar: get_default_bar_for(model),
      baz: !options[:anti_baz],
      **options
    })
  }

  # these options are forced overrides no matter what is passed in up top
  serialize :other_thing: with: OtherSerializer, with_options: {
    | model, options | ({
      **options,
      foo_author: model.foo.author
    })
  }
@sander-deryckere
Copy link

sander-deryckere commented Jun 1, 2022

We would also want this feature, But I find this a bit over-engineered.

If it depends on the model, or the existing options, you can use a custom defined function already. But for adding a simple option, having to define a custom function seems overkill.

So we would prefer something like this, where the existing options just get augmented with default new options

class ThingSerializer < CacheCrispies::Base
  serialize :thing, with: ThingSerializer, with_options: {
      foo: true,
      bar: "baz",
    }
end

It's especially useful to serialize a common object (i.e. a user), that has an option to switch between a basic and a full serializer.

@kyleyardlink
Copy link

kyleyardlink commented Aug 8, 2022

Any update on this? we would find it useful too.

@adamcrown
Copy link
Member

Sorry for sitting on this one for so long. I remember reading it earlier, but for some reason didn't comment.

This makes sense to me. Although, I'm not sure I'm 100% happy with either approach. If I have time I'll try to poke around a bit in the code and see what I can come up with.

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

4 participants