Skip to content

unless support for conditionals #72

@attilahorvath

Description

@attilahorvath

Currently the gem supports conditional attributes and relationships by passing an if option. I think it would make sense to make it symmetric by also providing an unless option, which would do the opposite.

This would be in line with how many other gems handle conditionals, including AMS and Rails in many places (callbacks, validations, etc.) and well, even the Ruby language itself with the unless keyword.

This feature would turn this:

class MovieSerializer
  include FastJsonapi::ObjectSerializer

  attribute :title, if: ->(movie) { !movie.secret_title? }

  has_many :actors, if: ->(movie) { !movie.in_development? }
end

Into the much nicer and easier to read version:

class MovieSerializer
  include FastJsonapi::ObjectSerializer

  attribute :title, unless: &:secret_title?

  has_many :actors, unless: &:in_development?
end

I'd be happy to submit a PR for this if you guys agree that it's a worthwhile feature.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions