RestPack / restpack_serializer

Model serialization, paging, side-loading and filtering
MIT License
175 stars 43 forks source link

Allow the default value of `include_xyz?` to be specified easily #116

Closed GavinJoyce closed 9 years ago

GavinJoyce commented 9 years ago

It's currently possible to do serializer.as_json(model, { include_description?: false }) which will exclude the description attribute. The default is to include the description.

It would be nice to be able to easily set the default to not include the attribute. Something like:

class PersonSerializer
    include RestPack::Serializer
    attributes :id, :name, { description: { include_by_default: false } }
end

serializer.as_json(model) would exclude the description serializer.as_json(model, { include_description?: false }) would exclude the description serializer.as_json(model, { include_description?: true }) would include the description

GavinJoyce commented 9 years ago

@eugeneius, how about:

class PersonSerializer
    include RestPack::Serializer

    attributes :id, :name
    optional_attributes :description
end

or

class PersonSerializer
    include RestPack::Serializer

    attributes :id, :name
    optional :description
end
eugeneius commented 9 years ago

Adding them separately definitely reads better than flagging them inline as part of attributes :+1:

eugeneius commented 9 years ago

What about using a similar API to mutations:

class PersonSerializer
  include RestPack::Serializer

  attributes :id, :name

  optional do
    attributes :description
  end
end
GavinJoyce commented 9 years ago

That's nice however I think if I added support for that, the main API should become:

class PersonSerializer
  include RestPack::Serializer

  required do
    attributes :id, :name
  end

  optional do
    attributes :description
  end
end

I think I'll keep it simple for now, I've almost got a PR ready