Skip to content

Block for conditional field is always called #49

Open
@ezekg

Description

@ezekg

Example

relationship :user, unless: -> { @object.user.nil? } do
  link :related do
    @url_helpers.v1_user_path @object.user
  end
end

Expected outcome

The relationship block is not called when the conditional fails.

Actual outcome

The relationship block is called regardless of the conditional failing; only the display of the serialized relationship is changed. This requires additional checks within the block, which makes the code less readable (especially for nested resource routes) and duplicates the condition:

relationship :user, unless: -> { @object.user.nil? } do
  link :related do
    @url_helpers.v1_user_path @object.user if @object.user.present?
  end
end

Solution

Do not call the block if the condition fails.


Would you be open to a pull request?

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